鍍金池/ 教程/ Linux/ 文件特殊權(quán)限
Linux 學(xué)習(xí)記錄--開機(jī)掛載錯(cuò)誤
日志系統(tǒng)
數(shù)據(jù)流重定向
內(nèi)存交換空間的構(gòu)建
文件系統(tǒng)簡(jiǎn)介
Linux 學(xué)習(xí)記錄--軟件安裝 RPM|SRPM|YUM
文件特殊權(quán)限
目錄配置 FHS
文件內(nèi)容查閱
Boot Loader
文件壓縮
Linux 學(xué)習(xí)記錄--文件權(quán)限
Linux 命令縮寫
命令與文件的查詢
文件|目錄的默認(rèn)權(quán)限與隱藏權(quán)限
shell script
服務(wù)
Linux 學(xué)習(xí)記錄--程序編譯與函數(shù)庫(kù)
正則表達(dá)式與其應(yīng)用
關(guān)機(jī)相關(guān)指令
shell
vim 與 vi 常用命令
系統(tǒng)調(diào)用:進(jìn)程控制
文件系統(tǒng)簡(jiǎn)單操作
磁盤掛載與卸載
有名管道通訊
磁盤分區(qū),格式化與檢驗(yàn)
工作管理與進(jìn)程管理
匿名管道通訊
Linux 學(xué)習(xí)記錄--啟動(dòng)流程
文件與目錄管理
管道命令
命名別名與歷史命令
文件備份|還原
shell變量
Linux 學(xué)習(xí)記錄--ACL 權(quán)限控制
內(nèi)核|內(nèi)核模塊編譯
文件管理相關(guān)系統(tǒng)編程

文件特殊權(quán)限

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

SUID

功能
SUID 權(quán)限僅對(duì)二進(jìn)制程序有效
執(zhí)行者對(duì)于程序需要有 X 可執(zhí)行的權(quán)限
執(zhí)行者將均有改程序所有者的權(quán)限
本權(quán)限只在執(zhí)行程序過(guò)程中有效

舉例
普通用戶也可以通過(guò)命令 passwd 修改自己的密碼。修改的密碼內(nèi)容將會(huì)記錄/etc/shadow 文件中,但是普通用戶對(duì)這個(gè)文件無(wú)任何權(quán)限,那如何修改這個(gè)文件呢?

以上步驟可以理解為這樣
普通用戶執(zhí)行 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 的功能,當(dāng)普通用戶在執(zhí)行 passwd 程序命令時(shí),由于 passwd 具有 SUID 權(quán)限,同時(shí)普通用戶對(duì)于 passwd 命令具有 X 權(quán)限,那么在 passwd 執(zhí)行過(guò)程中普通用戶將程序所有者(root)的權(quán)限,因此/etc/shadow 就可以被修改

SGID

SGID 對(duì)于二進(jìn)制程序來(lái)說(shuō),功能和 SUID 差不多
SGID 權(quán)限對(duì)二進(jìn)制程序有效
執(zhí)行者對(duì)于程序需要有 X 可執(zhí)行的權(quán)限
執(zhí)行者將均有改程序用戶組的權(quán)限
本權(quán)限只在執(zhí)行程序過(guò)程中有效

SGID 也可以針對(duì)目錄設(shè)置,功能如下
用戶在具有 SGID 權(quán)限的目錄下創(chuàng)建的文件或目錄其所屬的用戶組就是目錄所有的有戶組

說(shuō)明,默認(rèn)情況下用戶創(chuàng)建的文件所屬的用戶組為用戶的有效用戶組

舉例

[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 權(quán)限
[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 文件所屬用戶組發(fā)生變化,和目錄(newdir)的用戶組一樣

SBIT

SBIT 只針對(duì)目錄有效,其主要功能是 當(dāng)用戶擁有目錄的 WX 權(quán)限時(shí),用戶可以刪除(刪除,重命名,移動(dòng))目錄下的任意文件 當(dāng)目錄擁有 SBIT 權(quán)限時(shí),即使用戶擁有目錄的 WX 權(quán)限,用戶只能刪除自己創(chuàng)建的文件(可以修改不是自己創(chuàng)建的文件)。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 權(quán)限
[tkf@localhost root]$ touch /tmp/bitdir/tkffile ;ll /tmp/bitdir/
-rw-rw-r-- 1 tkf tkf 0 03-10 12:59 tkffile
=>在目錄下創(chuàng)建文件

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

[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
=>在目錄下創(chuàng)建文件 userfile

[userA@localhost bitdir]$ rm tkffile 
rm:是否刪除有寫保護(hù)的 一般空文件 “tkffile”? y
rm: 無(wú)法刪除 “tkffile”: 不允許的操作
=>由于目錄具有 SBIT 權(quán)限 雖然 userA 對(duì)目錄具有 WX 權(quán)限,但是不能刪除非他創(chuàng)建的文件

[root@localhost ~]# chmod o-t /tmp/bitdir/
=>將目錄 SBIT 權(quán)限去掉
[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:是否刪除有寫保護(hù)的 一般空文件 “tkffile”? y
[userA@localhost bitdir]$ ll
-rw-rw-r-- 1 userA userA 0 03-10 13:04 userfile
=>可以刪除不是自己創(chuàng)建的文件

權(quán)限設(shè)置方法

和設(shè)置和基本權(quán)限(rwx)方法基本,可以通過(guò)數(shù)字設(shè)置也可以通過(guò)符號(hào)設(shè)置

數(shù)字設(shè)置

SUID:4
SGID:2
SBIT:1

符號(hào)設(shè)置(+-=)

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 都是大寫是因?yàn)槲募旧聿痪哂蠿權(quán)限,而 S,T 權(quán)限設(shè)置成功的
=>前提是文件具有 X 權(quán)限,因此大寫的 ST 代表文件無(wú)這些特殊權(quán)限