目录和文件的特殊权限:chattr和lsattr

常见需求:

需求1:严格限制,限制后不可以创建,不可以删除,只能编辑

例如:我有两个文件夹,/root/a/1和/root/a/2和一个文件/root/a/1.txt
根据需求分析,我们只需要给a文件夹增加i权限即可(如果想要可以创建和删除可以减掉i权限)
[root@localhost ~]# ls
anaconda-ks.cfg install.log install.log.syslog
[root@localhost ~]# mkdir a
[root@localhost ~]# ls
a anaconda-ks.cfg install.log install.log.syslog
[root@localhost ~]# mkdir a/1 a/2
[root@localhost ~]# touch a/1.txt
[root@localhost ~]# tree a
a
|-- 1
|-- 1.txt
`-- 2

2 directories, 1 file
[root@localhost ~]# lsattr //查看特殊权限情况
-------------e- ./a
-------------e- ./install.log.syslog
-------------e- ./install.log
-------------e- ./anaconda-ks.cfg
[root@localhost ~]# chattr +i a //给a目录增加特殊权限
[root@localhost ~]# lsattr
----i--------e- ./a
-------------e- ./install.log.syslog
-------------e- ./install.log
-------------e- ./anaconda-ks.cfg
[root@localhost ~]# mkdir a/3
mkdir: cannot create directory `a/3’: Permission denied //不可以创建目录

[root@localhost ~]# touch a/2.txt
touch: cannot touch `a/2.txt’: Permission denied //不可以创建文件

[root@localhost ~]# rm -rf a/1
rm: cannot remove `a/1’: Permission denied //不可以删除目录

[root@localhost ~]# rm -rf a/1.txt
rm: cannot remove `a/1.txt’: Permission denied //不可以删除文件
[root@localhost ~]# vi a/1
1/ 1.txt
[root@localhost ~]# vi a/1
1/ 1.txt
[root@localhost ~]# vi a/1.txt //可以编辑文件
[root@localhost ~]# cat a/1.txt
fafdasfdsafdsafdsafdsavca4532543
[root@localhost ~]#

需求2:限制当前已有的子级不可创建,不可删除,只能编辑。不会限制日后创建的新子级

例如:我有两个文件夹,/root/b/1和/root/b/2和一个文件/root/b/2/1.txt
根据需求分析,我们只需要给b文件夹的子级增加i权限即可(如果想要可以创建和删除可以减掉i权限)
[root@localhost ~]# ls
a anaconda-ks.cfg install.log install.log.syslog
[root@localhost ~]# mkdir -p b/1 b/2
[root@localhost ~]# touch b/2/1.txt
[root@localhost ~]# tree b
b
|-- 1
-- 2– 1.txt

2 directories, 1 file
[root@localhost ~]# chattr +i b/* //给b目录下的文件和目录增加i权限
[root@localhost ~]# lsattr
----i--------e- ./a
-------------e- ./b //b目录并没有增加i权限,只是它的子级都增加了i权限
-------------e- ./install.log.syslog
-------------e- ./install.log
-------------e- ./anaconda-ks.cfg
[root@localhost ~]# lsattr b //看到了没b的子级都有i权限
----i--------e- b/2
----i--------e- b/1
[root@localhost ~]# mkdir b/1/1
mkdir: cannot create directory `b/1/1’: Permission denied //不可创建目录

[root@localhost ~]# touch b/1/1.txt
touch: cannot touch `b/1/1.txt’: Permission denied //不可创建文件

[root@localhost ~]# rm -rf b/1
rm: cannot remove `b/1’: Operation not permitted //不可删除目录

[root@localhost ~]# rm -rf b/2/1.txt
rm: cannot remove `b/2/1.txt’: Permission denied //不可删除文件
[root@localhost ~]# vi b/2/1.txt //可以编辑文件
[root@localhost ~]# cat b/2/1.txt
iiifadfadsfdasfdsafd
[root@localhost ~]# mkdir b/3
[root@localhost ~]# touch b/3.txt
[root@localhost ~]# ls b
1 2 3 3.txt
[root@localhost ~]# lsattr b //发现没有,后来创建的b的新儿子都没有i权限(新儿子都可以操作)
----i--------e- b/2
-------------e- b/3.txt
----i--------e- b/1
-------------e- b/3
[root@localhost ~]# rm -rf b/3
[root@localhost ~]# rm -rf b/3.txt
[root@localhost ~]# ls
a anaconda-ks.cfg b install.log install.log.syslog
[root@localhost ~]# ls b
1 2
[root@localhost ~]#

需求3:只限制删除

根据需求分析,我们只需要给我相应的文件增加a权限即可实现只限制删除

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值