[文件锁]对root同样有用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
1.给文件/目录 添加属性 chattr +i [文件/目录] chattr +a [文件/目录] chattr -i [文件/目录] chattr -a [文件/目录] 选项: +: 增加权限 -: 删除权限 =: 等于某权限 i: [文件]如果对文件设置i属性,那么不允许对文件进行删除,改名,也不能添加和修改数据; [目录]如果对目录设置i属性,那么只能修改目录下的文件数据,但不允许建立和删除文件 a: [文件]如果对文件设置a属性,那么只能在文件中增加数据,不能删除也不能修改数据, 只能用echo增加数据 不能用vim追加因为他不知道你删了还是加了所以不会让你写入 [目录]如果对目录中设置了a属性,那么只允许在目录中建立和修改文件不允许删除 只能用echo增加数据 不能用vim追加因为他不知道你删了还是加了所以不会让你写入 e:[仅代表文件系统,没啥其他含义] linux中绝大多数文件都默认拥有e属性。表示该文件是使用ext文件系统进行存储的,而且不能使用 "chattr -e"命令取消e属性 |
[查看文件系统属性]
1 2 |
lsattr [文件名] #查看该文件是否被加锁什么的 lsarrt -d [目录名] #查看目录权限,查目录一定要加 -d -d -d |
例:[对文件设置i属性]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[root@localhost ~]# lsattr Dockerfile #查看当前文件权限e -------------e-- Dockerfile [root@localhost ~]# chattr +i Dockerfile #给文件添加i属性 [root@localhost ~]# lsattr Dockerfile #查看添加i属性后的文件权限 ----i--------e-- Dockerfile [root@localhost ~]# rm -rf Dockerfile #删除文件 rm: cannot remove ‘Dockerfile’: Operation not permitted #删除失败 [root@localhost ~]# mv Dockerfile Dockerfile_back #给文件改名 mv: cannot move ‘Dockerfile’ to ‘Dockerfile_back’: Operation not permitted #失败 [root@localhost ~]# echo '我来啦我来啦' >> Dockerfile # 追加 修改文件 失败 -bash: Dockerfile: Permission denied [root@localhost ~]# echo '我来啦我来啦' > Dockerfile #直接输出到文件 失败 -bash: Dockerfile: Permission denied [root@localhost ~]# |
例:[给文件夹设置i属性]
1 2 3 4 5 6 7 8 9 |
[root@localhost ~]# chattr -i Dockerfile [root@localhost ~]# lsattr -d /root/tmp ----i--------e-- /root/tmp [root@localhost tmp]# touch text.txt #在目录下新建文件 touch: cannot touch ‘text.txt’: Permission denied #新建失败 [root@localhost tmp]# rm -rf default.conf #在目录下删除文件 rm: cannot remove ‘default.conf’: Permission denied #删除失败 [root@localhost tmp]# echo '123123' >> default.conf #修改文件成功 |
例:[给文件设置a属性]
1 2 3 4 5 6 7 8 9 10 |
[root@localhost ~]# chattr +a Dockerfile [root@localhost ~]# lsattr Dockerfile -----a-------e-- Dockerfile [root@localhost ~]# echo '我来修改啦'>> Dockerfile #修改成功 [root@localhost ~]# rm -rf Dockerfile #删除失败 rm: cannot remove ‘Dockerfile’: Operation not permitted [root@localhost ~]# echo '我来修改啦'> Dockerfile #修改失败 -bash: Dockerfile: Operation not permitted [root@localhost ~]# |
例:[给文件夹设置a属性]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[root@localhost ~]# chattr +a tmp [root@localhost ~]# lsattr -d tmp -----a-------e-- tmp [root@localhost ~]# cd tmp/ [root@localhost tmp]# touch index.html #新建文件成功 [root@localhost tmp]# echo '写入文件内容'> index.html #写入文件成功 [root@localhost tmp]# echo '修改内容' >> index.html #修改文件成功 [root@localhost tmp]# rm -rf index.html #删除文件失败 rm: cannot remove ‘index.html’: Operation not permitted [root@localhost tmp]# cp index.html /root/ #拷贝文件成功 [root@localhost tmp]# mv index.html /root/ #移动文件失败 mv: overwrite ‘/root/index.html’? y mv: cannot move ‘index.html’ to ‘/root/index.html’: Operation not permitted [root@localhost tmp]# |
一般 chattr -a 对于文件目录用的多一些,可以新建修改 但是不允许用户删除,这个非常好。
- 本文固定链接: https://www.yoyoask.com/?p=240
- 转载请注明: shooter 于 SHOOTER 发表