linux无法写入链接文件夹,Linux - chattr & lsattr

通过命令 chattr,可以设置文件/文件夹的隐藏属性,来保证文件/文件夹的安全.其中比较重要的参数为i和a.这两个属性只有root用户才可以设置或清除.而通过命令 lsattr 可以查看这些属性.

i -- immutable

a -- append only

1. 文件的隐藏属性i测试

通过使用参数i,可以保证文件不被删除,重命名,硬链接,也不能被修改.就象把文件"冻"在了原地.下面是一些测试:

1.1 设置文件 test.txt 的隐藏属性

[root@localhost yuechaotian]# pwd

/home/yuechaotian

[root@localhost yuechaotian]# ll

总用量 8

drwx--x--x  3 root        root        4096 12月  7 14:52 study

dr-xr-xr-x  2 yuechaotian yuechaotian 4096 12月  9 22:33 test

-rw-rw-rw-  1 yuechaotian yuechaotian    0 12月  9 22:33 test.txt

[root@localhost yuechaotian]# lsattr test.txt

------------- test.txt

[root@localhost yuechaotian]# chattr +i test.txt

[root@localhost yuechaotian]# lsattr test.txt

----i-------- test.txt

1.2 test.txt 不能被删除,重命名,硬链接

[root@localhost yuechaotian]# rm test.txt

rm:是否删除有写保护的一般空文件‘test.txt’? y

rm: 无法删除‘test.txt’: 不允许的操作

[root@localhost yuechaotian]# mv test.txt test.txt.copy

mv: 无法移动‘test.txt’至‘test.txt.copy’: 不允许的操作

[root@localhost yuechaotian]# ln test.txt test.txt.ln

ln: 正在创建连至‘test.txt’的硬链接‘test.txt.ln’: 不允许的操作

1.3 可以被拷贝,新生成的文件并不具有该属性

[root@localhost yuechaotian]# cp test.txt test.txt.copy

[root@localhost yuechaotian]# ll

总用量 16

drwx--x--x  3 root        root        4096 12月  7 14:52 study

dr-xr-xr-x  2 yuechaotian yuechaotian 4096 12月  9 22:33 test

-rw-rw-rw-  1 yuechaotian yuechaotian   29 12月 10 20:27 test.txt

-rw-r--r--  1 root        root          29 12月 10 20:30 test.txt.copy

[root@localhost yuechaotian]# lsattr

----i-------- ./test.txt

------------- ./test

------------- ./study

------------- ./test.txt.copy

1.4 可以被符号链接

[root@localhost yuechaotian]# ln -s test.txt test.txt.ln

[root@localhost yuechaotian]# ll

总用量 16

drwx--x--x  3 root        root        4096 12月  7 14:52 study

dr-xr-xr-x  2 yuechaotian yuechaotian 4096 12月  9 22:33 test

-rw-rw-rw-  1 yuechaotian yuechaotian   29 12月 10 20:27 test.txt

-rw-r--r--  1 root        root          29 12月 10 20:30 test.txt.copy

lrwxrwxrwx  1 root        root           8 12月 10 20:32 test.txt.ln -> test.txt

1.5 当使用vi编辑文件 test.txt,并试图保存时,就会出现如下提示:

E45: 'readonly' option is set (add ! to override)

2. 文件的隐藏属性a测试

a属性与i属性的区别就是:此时文件是可以增加数据的(但不可被删除)

2.1 设置文件 test.txt 的隐藏属性

[root@localhost yuechaotian]# rm test.txt.copy

rm:是否删除一般文件‘test.txt.copy’? y

[root@localhost yuechaotian]# rm test.txt.ln

rm:是否删除符号链接‘test.txt.ln’? y

[root@localhost yuechaotian]# chattr -i +a test.txt

[root@localhost yuechaotian]# lsattr test.txt

-----a------- test.txt

2.2 与 i 参数不同的是,此时文件 test.txt 是可以写入的:

[root@localhost yuechaotian]# cat test.txt

http://yuechaotian.cublog.cn

[root@localhost yuechaotian]# echo>>test.txt adsf

[root@localhost yuechaotian]# cat test.txt

http://yuechaotian.cublog.cn

adsf

2.3 同样地,具有隐藏属性a的文件,不能被删除,重命名,以及硬链接

[root@localhost yuechaotian]# rm test.txt

rm:是否删除一般文件‘test.txt’? y

rm: 无法删除‘test.txt’: 不允许的操作

[root@localhost yuechaotian]# mv test.txt test.cp

mv: 无法移动‘test.txt’至‘test.cp’: 不允许的操作

[root@localhost yuechaotian]# ln test.txt test.ln

ln: 正在创建连至‘test.txt’的硬链接‘test.ln’: 不允许的操作

2.4 而当试图使用 vi 编辑它时,会出现如下提示:

"test.txt" E212: 不能以写入模式打开

在日常工作中,对于一些不允许修改的配置文件,可以设置其隐藏属性为i;对于日志文件,可以设置其隐藏属性为a.

3. 文件夹的i与a

一样的道理,文件夹中的文件相当于该文件夹的"数据".当文件夹被设置为i或a属性后,该文件夹的"数据"也具有了如上测试中的约束.

3.1 当文件夹具有隐藏属性i时,该文件夹不能被删除,重命名,不能向该文件夹内增加文件,该文件夹的文件也不能被删除.

[root@localhost yuechaotian]# ll

总用量 12

drwx--x--x  3 root        root        4096 12月  7 14:52 study

drwxrwxrwx  2 yuechaotian yuechaotian 4096 12月 10 21:47 test

-rw-rw-rw-  1 yuechaotian yuechaotian   34 12月 10 20:48 test.txt

[root@localhost yuechaotian]# chattr +i test

[root@localhost yuechaotian]# rm -r test

rm:是否进入有写保护的目录‘test’? y

rm:是否删除一般文件‘test/t.2’? y

rm: 无法删除‘test/t.2’: 权限不够

[root@localhost yuechaotian]# mv test t

mv: 无法移动‘test’至‘t’: 不允许的操作

[root@localhost yuechaotian]# mv test.txt test

mv: 无法移动‘test.txt’至‘test/test.txt’: 权限不够

[root@localhost yuechaotian]# rm test/t.2

rm:是否删除一般文件‘test/t.2’? y

rm: 无法删除‘test/t.2’: 权限不够

3.2 当文件夹具有隐藏属性a时,与i不同的是:可以向该文件夹内增加文件

[root@localhost yuechaotian]# chattr -i +a test

[root@localhost yuechaotian]# mv test.txt test

[root@localhost yuechaotian]# rm test/test.txt

rm:是否删除一般文件‘test/test.txt’? y

rm: 无法删除‘test/test.txt’: 不允许的操作

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值