Linux-SUID-SGID-SBIT

3.文件管理

本章同步視頻:https://edu.51cto.com/sd/e4874

3.3.3 SUID、SGID、SBIT

1.SUIDide

(1)SUID測試

l  SUID 權限僅對二進制程序(binary program)有效;spa

l  執行者對於該程序須要具備 x 的可執行權限;orm

l  本權限僅在執行該程序的過程當中有效 (run-time);視頻

l  執行者將具備該程序擁有者 (owner) 的權限。rem

(2)無SUID的效果get

[root@localhost tmp]# ll aaa 同步

-rw-r-----. 1 root root 10 Mar 13 21:42 aaait

[root@localhost tmp]# su - calfio

Last login: Fri Mar 13 21:26:08 CST 2020 on pts/0

[calf@localhost ~]$ cd /tmp/

[calf@localhost tmp]$ cat aaa

cat: aaa: Permission denied

#注:注意帳號切換

(3)設置SUID

[root@localhost ~]# ll /bin/cat

-rwxr-xr-x. 1 root root 54048 Jan 25  2014 /bin/cat

[root@localhost ~]# chmod 4755 /bin/cat

[root@localhost ~]# ll /bin/cat

-rwsr-xr-x. 1 root root 54048 Jan 25  2014 /bin/cat

注:設置SUID的另一種方法:

[root@localhost tmp]# chmod u+s /bin/cat

(4)設置SUID後的效果

[root@localhost ~]# su - calf

Last login: Fri Mar 13 22:01:39 CST 2020 on pts/0

[calf@localhost ~]$ cd /tmp/

[calf@localhost tmp]$ cat aaa

aaaa

bbbb

#注:注意帳號切換

(5)S與s的區別

[root@localhost tmp]# chmod u-x /bin/cat

[root@localhost tmp]# ll /bin/cat

-rwSr-xr-x. 1 root root 54048 Jan 25  2014 /bin/cat

#注:1.S表示文件擁有者無x權限,s表示文件擁有者有x權限。

         2.命令執行者必須具備x權限,不受文件擁有者是否有x權限的影響。

2.SGID

(1)SGID對文件的效果

l  SGID 對二進制程序有用;

l  程序執行者對於該程序來講,需具有 x 的權限;

l  執行者在執行的過程當中將會得到該程序羣組的支持!

#注:效果和SUID同樣,只是SUID是得到擁有者的權限,而SGID是得到羣組的權限。

(2)SGID對目錄的效果

l  用戶若對於此目錄具備 r 與 x 的權限時,該用戶可以進入此目錄;

l  用戶在此目錄下的有效羣組(effective group)將會變成該目錄的羣組;

l  若用戶在此目錄下具備 w 的權限(能夠新建檔案),則使用者所創建的新檔案,該新檔案的羣組與此目錄的羣組相同。

(3)SGID對目錄效果測試

[root@localhost tmp]# mkdir dir

[root@localhost tmp]# chmod 2777 dir       

#設置SGID,同時開通滿權限

[root@localhost tmp]# ll -d dir/

drwxrwsrwx. 2 root root 17 Mar 13 22:20 dir/

#注:其餘用戶須要有寫權限

[root@localhost tmp]# touch dir/root        

#root帳號建立了文件root

[root@localhost tmp]# su - calf

Last login: Fri Mar 13 22:05:04 CST 2020 on pts/0

[calf@localhost ~]$ touch /tmp/dir/calf    

#calf帳號建立了文件calf

[root@localhost tmp]# ll dir

total 0

-rw-rw-r--. 1 calf root 0 Mar 13 22:26 calf

-rw-r--r--. 1 root root 0 Mar 13 22:20 root

#注:不一樣用戶建立的文件都屬於root組,即父目錄dir所屬的組。

(4)s與S的含義

[root@localhost tmp]# chmod g-x dir/

[root@localhost tmp]# ll -d dir/

drwxrwSrwx. 2 root root 41 Mar 13 22:30 dir

#注:1.S表示同組用戶無x權限,s表示同組用戶有x權限。

         2.文件的建立者必須對該目錄有w權限,與同組用戶是否具備w權限無關。

3.SBIT

(1)SBIT的效果

l  當用戶對於此目錄具備 w, x 權限,亦即具備寫入的權限時;

l  當用戶在該目錄下創建檔案或目錄時,僅有本身與 root 纔有權力刪除該檔案。

(2)設置SBIT

[root@localhost tmp]# mkdir dir

[root@localhost tmp]# chmod 1777 dir

[root@localhost tmp]# ll -d dir

drwxrwxrwt. 2 root root 6 Mar 13 22:47 dir

#注:只加SBIT的命令也能夠寫成:chmod o+t dir

(3)準備環境

[root@localhost tmp]# su - calf        #切換到calf

Last login: Fri Mar 13 22:37:37 CST 2020 on pts/0

[calf@localhost ~]$ touch /tmp/dir/calf{1,2,3}   #建立3個文件

[root@localhost tmp]# su - stu         #切換到stu

Last login: Thu Mar  5 10:56:32 CST 2020 on pts/2

[stu@localhost ~]$ touch /tmp/dir/stu{1,2,3}     #建立3個文件

[root@localhost tmp]# ll dir

total 0

-rw-rw-r--. 1 calf calf 0 Mar 13 22:50 calf1

-rw-rw-r--. 1 calf calf 0 Mar 13 22:50 calf2

-rw-rw-r--. 1 calf calf 0 Mar 13 22:50 calf3

-rw-rw-r--. 1 stu  stu  0 Mar 13 22:51 stu1

-rw-rw-r--. 1 stu  stu  0 Mar 13 22:51 stu2

-rw-rw-r--. 1 stu  stu  0 Mar 13 22:51 stu3

(4)測試刪除

[calf@localhost ~]$ cd /tmp/dir/

[calf@localhost dir]$ ls

calf1  calf2  calf3  stu1  stu2  stu3

[calf@localhost dir]$ rm -f calf1     #測試刪除文件

[calf@localhost dir]$ ls

calf2  calf3  stu1  stu2  stu3           #文件確實被刪除

[calf@localhost dir]$ rm -f stu1

rm: cannot remove ‘stu1’: Operation not permitted    

#但不能刪除別人的文件

#注:用戶對目錄具備w權限,便可刪除該目錄下的文件。但此例中,用戶只能刪除擁有者是本身的文件,不能刪除擁有者是其餘用戶的文件。這就是SBIT的效果。

(5)T和t的區別

[root@localhost tmp]# chmod o-x dir/

[root@localhost tmp]# ll -d  dir

drwxrwxrwT. 2 root root 63 Mar 13 22:54 dir

#注:1.T表示其餘用戶不具備x權限,t表示其餘用戶具備x權限。

         2.操做者須要對目錄具備w權限。

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息