1. 程式人生 > >Linux2.5隱藏權限及特殊權限

Linux2.5隱藏權限及特殊權限

修改 brush 必須 影響 chmod u+s set 理論 div bsp

隱藏權限lsattr_chattr

語法:chattr [+-=] [Asaci] [文件或者目錄名]

#   i   增加後,文件不能刪除、重命名、設定鏈接、寫入或者增加數據
[root@chy002 tmp]# ls -l 1.txt
-rw-rw-rw-. 1 chy002 chy002 0 10月 25 06:08 1.txt
[root@chy002 tmp]# chattr +i 1.txt
[root@chy002 tmp]# ls -l 1.txt
-rw-rw-rw-. 1 chy002 chy002 0 10月 25 06:08 1.txt
[root@chy002 tmp]# vi 1.txt

[No write since last change]
[root@chy002 tmp]# head -n2 /etc/passwd  > 1.txt
-bash: 1.txt: 權限不夠
[root@chy002 tmp]# lsattr 1.txt       #查看隱藏權限
----i----------- 1.txt
#不能touch,因為會修改創建時間;不能rm;不能mv
#   a只能追加,不能刪除,非root用戶不能設定該屬性
[root@chy002 tmp]# chattr +a 2.txt
[root@chy002 tmp]# lsattr 2.txt
-----a---------- 2.txt
[root@chy002 tmp]# rm -f 2.txt
rm: 無法刪除"2.txt": 不允許的操作
[root@chy002 tmp]# mv 2.txt 20.txt
mv: 無法將"2.txt" 移動至"20.txt": 不允許的操作
[root@chy002 tmp]# touch 2.txt
[root@chy002 tmp]# !l
lsattr 2.txt
-----a---------- 2.txt
[root@chy002 tmp]# head -n2 /etc/passwd > 2.txt
-bash: 2.txt: 不允許的操作
[root@chy002 tmp]# head -n2 /etc/passwd >> 2.txt
[root@chy002 tmp]# chattr -a 2.txt

lsattr   -a   連同隱藏文件一同列出來
           -R  連同子目錄的數據一同列出
           -d   僅該目錄本身

  

特殊權限set_uid

該權限是針對二進制可執行文件,使文件在執行階段具有文件所有者的權限。比如普通用戶使用passwd命令,可以臨時獲得root權限即該命令所有者權限,從而更改密碼。Linux僅有這一個自帶suid權限/usr/bin/passwd。

如果增加suid權限後,看到字母大寫的S,這是由於之前的所有者沒有了x執行權限,不受影響。

[root@chy002 chy]# ls -l /usr/bin/passwd
-rwsr-xr-x. 1 root root 27832 6月  10 2014 /usr/bin/passwd
[root@chy002 chy]# su user
[user@chy002 chy]$ ls /root/
ls: 無法打開目錄/root/: 權限不夠
[user@chy002 chy]$ su
密碼:
[root@chy002 chy]# chmod u+s /usr/bin/ls
[root@chy002 chy]# ls -l /usr/bin/ls
-rwsr-xr-x. 1 root root 117616 6月  10 2014 /usr/bin/ls
[root@chy002 chy]# su user
[user@chy002 chy]$ ls /root/
anaconda-ks.cfg

特殊權限set_gid

可以應用在文件上同樣可以作用在目錄上。設置在文件上和set_uid類似,前提這個文件必須是可執行的二進制文件。設置set_gid後,執行該文件的用戶會臨時以該文件所屬組的身份執行。若目錄被設置這個權限後,任何用戶在此目錄下創建的文件或者目錄都具有和該目錄所屬組相同的組。

[root@chyuanliu-01 tmp]# ls -ld chy0826/
drwxr-xr-x. 2 root root 30 8月  27 02:13 chy0826/
[root@chyuanliu-01 tmp]# chown :chy chy0826/
[root@chyuanliu-01 tmp]# ls -ld chy0826/
drwxr-xr-x. 2 root chy 30 8月  27 02:13 chy0826/
[root@chyuanliu-01 tmp]# touch chy0826/iii.txt
[root@chyuanliu-01 tmp]# ls -ld chy0826/iii.txt
-rw-r--r--. 1 root root 0 8月  27 02:17 chy0826/iii.txt
[root@chyuanliu-01 tmp]# chmod g+s chy0826/
[root@chyuanliu-01 tmp]# touch chy0826/ii.txt
[root@chyuanliu-01 tmp]# ls -ld chy0826/ii.txt
-rw-r--r--. 1 root chy 0 8月  27 02:17 chy0826/ii.txt

特殊權限stick_bit

[user@chy002 ~]$ ls -dl /tmp/      
drwxrwxrwt. 9 root root 4096 10月 26 01:23 /tmp/
#可以看到tmp文件權限最後一位是t,這就是防刪除位
一個文件能否被刪除,取決於該文件的父目錄的權限,/tmp/目錄是777,任何人都可寫的,所以理論上任何人都可以刪除/tmp/下的所有文件,但是,user2是不可以刪除user1的文件的,可以修改,只有user1和root可以刪除,這就是因為/tmp/目錄有一個stick_bit。
chmod o+t 目錄    #防止其他用戶刪除自己文件,root用戶除外 

Linux2.5隱藏權限及特殊權限