天天看點

e盤是否具有讀寫權限_檔案權限管理

一、Linux使用者權限解析
我們linux伺服器上有嚴格的權限等級,如果權限過高導緻誤操作會增加伺服器的風險。是以對于了解linux系統中的各種權限及要給使用者,服務等配置設定合理的權限十分重要

1.基本權限 UGO

=====================================================

檔案權限設定: 可以賦于某個使用者或組 能夠以何種方式 通路某個檔案

e盤是否具有讀寫權限_檔案權限管理
權限對象:
 屬主------->u
 屬組------->g
 其他人------>o
 基本權限類型:
 讀(read):r   ---->4
 寫(write):w  ---->2
 執行: x(exec) ----->1
           
1.1.設定權限
chown:改變檔案或目錄的所屬主以及所屬組
 chmod:為檔案或目錄設定通路權限
           

更改檔案的屬主(所有者)、屬組 (所屬組)

chown

[[email protected] ~]# chown alice.hr file1.txt  //修改屬主、屬組
 [[email protected] ~]# chown tom  file1.txt  //修改屬主
 [[email protected] ~]# chown .it file1.txt   //隻改屬組
 [[email protected] ~]# chown -R alice.hr dir1 //遞歸修改---針對目錄
           

更改權限

a. 使用符号
e盤是否具有讀寫權限_檔案權限管理
[[email protected] ~]# chmod u+x file1.txt     //屬主增加執行
 [[email protected] ~]# chmod a=rwx file1.txt  //所有人等于讀寫執行
 [[email protected] ~]# chmod a=- file1.txt   //所有人都沒有權限
 [[email protected] ~]# chmod ug=rw,o=r file1.txt  //屬主屬組等于讀寫,其他人隻讀
 [[email protected] ~]# ll
 -rw-rw-r--. 1 tom   it      0 Nov  1 15:30 file1.txt
           
b.使用數字
[[email protected] ~]# chmod 644 file1.txt 
 [[email protected] ~]# ll file1.txt 
 -rw-r--r--. 1 tom it 0 Nov  1 15:30 file1.txt
 ​
 [[email protected] ~]# chmod 755 file1.txt
 [[email protected] ~]# ll
 -rwxr-xr-x  1 root root    0 Jul 23 22:40 file1.txt
 ​
 [[email protected] ~]# chmod 521 file1.txt
 [[email protected] ~]# ll
 -r-x-w---x  1 root root    0 Jul 23 22:40 file1.txt
           

權限掩碼

umask 使用者掩碼

控制使用者建立檔案和目錄的預設權限

root使用者預設權限

目錄777 檔案666

檢視umask
 [[email protected] ~]#umask
 0022 root賬戶預設
 0002 普通使用者預設
 ​
 修改umask
 [[email protected] ~]#umask 0111
 ​
 通過計算得出root使用者建立目錄和檔案的權限為
           
r、w、x權限對檔案和目錄的意義
e盤是否具有讀寫權限_檔案權限管理

對檔案:

r----cat

w ---vi、vim

x ---- bash /dir/file

對目錄:

r ---ls

w -----touch、rm

x ---- cd

2.2.rwx對檔案的影響

實戰案例1:rwx對檔案的影響

[[email protected] ~]# vim /home/file1
 date
 [[email protected] ~]# ll /home/file1 
 -rw-r--r--. 1 root root 5 Nov  3 15:19 /home/file1
 ​
 [[email protected] ~]# su - alice  #切換普通使用者
 [[email protected] ~]$ cat /home/file1 
 date
 [[email protected] ~]$ /home/file1   #執行檔案
 -bash: /home/file1: Permission denied
 [[email protected] ~]$ exit
 logout
 [[email protected] ~]# chmod o+x /home/file1
 [[email protected] ~]$ /home/file1 
 Sun Nov  3 15:26:21 CST 2019
 ​
 [[email protected] ~]# chmod o+w /home/file1 
 [[email protected] ~]$ vim /home/file1
 date
 123
 ls
           
2.3.rwx對目錄的影響

實戰案例2:對目錄沒有w,對檔案有rwx

[[email protected] ~]# mkdir /dir10
 [[email protected] ~]# touch /dir10/file1
 [[email protected] ~]# chmod 777 /dir10/file1 
 [[email protected] ~]# ll -d /dir10/
 drwxr-xr-x. 2 root root 19 Nov  3 15:37 /dir10/
 [[email protected] ~]# ll /dir10/file1 
 -rwxrwxrwx. 1 root root 0 Nov  3 15:37 /dir10/file1
 [[email protected] ~]# vim /dir10/file1
 jack
 [[email protected] ~]# su - alice
 Last login: Sun Nov  3 15:28:06 CST 2019 on pts/0
 [[email protected] ~]$ cat /dir10/file1 
 jack
 [[email protected] ~]$ rm -rf /dir10/file1   #權限不夠
 rm: cannot remove ‘/dir10/file1’: Permission denied
 [[email protected] ~]$ touch /dir10/file2   #權限不夠
 touch: cannot touch ‘/dir10/file2’: Permission denied
           

實戰案例3:對目錄有w,對檔案沒有任何權限

[[email protected] ~]# chmod 777 /dir10/
 [[email protected] ~]# chmod 000 /dir10/file1 
 [[email protected] ~]# ll -d /dir10/
 drwxrwxrwx. 2 root root 19 Nov  3 15:38 /dir10/
 [[email protected] ~]# ll /dir10/file1 
 ----------. 1 root root 5 Nov  3 15:38 /dir10/file1
 [[email protected] ~]# su - alice   #切換普通使用者
 Last login: Sun Nov  3 15:38:53 CST 2019 on pts/0
 [[email protected] ~]$ cat /dir10/file1 
 cat: /dir10/file1: Permission denied    #沒有權限
 [[email protected] ~]$ rm -rf /dir10/file1 
 [[email protected] ~]$ touch /dir10/file2
 小結
 對目錄有w權限,可以在目錄中建立新檔案,可以删除目錄中的檔案(跟檔案權限無關)
 注意事項
 檔案: x 權限小心給予
 目錄: w 權限小心給予
           

進階權限

進階權限 suid,sgid,sticky

問題1: 為什麼會失敗!

[[email protected] ~]# chown root.root /root/file1.txt
 [[email protected] ~]# vim /root/file1.txt
 123
 [[email protected] ~]# ll /root/file1.txt 
 -rw-r--r--. 1 root root 0 Nov  1 15:30 /root/file1.txt
 ​
 [[email protected] ~]# su - alice
 Last login: Sun Nov  3 15:57:41 CST 2019 on pts/0
 [[email protected] ~]$ cat /root/file1.txt
 cat: /root/file1.txt: Permission denied
           
1.1.進階權限的類型
suid ==== 4 提權 (隻對二進制指令檔案生效,其他不管用)
 sgid ==== 2 組權限繼承    (隻能對目錄設定)
 sticky == 1 (t權限)  權限控制
           
1.2.設定特殊權限
a、字元---文法:
 chmod u+s file
 chmod g+s dir 
 chmod o+t dir
 ​
 b、數字
 chmod 4777 file 
 chmod 2770 dir 
 chmod 1770 dir
           
案例一
suid   普通使用者通過suid提權     <針對檔案>
 在程序檔案(二進制,可執行的指令檔案)上增加suid權限
 [[email protected] ~]# chmod u+s /usr/bin/cat
 [[email protected] ~]# chmod u+s /usr/bin/rm
 [[email protected] ~]# su - alice
 Last login: Wed Nov  6 17:40:40 CST 2019 on pts/0
 [[email protected] ~]$ cat /root/file1.txt
 123
 [[email protected] ~]$ rm -rf /root/file1.txt
           
Set UID
那麼這個特殊權限的特殊性的作用是什麼呢?
 1、SUID權限僅對指令檔案(二進制檔案)有效;
 2、執行者将具有該程式擁有者(owner)的權限。
           
取消提權
[[email protected] ~]# ll /usr/bin/rm
 -rwsr-xr-x. 1 root root 62864 Nov  6  2016 /usr/bin/rm
 此時一旦給rm加上suid權限之後,普通使用者相當于root使用者。(即提權)
 [[email protected] ~]# chmod u-s /usr/bin/rm  #取消提權
           
案例二

首先建立一個使用者組,兩個使用者進行這三個案例操作

Set GID
把s放到檔案的所屬使用者組的x位置上的話,就是SGID。那麼SGID的功能是什麼呢?和SUID一樣,隻是SGID是獲得該程式所屬使用者組的權限。
 SGID主要用在目錄上-----如果使用者在此目錄下具有w權限的話,使用者在此目錄下建立新檔案,則建立的這個檔案的群組與此目錄的群組相同。
           

案例

[[email protected] ~]# mkdir /opt/dir1  #建立目錄
 [[email protected] ~]# groupadd hr  #建立一個組
 [[email protected] ~]# chmod 775 /opt/dir1/  #設定權限
 [[email protected] ~]# ll -d /opt/dir1/
 drwxrwxr-x. 2 root root 6 Nov  6 21:26 /opt/dir1/
 [[email protected] ~]# chown .hr /opt/dir1/  #設定屬組
 [[email protected] ~]# chmod g+s /opt/dir1/  #設定sgid
 [[email protected] ~]# ll -d /opt/dir1/
 drwxrwsr-x. 2 root hr 6 Nov  6 21:26 /opt/dir1/
 [[email protected] ~]# touch /opt/dir1/a.txt
 [[email protected] ~]# ll /opt/dir1/a.txt
 -rw-r--r--. 1 root hr 0 Nov  6 21:33 /opt/dir1/a.txt
 ​
 [[email protected] ~]# chmod o+w /opt/dir1/ -R 
 [[email protected] ~]# su - alice
 Last login: Wed Nov  6 21:34:59 CST 2019 on pts/2
 [[email protected] ~]$ touch /opt/dir1/b.txt
 [[email protected] ~]$ ll /opt/dir1/b.txt
 -rw-rw-r--. 1 alice hr 0 Nov  6 21:35 /opt/dir1/b.txt
           
Sticky Bit
這個就是針對others來設定的了,和上面兩個一樣,隻是功能不同而已。
 SBIT(Sticky Bit)目前隻針對目錄有效,對于目錄的作用是:當使用者在該目錄下建立檔案或目錄時,僅有自己與 root才有權力删除。
           

案例

[[email protected] ~]# cd /home/
 [[email protected] home]# mkdir dir2
 [[email protected] home]# chmod 757 dir2/
 [[email protected] home]# chmod o+t dir2/
 [[email protected] home]# ll -d dir2/
 drwxr-xrwt. 2 root root 52 Oct 31 16:49 dir2/
 [[email protected] home]# useradd jack  #建立使用者
 [[email protected] home]# su - alice 
 Last login: Wed Nov  6 21:48:12 CST 2019 on pts/2
 [[email protected] ~]$ touch /home/dir2/alice.txt  #使用者alice建立檔案
 [[email protected] ~]$ exit
 logout
 [[email protected] home]# su - jack 
 Last login: Wed Nov  6 21:48:36 CST 2019 on pts/2
 [[email protected] ~]$ touch /home/dir2/jack.txt   #使用者jack建立檔案
 [[email protected] ~]$ rm -rf /home/dir2/alice.txt 
 rm: cannot remove ‘/home/dir2/alice.txt’: Operation not permitted
 測試jack删除alice建立的檔案,無法删除
           
1.3.目前兩種給普通使用者提權手段:
sudo: 有針對性,例如針對某個使用者以能夠以root的身份執行某些指令。
 suid: 基本針對所有使用者,任何使用者在執行有suid權限的程式時(例如/usr/bin/rm),都是以root身份在執行。