天天看點

容易忘記的linux指令之chattr lsattr 設定隐藏權限與特殊權限的設定

Attr了解為屬性,attribution

一lsattr

列出隐藏屬性資訊

參數 :

-d 隻列出目錄

-R 遞歸顯示隐藏資訊,就是所有子目錄中的檔案得到周遊

-a 包括隐藏檔案和.目錄..目錄

[root@localhost test]# ll -R

.:

total 4

-rw-r--r--. 1 root root 0 Apr 21 07:20 1.txt

drwxr-xr-x. 2 root root 4096 Apr 21 07:23 test2

./test2:

total 0

-rw-r--r--. 1 root root 0 Apr 21 07:23 2.txt

[root@localhost test]# lsattr -R

-------------e- ./test2

-------------e- ./test2/2.txt

-------------e- ./1.txt

這裡,我們建立一個.test檔案,然後lsattr一下

[root@localhost test]# touch .test

[root@localhost test]# lsattr

預設情況下,我們發現,lsattr隻顯示目錄下的直覺的,隐藏的檔案,遞歸的目錄均不顯示我們用,lsattr a試一下

[root@localhost test]# lsattr -a

--------------- ./..

-------------e- ./.

-------------e- ./.test#隐藏檔案

當我們想顯示某個目錄的隐藏屬性時,如test,可以進入上層目錄,輸入lsattr d /PATH/TO/DIR

[root@localhost /]# lsattr -d /test

-------------e- /test

二 chattr

①chattr [|+] i /PATH/TO/FILE OR DIR

I了解為自我I,這時候檔案始終存在,切不能被删除與添加,想象系統重要檔案,

[root@localhost /]# chattr +i /test/

[root@localhost /]# touch haha.txt /test

touch: setting times of `/test': Permission denied

②chattr [-|+]a /PATH/TO/FILE OR DIR

a了解為add增加,這時候,檔案隻能增加,也不可以被删除,想象日志檔案log

[root@localhost /]# chattr +a /test/1.txt

[root@localhost /]# lsattr /test/1.txt

-----a-------e- /test/1.txt

[root@localhost /]# echo "hello" >/test/1.txt

-bash: /test/1.txt: Operation not permitted#權限不夠,因為設定了add權限

[root@localhost /]# rm /test/1.txt

rm: remove regular empty file `/test/1.txt'? y

rm: cannot remove `/test/1.txt': Operation not permitted#也不能删除,日志當然不能删除咯

三 特殊權限 SUID SGID STICKY

S了解為set,

SetUID(針對二進制檔案!)(chmod 4XXX chmod u+s)

當其他使用者執行該二進制檔案時暫時擁有其所有者的權限,比如/bin/ls這個檔案

SetGID(檔案+目錄)(chmod 2XXX chmod g+s)

對于二進制檔案

要求其他權限至少為x,為了安全性,沒有執行權限當然不讓你其他了

使用者執行該二進制檔案時暫時擁有使用者組的權限

對于目錄

要求使用者至少能進去目錄,是以至少為rx權限,此時使用者暫時屬于目錄的本使用者組,比如建立一個檔案,,其使用者組認為原來的目錄使用者組

Sticky Bit(一般隻對目錄設定)(chmod 1XXX chmod o+t)

在一個目錄上設了sticky位後,所有的使用者都可以在這個目錄下建立檔案,但隻能删除自己建立的檔案(root除外),這就對所有使用者能寫的目錄下的使用者檔案啟到了保護的作用。

繼續閱讀