I ran the following commands:
chown root:root file.php
chmod 0644 file.php
Settings appear to have been applied when typing ls -l
:
-rw-r--r-- 1 root root 310 Jul 26 01:25 file.php
However, the original user can still edit the file via Cpanel File Manager and upon saving, the file ownership went back to the original user. E.g.,
-rw-r--r-- 1 orig_user orig_user 310 Jul 26 01:25 file.php
How do I prevent the user from editing files already owned by root?
2
Answers
Use
chattr
on the file to make it immutable.If the editor uses “erase old file + write new file” instead of “modify existing file”, this works perfect. If so, the permissions on the containing directory must be changed to prevent a user from erasing the file.
Removing a file did not depend on the permission of the file!
Try it out in a shell:
but the file will be erased!
As this
chown
andchmod
did not help for the file, but for the containing directory.On filesystems like ext2, ext3 and most common newer ones you can use
chattr
to protect with more attributes.From the
chattr
man page: