一.chattr命令格式 格式 chattr [+ =][選項] 文件或目錄名 操作 選項 實例 二.查看文件系統屬性 格式 lsattr 選項 文件名 選項 ...
一.chattr命令格式
格式
chattr [+-=][選項] 文件或目錄名
操作
+: 增加許可權
-: 刪除許可權
=: 等於某許可權
選項
i:
如果對文件設置i屬性,那麼不允許對文件進行刪除/改名,也不能添加和修改數據;(相當於鎖死文件,什麼操作都不允許進行,對root用戶也生效)
如果對目錄設置i屬性,那麼只能修改目錄下文件的數據,但不允許建立和刪除文件.
a:
如果對文件設置a屬性,那麼只能在文件中增加數據,但是不能刪除也不能修改數據;(相當於鎖死文件中現有的數據,只能添加新的數據[不能使用vi或vim,只能使用>>],其他的操作都不允許進行,對root生效)
如果對目錄設置a屬性,那麼只允許在目錄中建立和修改文件,但是不允許刪除.
實例
## 文件添加i屬性
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# touch test
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# echo 111 >> test
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# cat test
111
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# chattr +i test
# i代表i屬性,e代表文件是在ext文件系統下建立的
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# lsattr -a test
----i--------e-- test
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# echo 222 >> test
-bash: test: Permission denied
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# rm -rf test
rm: cannot remove ‘test’: Operation not permitted
## 文件夾添加i屬性
# 創建文件夾test
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# mkdir test
# 在test文件夾下新建文件aaa
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# touch test/aaa
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls test
aaa
# 給文件夾test添加i屬性
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# chattr +i test
# test文件夾有i屬性,aaa沒有i屬性
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# lsattr -a test
----------I--e-- test/..
----i--------e-- test/.
-------------e-- test/aaa
# 文件aaa可以編輯
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# echo 111 >> aaa
# 在test文件夾下不可以新建文件
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# touch test/abc
touch: cannot touch ‘test/abc’: Permission denied
# 在刪除test文件夾下的aaa沒有許可權
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# rm -rf test/aaa
rm: cannot remove ‘test/aaa’: Permission denied
## 文件添加a屬性
二.查看文件系統屬性
格式
lsattr 選項 文件名
選項
-a: 顯示所有文件和目錄
-d: 若目標是目錄,僅列出目錄本身的屬性,而不是子文件