Think before you speak, read before you think.

chattr lsattr命令

chattr与lsattr可以修改文件属性,是比chmod, chown的属性更底层的属性。

chattr可以结合 -减  +增  = 指定 属性

查看属性,默认只有一个e属性

#lsattr /etc/hosts
-------------e- /etc/hosts

添加i属性,则这个文件将不能修改,删除,重命名,追加,也不能创建硬链接。root也只能在回收这个属性值之后才能正常使用。

# chattr +i /etc/hosts
# lsattr /etc/hosts
----i--------e- /etc/hosts
rm /etc/hosts会出现Operation not permitted

添加a属性,使文件只能被追加,不能被删除,常用于日志文件,比如说在logrotate的时候对日志文件先-a,然后再+a。如nginx的logrotate

# This configuration is from jpuyy 2013-12-16
 /web/nginx/logs/access.log {
 weekly
 rotate 5
 compress
 sharedscripts
 prerotate
 /usr/bin/chattr -a /web/nginx/logs/access.log
 endscript
 sharedscripts
 postrotate
 /usr/bin/killall -HUP nginx
 /usr/bin/chattr +a /web/nginx/logs/access.log
 endscript
 }

chattr -R 递规更改属性


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *