Assign Special attributes to a file using "chattr".
If you want to restrict all the operation on a file except readonly including root user, chattr can help to achieve this.
[root@localhost rpm]# touch /tmp/my.txt
[root@localhost rpm]# ll /tmp/my.txt
-rw-r--r--. 1 root root 0 Jan 25 15:36 /tmp/my.txt
[root@localhost rpm]# chgrp harry /tmp/my.txt
[root@localhost rpm]# chown natasha /tmp/my.txt
[root@localhost rpm]# ll /tmp/my.txt
-rw-r--r--. 1 natasha harry 0 Jan 25 15:36 /tmp/my.txt
[root@localhost rpm]# chmod 000 /tmp/my.txt
[root@localhost rpm]# ll /tmp/my.txt
----------. 1 natasha harry 0 Jan 25 15:36 /tmp/my.txt
[root@localhost rpm]#
If you want to restrict all the operation on a file except readonly including root user, chattr can help to achieve this.
[root@localhost rpm]# touch /tmp/my.txt
[root@localhost rpm]# ll /tmp/my.txt
-rw-r--r--. 1 root root 0 Jan 25 15:36 /tmp/my.txt
[root@localhost rpm]# chgrp harry /tmp/my.txt
[root@localhost rpm]# chown natasha /tmp/my.txt
[root@localhost rpm]# ll /tmp/my.txt
-rw-r--r--. 1 natasha harry 0 Jan 25 15:36 /tmp/my.txt
[root@localhost rpm]# chmod 000 /tmp/my.txt
[root@localhost rpm]# ll /tmp/my.txt
----------. 1 natasha harry 0 Jan 25 15:36 /tmp/my.txt
[root@localhost rpm]#
root user doesn't have any permission on /tmp/my.txt file, still it can do all the file operation on this file.
[root@localhost rpm]# cat >> /tmp/my.txt
root user added some data
[root@localhost rpm]# cat /tmp/my.txt
root user added some data
[root@localhost rpm]#
How to restrict root user so that any user can't perform any operation?
[root@localhost rpm]# chattr +i /tmp/my.txt
[root@localhost rpm]# lsattr /tmp/my.txt
----i----------- /tmp/my.txt
[root@localhost rpm]#
Now if any user including root try to do any file operation except read, they can't do.
[root@localhost rpm]# cat >> /tmp/my.txt
-bash: /tmp/my.txt: Permission denied
[root@localhost rpm]#
[root@localhost rpm]# rm -rvf /tmp/my.txt
rm: cannot remove ‘/tmp/my.txt’: Operation not permitted
[root@localhost rpm]#
[root@localhost rpm]# chattr -i /tmp/my.txt ---------- if want to remove
chattr has lots of option but i and a are my favorite.
a -> We can append some data but can't remove anything.
[root@localhost rpm]# chattr +a /tmp/my.txt
[root@localhost rpm]# cat > /tmp/my.txt
-bash: /tmp/my.txt: Operation not permitted
[root@localhost rpm]# cat >> /tmp/my.txt
adding few more lines
[root@localhost rpm]# cat /tmp/my.txt
root user added some data
adding few more lines
[root@localhost rpm]#
Comments
Post a Comment
Please share your experience.....