0 背景知识
系统命令 | 作用 |
---|---|
ls | list, 列出目录和文件信息 |
rm | 删除文件/目录 |
whoami | 打印有效用户ID |
chattr/lsattr | 改变(change)/列出(list)文件/目录的属性 |
1 标准玩法
不知道各位有遇到过rm命令删除不了文件的情况,反正我……遇到过,嗯。
比如这样,
root@icymoon_dev> rm test_file
rm: remove regular empty file ‘test_file’? y
rm: cannot remove ‘test_file’: Operation not permitted
root@icymoon_dev> rm -f ./test_file
rm: cannot remove ‘./test_file’: Operation not permitted
root@icymoon_dev> whoami
root
root@icymoon_dev> ls -l
total 4
-rw-r--r-- 1 root root 0 Sep 5 22:51 test_file
root@icymoon_dev>
这是什么?我是root啊,我有写权限啊,还有我不能干的事情?别急,往下看看这个,
File systems use permissions and attributes to regulate the level of interaction that system processes can have with files and directories. [1]
Hmmm~~~~~,咱们来看看,
root@icymoon_dev> lsattr ./test_file
----i--------e-- ./test_file
“i” 这个attribute是“ immutable”,翻译过来是“一成不变”。真的是什么都不能变吗?咱们试试:
root@icymoon_dev> chmod +x ./test_file
chmod: changing permissions of ‘./test_file’: Operation not permitted
root@icymoon_dev> echo > ./test_file
-bash: ./test_file: Permission denied
root@icymoon_dev> mv ./test_file ./file_test
mv: cannot move ‘./test_file’ to ‘./file_test’: Operation not permitted
root@icymoon_dev>
看起来是真的了,肿么办呢?
root@icymoon_dev> chattr -i ./test_file
root@icymoon_dev> lsattr ./test_file
-------------e-- ./test_file
root@icymoon_dev> rm ./test_file
rm: remove regular empty file ‘./test_file’? y
root@icymoon_dev> ls ./test_file
ls: cannot access ./test_file: No such file or directory
root@icymoon_dev>
就这样子,简单吧,嘿嘿
2 其他玩法,负作用大,慎用哈
- 只读挂载整个分区;
- 替换掉rm等命令;
- 改内核相关的系统调用;
[1] https://wiki.archlinux.org/index.php/File_permissions_and_attributes