这几天温习 Linux 基础知识,把比较薄弱的点记在这里,方便回头查阅。
查看隐藏属性:lsattr
Linux 下的文件除了 rwx 权限之外,还有一些隐藏属性,必须用 lsattr 才能看到:
1
2
| [root@localhost ~]# lsattr test
------------- test
|
第一列是 13 个短横杆,每一位代表一个属性;某一位被设置后,就会显示对应的字符。
要设置这些隐藏属性,用的是 chattr 命令。下面介绍两个最常用的。
a 属性:只能追加,不能删除
带 a 属性的文件只能在尾部追加数据,不能被删除。用来保护重要的日志类文件很合适——既能继续写入,又能防止被恶意删除。
1
2
3
4
5
6
7
| [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
|
可以看到,即使权限是 777、即使是 root 用户都删不掉它。但仍然可以用追加的方式继续写入:
1
2
3
4
5
6
7
8
9
10
| [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
|
注意 >test 这种覆盖写入会被拒绝,只有 >> 追加才允许。
i 属性:完全锁定
带 i 属性的文件无法写入、改名、删除,root 用户也不行。这种属性常用于保护系统或关键服务的配置文件,对提升系统安全性很有帮助。
小结
- 查看:
lsattr 文件名 - 设置/取消:
chattr +a、chattr -a(i 同理) - a:只能追加,防误删,适合日志
- i:完全锁定,适合关键配置
更多隐藏属性请用 man chattr 查看。