chmod移除写权限并不会对root用户生效,如果想防止文件或文件夹被意外修改,可以使用chattr命令
chattr +i 文件/目录
可以对文件和文件夹加保护,之后任何用户都无法修改和删除
root@localhost:~ # mkdir /opt/test111
root@localhost:~ #
root@localhost:~ # chattr +i /opt/test111
root@localhost:~ # touch /opt/test111/foo.txt
touch: cannot touch ‘/opt/test111/foo.txt’: Permission denied
root@localhost:~ # mkdir /opt/test111/foo
mkdir: cannot create directory ‘/opt/test111/foo’: Permission denied
root@localhost:~ # rm -rf /opt/test111/
rm: cannot remove ‘/opt/test111/’: Operation not permitted
root@localhost:~ #
root@localhost:~ # chattr -i /opt/test111/
root@localhost:~ # touch /opt/test111/foo.txt
root@localhost:~ # mkdir /opt/test111/foo
root@localhost:~ # rm -rf /opt/test111/
参考:https://www.runoob.com/linux/linux-comm-chattr.html