linux中文件系统属性chattr权限,


一.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: 若目标是目录,仅列出目录本身的属性,而不是子文件

相关内容

    暂无相关文章