Linux2 5隱藏許可權及特殊許可權

2022-02-12 11:11:31 字數 3284 閱讀 4344

語法: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 僅該目錄本身

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

如果增加suid許可權後,看到字母大寫的s,這是由於之前的所有者沒有了x執行許可權,不受影響。不能直接 u=rws    可以u=rwsx或者 u=rws u+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_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

[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使用者除外

linux學習筆記 特殊許可權和隱藏許可權

linux 檔案隱藏屬性 chattr 只能在ext2 ext3 上生效。chattr asacdistu file a 設定a後,這個檔案只能增加資料,而不能刪除也不能修改資料,只有root.對於登入log 文件有用。i 讓檔案不能被修改 刪除 改名設定鏈結,也無法寫入新的資料,提高安全性。cha...

Linux 檔案許可權及特殊許可權管理

第一位指檔案型別,常見檔案型別有 普通檔案 d 目錄檔案 b 塊裝置檔案 block c 字元裝置檔案 character p 命令管道檔案 pipe s 套接字檔案 socket 後面的9位指檔案許可權 每三位一組,前三位是檔案所有者 u 中間三位指檔案所屬組 g 後三位指其他使用者 o 檔案的大...

Linux特殊許可權設定及ACL許可權

檔案特殊許可權 特殊許可權 說明suid 當乙個設定了suid的檔案被執行時,該檔案將以其所有者的身份執行,而不是執行者的許可權。sgid 當乙個檔案設定了sgid時,該檔案將以其所屬組的身份執行,而不是命令執行者的許可權 目錄特殊許可權 特殊許可權 說明sgid 存放在該目錄的檔案僅准許其屬主執行...