Linux學習記錄--文件特殊權限

文件特殊權限


文件除了讀寫(r),寫(w),執行(x) 權限,還有些特殊權限(s,t)

SUID

功能:

SUID權限僅對二進制程序有效

執行者對於程序需要有X可執行的權限

執行者將均有改程序所有者的權限

本權限只在執行程序過程中有效

 

舉例:

普通用戶也可以通過命令passwd修改自己的密碼。修改的密碼內容將會記錄/etc/shadow文件中,但是普通用戶對這個文件無任何權限,那如何修改這個文件呢?

 

以上步驟可以理解爲這樣

普通用戶執行passwd命令修改密碼èèpasswd命令程序修改/etc/shadow文件將密碼記錄其中

 

[root@localhost /]# ll /etc/shadow/usr/bin/passwd 

-r-------- 1 root root  1352 02-14 10:36 /etc/shadow

-rwsr-xr-x 1 root root 23420 2010-08-11/usr/bin/passwd

 

這就是SUID的功能,當普通用戶在執行passwd程序命令時,由於passwd具有SUID權限,同時普通用戶對於passwd命令具有X權限,那麼在passwd執行過程中普通用戶將程序所有者(root)的權限,因此/etc/shadow就可以被修改

 

SGID

SGID對於二進制程序來說,功能和SUID差不多

SGID權限對二進制程序有效

執行者對於程序需要有X可執行的權限

執行者將均有改程序用戶組的權限

本權限只在執行程序過程中有效

 

SGID也可以針對目錄設置,功能如下

用戶在具有SGID權限的目錄下創建的文件或目錄其所屬的用戶組就是目錄所有的有戶組

 

說明,默認情況下用戶創建的文件所屬的用戶組爲用戶的有效用戶組

 

舉例


[root@localhost /]# mkdir -m 777 /tmp/newdir;ll -d /tmp/newdir
drwxrwxrwx 2 root root 4096 03-10 12:35 /tmp/newdir
[root@localhost /]# cd /tmp/newdir/
[root@localhost newdir]# touch rootfile
[root@localhost newdir]# ll rootfile 
-rw-r--r-- 1 root root 0 03-10 12:35 rootfile
=>所屬用戶和所屬用戶組都是root
[root@localhost newdir]# su tkf
=>切換到普通用戶
[tkf@localhost newdir]$ touch tkffile
[tkf@localhost newdir]$ ll 
-rw-r--r-- 1 root root 0 03-10 12:35 rootfile
-rw-rw-r-- 1 tkf  tkf  0 03-10 12:36 tkffile
=>所屬用戶和所屬用戶組都是tkf

[root@localhost ~]# chmod g+s /tmp/newdir/
=>給newdir添加SGID權限
[root@localhost ~]# ll -d /tmp/newdir/
drwxrwsrwx 2 root root 4096 03-10 12:36 /tmp/newdir/

[root@localhost ~]# su tkf
[tkf@localhost root]$ touch /tmp/newdir/sgidfile;ll /tmp/newdir/
-rw-r--r-- 1 root root 0 03-10 12:35 rootfile
-rw-rw-r-- 1 tkf  root 0 03-10 12:40 sgidfile
-rw-rw-r-- 1 tkf  tkf  0 03-10 12:36 tkffile
=> sgidfile文件所屬用戶組發生變化,和目錄(newdir)的用戶組一樣

 

SBIT

 

SBIT只針對目錄有效,其主要功能是

當用戶擁有目錄的WX權限時,用戶可以刪除(刪除,重命名,移動)目錄下的任意文件

當目錄擁有SBIT權限時,即使用戶擁有目錄的WX權限,用戶只能刪除自己創建的文件(可以修改不是自己創建的文件)。root用戶都可以刪除

舉例 

 

[root@localhost ~]# mkdir -m 1777 /tmp/bitdir
[root@localhost ~]# su tkf
[tkf@localhost root]$ ll -d /tmp/bitdir/
drwxrwxrwt 2 root root 4096 03-10 12:59 /tmp/bitdir/
=>tkf用戶擁有目錄的rwx權限
[tkf@localhost root]$ touch /tmp/bitdir/tkffile ;ll /tmp/bitdir/
-rw-rw-r-- 1 tkf tkf 0 03-10 12:59 tkffile
=>在目錄下創建文件

[tkf@localhost root]$ su userA
=>切換另一個用戶
[userA@localhost root]$ ll -d /tmp/bitdir/
drwxrwxrwt 2 root root 4096 03-10 12:59 /tmp/bitdir/
=> userA用戶擁有目錄的rwx權限

[userA@localhost root]$ cd /tmp/bitdir/
[userA@localhost bitdir]$ touch userfile;ll
-rw-rw-r-- 1 tkf   tkf   0 03-10 12:59 tkffile
-rw-rw-r-- 1 userA userA 0 03-10 13:04 userfile
=>在目錄下創建文件userfile

[userA@localhost bitdir]$ rm tkffile 
rm:是否刪除有寫保護的 一般空文件 “tkffile”? y
rm: 無法刪除 “tkffile”: 不允許的操作
=>由於目錄具有SBIT權限 雖然userA對目錄具有WX權限,但是不能刪除非他創建的文件

[root@localhost ~]# chmod o-t /tmp/bitdir/
=>將目錄SBIT權限去掉
[root@localhost ~]# su userA
[userA@localhost root]$ cd /tmp/bitdir/
[userA@localhost bitdir]$ ll
-rw-rw-r-- 1 tkf   tkf   0 03-10 12:59 tkffile
-rw-rw-r-- 1 userA userA 0 03-10 13:04 userfile
[userA@localhost bitdir]$ rm tkffile 
rm:是否刪除有寫保護的 一般空文件 “tkffile”? y
[userA@localhost bitdir]$ ll
-rw-rw-r-- 1 userA userA 0 03-10 13:04 userfile
=>可以刪除不是自己創建的文件

 

權限設置方法

 

和設置和基本權限(rwx)方法基本,可以通過數字設置也可以通過符號設置

數字設置

SUID:4

SGID:2

SBIT:1

符號設置(+-=)

SUID:u+s

SGID:g+s

SBIT:o+t

舉例:

[root@localhost ~]# touch test
[root@localhost ~]# chmod 4755 test; ll test 
-rwsr-xr-x 1 root root 0 03-10 13:14 test
[root@localhost ~]# chmod 6755 test; ll test 
-rwsr-sr-x 1 root root 0 03-10 13:14 test
[root@localhost ~]# chmod 1755 test; ll test 
-rwxr-xr-t 1 root root 0 03-10 13:14 test
[root@localhost ~]# chmod 7666 test; ll test 
-rwSrwSrwT 1 root root 0 03-10 13:14 test
=>這裏S,T都是大寫是因爲文件本身不具有X權限,而S,T權限設置成功的
=>前提是文件具有X權限,因此大寫的ST代表文件無這些特殊權限


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章