chattr

功能说明:chattr命令用于改变文件的扩展属性。与chmod这个命令相比,chmod只是改变文件的读、写、执行权限,更底层的属性控制是由chattr来改变。
 
语法格式:
chattr [选项] [模式] [<文件或目录>]
 
参数选项:
-R 递归更改目录属性。
-V 显示命令执行过程。
 
mode:
+ 增加参数。
- 移除参数。
= 更新为指定参数。
A 不要修改文件的最后访问时间。
a 只能向文件中添加数据,而不能删除,多用于服务器日志文件安全。
i 设定文件不能被删除、改名、写入或新增内容。
 
范例:设置只能往文件里追加内容,但不能删除文件。
[root@VM_0_15_centos ~]# touch test
[root@VM_0_15_centos ~]# lsattr test
-------------e-- test
[root@VM_0_15_centos ~]# chattr +a test
[root@VM_0_15_centos ~]# lsattr test
-----a-------e-- test
[root@VM_0_15_centos ~]# rm -rf test    # 即使是root用户也不能删除文件
rm: cannot remove ‘test’: Operation not permitted
[root@VM_0_15_centos ~]# echo "111" > test
-bash: test: Operation not permitted
[root@VM_0_15_centos ~]# echo 111 > test    # 不能清空文件内容
-bash: test: Operation not permitted
[root@VM_0_15_centos ~]# cat test
[root@VM_0_15_centos ~]# echo "222" >> test   # 可以追加文件
[root@VM_0_15_centos ~]# cat test
222


范例:给文件加锁,使其只能是只读
[root@VM_0_15_centos ~]# touch file.txt
[root@VM_0_15_centos ~]# chattr +i file.txt
[root@VM_0_15_centos ~]# lsattr file.txt
----i--------e-- file.txt
[root@VM_0_15_centos ~]# rm file.txt      # 即使是root用户也不能删除文件
rm: remove regular empty filefile.txt’? y
rm: cannot remove ‘file.txt’: Operation not permitted
[root@VM_0_15_centos ~]# rm -rf file      # 其实没有删除
[root@VM_0_15_centos ~]# ls -lh file.txt
-rw-r--r-- 1 root root 0 Jun 23 18:01 file.txt
[root@VM_0_15_centos ~]# echo 111 >> file.txt  # 不能追加文件
-bash: file.txt: Permission denied
[root@VM_0_15_centos ~]# echo 111 > file.txt    # 不能清空文件内容
-bash: file.txt: Permission denied
[root@VM_0_15_centos ~]# chattr -i file.txt 
[root@VM_0_15_centos ~]# rm -rf file.txt 

 

posted @ 2018-08-02 23:02  屠魔的少年  阅读(340)  评论(0)    收藏  举报