Linux的chattr與lsattr命令介紹

2021-09-21 04:43:41 字數 3795 閱讀 8053

linux的chattr與lsattr命令介紹

命令lsattr是用來檢視檔案、目錄屬性的,命令chattr是用來對其做更改的。與chmod這個命令相比,chmod只是改變檔案的讀寫、執行許可權,更底層的屬性控制是由chattr來改變的。

chattr命令語法:

chattr [+-=][asaci][檔案或者目錄名]

注:+表示增加,-表示減少,=表示設定。

注:實際工作中運用的最多的就是兩個引數:i和a

舉例說明:lsattr

[root@jkkhj ~]# lsattr

-------------e- ./wu.txt

-------------e- ./install.log.syslog

-------------e- ./anaconda-ks.cfg

-------------e- ./install.log

注:e表示ext4或ext3所持人的許可權。

[root@jkkhj ~]# chattr +a wu.txt

[root@jkkhj ~]# lsattr

-----a-------e- ./wu.txt

-------------e- ./install.log.syslog

-------------e- ./anaconda-ks.cfg

-------------e- ./install.log

[root@jkkhj ~]# echo '111111' >> wu.txt

[root@jkkhj ~]# cat wu.txt

111111

[root@jkkhj ~]# rm -rf wu.txt

rm: cannot remove `wu.txt': operation not permitted

[root@jkkhj ~]# echo '2222' > wu.txt

-bash: wu.txt: operation not permitted

上述中:wu.txt檔案增加a許可權後,只可以追加,不能重定向及刪除!

[root@jkkhj ~]# mkdir shiyan

[root@jkkhj ~]# lsattr

-----a-------e- ./wu.txt

-------------e- ./install.log.syslog

-------------e- ./anaconda-ks.cfg

-------------e- ./install.log

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

[root@jkkhj ~]# chattr +a shiyan

[root@jkkhj ~]# lsattr

-----a-------e- ./wu.txt

-------------e- ./install.log.syslog

-------------e- ./anaconda-ks.cfg

-------------e- ./install.log

-----a-------e- ./shiyan

[root@jkkhj ~]# touch shiyan/test1

[root@jkkhj ~]# cd shiyan

[root@jkkhj shiyan]# lsattr

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

[root@jkkhj shiyan]# rm -rf test1

rm: cannot remove `test1': operation not permitted

上述中:shiyan目錄增加a許可權後,只可以在裡面建立檔案,而不能刪除檔案!

[root@jkkhj ~]# chattr -a shiyan

[root@jkkhj ~]# chattr -a wu.txt

[root@jkkhj ~]# ls

anaconda-ks.cfg  install.log  install.log.syslog  shiyan  wu.txt

[root@jkkhj ~]# lsattr

-------------e- ./wu.txt

-------------e- ./install.log.syslog

-------------e- ./anaconda-ks.cfg

-------------e- ./install.log

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

[root@jkkhj ~]# chattr +i wu.txt

[root@jkkhj ~]# lsattr wu.txt

----i--------e- wu.txt

[root@jkkhj ~]# echo 'nihao' >> wu.txt

-bash: wu.txt: permission denied

[root@jkkhj ~]# echo 'nihao' > wu.txt

-bash: wu.txt: permission denied

[root@jkkhj ~]# rm -rf wu.txt

rm: cannot remove `wu.txt': operation not permitted

上述中:給wu.txt增加i許可權之後,檔案即使在root賬戶下也不能進行追加、重定向、刪除等操作!

[root@jkkhj ~]# lsattr

----i--------e- ./wu.txt

-------------e- ./install.log.syslog

-------------e- ./anaconda-ks.cfg

-------------e- ./install.log

----i--------e- ./shiyan

[root@jkkhj ~]# touch shiyan/test2

touch: cannot touch `shiyan/test2': permission denied

[root@jkkhj ~]# rm -rf shiyan

rm: cannot remove `shiyan/test1': permission denied

上述中:給shiyan目錄增加i許可權之後,即使在root賬戶下也不能在shiyan目錄下建立檔案test1及刪除shiyan這個目錄等操作!

[root@jkkhj ~]# chattr -i shiyan

[root@jkkhj ~]# chattr -i wu.txt

[root@jkkhj ~]# lsattr

-------------e- ./wu.txt

-------------e- ./install.log.syslog

-------------e- ./anaconda-ks.cfg

-------------e- ./install.log

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

[root@jkkhj ~]# rm -rf shiyan;rm -rf wu.txt

[root@jkkhj ~]# lsattr

-------------e- ./install.log.syslog

-------------e- ./anaconda-ks.cfg

-------------e- ./install.log

Linux的chattr與lsattr命令詳解

ps 有時候你發現用root許可權都不能修改某個檔案,大部分原因是曾經用chattr命令鎖定該檔案了。chattr命令的作用很大,其中一些功能是由linux核心版本來支援的,不過現在生產絕大部分跑的linux系統都是2.6以上核心了。通過chattr命令修改屬性能夠提高系統的安全性,但是它並不適合所...

Linux的chattr與lsattr命令詳解

有時候你發現用root許可權都不能修改某個檔案,大部分原因是曾經用chatt命令鎖定該檔案了。chattr命令的作用很大,其中一些功能是由linux核心版本來支援的,不過現在生產絕大部分跑的linux系統都是2.6以上核心了。通過chattr命令修改屬性能夠提高系統的安全性,但是它並不適合所有的目錄...

Linux的chattr與lsattr命令詳解

有時候你發現用root許可權都不能修改某個檔案,大部分原因是曾經用chattr命令鎖定該檔案了。chattr命令的作用很大,其中一些功能是由linux核心版本來支援的,不過現在生產絕大部分跑的linux系統都是2.6以上核心了。通過chattr命令修改屬性能夠提高系統的安全性,但是它並不適合所有的目...