5

Linux基础知识之文件隐藏属性

 3 years ago
source link: https://zhang.ge/1017.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Linux基础知识之文件隐藏属性 | 张戈博客
Jager · 1月14日 · 2014年linux基础知识 · linux隐藏属性 4646次已读

这几天温习下Linux基础知识,基础里面较为薄弱的知识点我会在这里记录一下,以便回头查看


Linux下的文件除了权限属性,还有一些隐藏属性,必须使用lsattr来显示,如下所示:

[root@localhost ~]# lsattr test
------------- test

结果中第一列是13个短横杆,其中每一个横杆都是一个属性,如果当前位置上设置了该属性就会显示相对应的字符。

如果要设置文件的隐藏属性,需要使用chattr命令。这里介绍几个常用的隐藏属性,第一种是a属性。拥有这种属性的文件只能在尾部增加数据而不能被删除。个人觉得用于重要的日志类文件非常不错,即可以继续追加内容,又可以避免被恶意删除。

下面实例使用chattr来给该文件添加a属性并测试:

[root@localhost ~]# ll test
-rwxrwxrwx 1 root root 187 Dec  5 07:55 test
[root@localhost ~]# chattr +a test
[root@localhost ~]# lsattr test
-----a------- test
[root@localhost ~]# rm -rf test
rm: cannot remove `test': Operation not permitted
[root@localhost ~]#

如上所示,设置了a属性的文件(夹),即便权限是777,即便是root用户都不能删除它,但是实际上可以以尾部新增(append)的方式继续像该文件中写入内容:

[root@localhost ~]# lsattr test
-----a------- test
[root@localhost ~]# cat test
first line
[root@localhost ~]# >test
-bash: test: Operation not permitted
[root@localhost ~]# echo second line >>test
[root@localhost ~]# cat test
first line
second line
[root@localhost ~]#

还有一种比较常见的属性是i属性。设置了这种属性的文件将无法写入、改名、删除,即使是root用户也不行。这种属性常用于设置在系统或者关键服务中的配置文件,这对提升系统安全性有很大帮助。

更多隐藏属性请使用man chattr查看。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK