Now in this article I will show you steps to prevent or restrict access of root user to access certain files or directories. Now by default root is the super user who has access to all the files and directories available on the Linux node but it is also possible to restrict even a root user from accessing and modifying the content of a file or directory.

You can restrict root user from accessing and modifying a file or directory using extended file attributes. We will be dealing with chattr and lsattr to achieve this in our demonstration.
Now chattr - change file attributes on a Linux file system,
supports multiple options but we will be concentrating only on the
options which can help restrict root user access on certain files and
directories.

To get the complete list of options supported with chattr you can view the man page of chattr using below command

# man chattr

We will work with two attributes
a:

  • Append text to a file
  • Can’t overwrite

i:

  • Makes a file immutable
  • Can’t be deleted or changed in any way

Create a secret file and directory

Now
before we start we must have a top secret file which needs protection
from root user. I have created a secret_file with below text

[root@node1 ~]# cat /tmp/deepak/secret_file
This is a secret file

Check the assigned attributes

By default when we create a file or directory, it does not has any extended attributes other than “e” which means extent format i.e. these files support extended attributes

[root@node1 ~]# lsattr /tmp/
-------------e-- /tmp/tracker-extract-files.0
-------------e-- /tmp/yum_save_tx.2019-03-22.22-16.7ocUW8.yumtx
-------------e-- /tmp/systemd-private-1ad03926d17f4de68a8fdfdd0449c980-chronyd.service-FhlC0B
-------------e-- /tmp/systemd-private-1ad03926d17f4de68a8fdfdd0449c980-bolt.service-2Oomt7
-------------e-- /tmp/systemd-private-1ad03926d17f4de68a8fdfdd0449c980-rtkit-daemon.service-TEwKlB
-------------e-- /tmp/deepak
-------------e-- /tmp/systemd-private-1ad03926d17f4de68a8fdfdd0449c980-colord.service-cUfgTm
-------------e-- /tmp/yum_save_tx.2019-03-22.22-16.ZCjaVi.yumtx
-------------e-- /tmp/systemd-private-1ad03926d17f4de68a8fdfdd0449c980-cups.service-5yacYU

Restrict access and allow only to append content

Now we will use “+a” to allow root user to append some data to our secret file but root won’t be allowed to overwrite the file.

[root@node1 ~]# chattr +a /tmp/deepak/secret_file

Check the assigned attributes and as you see now we have “a” also assigned to our secret_file

[root@node1 ~]# lsattr /tmp/deepak/
-----a-------e-- /tmp/deepak/secret_file

Next try to append some data to this file

[root@node1 ~]# echo "I am appending some more content" >> /tmp/deepak/secret_file

Looks like it worked as expected, verify the same

[root@node1 ~]# cat /tmp/deepak/secret_file
This is a secret file
I am appending some more content

So, as you see now our secret file has some more content.

Let us try to overwrite the data

[root@node1 ~]# echo "I am trying to overwrite the content" > /tmp/deepak/secret_file
-bash: /tmp/deepak/secret_file: Operation not permitted

As expected the extended attributes didn’t allowed me to overwrite the data.

Make the file immutable (restrict all activity)

Now let us make the file immutable so no change at all can be made to this file.

[root@node1 ~]# chattr +i /tmp/deepak/secret_file

Check the applied attributes

[root@node1 ~]# lsattr /tmp/deepak/secret_file
----ia-------e-- /tmp/deepak/secret_file

As you see both “a” and “i” are applied to our secret file but since “i” serves our purpose we do not need “a” here so we will remove the “a” attribute

[root@node1 ~]# chattr -a /tmp/deepak/secret_file

Next verify the applied attributes again

[root@node1 ~]# lsattr /tmp/deepak/secret_file
----i--------e-- /tmp/deepak/secret_file

Next I will try to overwrite the data of this file and will also attempt to remove this file

[root@node1 ~]# echo "I am trying to overwrite the content" > /tmp/deepak/secret_file
-bash: /tmp/deepak/secret_file: Permission denied [root@node1 ~]# rm -f /tmp/deepak/secret_file
rm: cannot remove ‘/tmp/deepak/secret_file’: Operation not permitted

But as you see due to the extended attributes the system does not allows root user to perform any activity on this file.

Instead of file you can also apply these attributes at directory level to protect all the files under the respective directory.

Remove extended attributes

To remove an extended attributes as I also showed in above step use minus sign along with the option

# chattr -a <file/directory>
# chattr -i <file/directory>

Lastly I hope the steps from the article to prevent or restrict root user access on files and directories on Linux was helpful. So, let me know your suggestions and feedback using the comment section.

https://www.golinuxcloud.com/restrict-root-directory-extended-attributes/

How to restrict root user to access or modify a file and directory in Linux的更多相关文章

  1. linux下编译安装MariaDB 10.4.7,解决错误:cannot access ‘/auth_pam_tool_dir’: No such file or directory

    编译安装MariaDB 10.4.7,前面的步骤我就不复述了,一切正常没什么问题. 当执行到:scripts/mysql_install_db --basedir=/usr/local/mysql - ...

  2. python文件和文件夹訪问File and Directory Access

    http://blog.csdn.net/pipisorry/article/details/47907589 os.path - Common pathname manipulations 都是和路 ...

  3. 命令stat anaconda-ks.cfg会显示出文件的三种时间状态(已加粗):Access、Modify、Change。这三种时间的区别将在下面的touch命令中详细详解:

    7.stat命令 stat命令用于查看文件的具体存储信息和时间等信息,格式为"stat 文件名称". stat命令可以用于查看文件的存储信息和时间等信息,命令stat anacon ...

  4. hadoop 异常 ls: Cannot access .: No such file or directory.

    bin/hadoop dfs -lsls: Cannot access .: No such file or directory. bin/hadoop dfs -ls /用这个命令代替试试 原因是格 ...

  5. [转] stat命令输出结果中, Access,Modify,Change的含义

    先建立一个空白文件a.txt 1 [emduser@emd tmp]$ touch a.txt 2   3 [emduser@emd tmp]$ ls -al a.txt 4   5 -rw-rw-r ...

  6. 初次启动hive,解决 ls: cannot access /home/hadoop/spark-2.2.0-bin-hadoop2.6/lib/spark-assembly-*.jar: No such file or directory问题

    >>提君博客原创  http://www.cnblogs.com/tijun/  << 刚刚安装好hive,进行第一次启动 提君博客原创 [hadoop@ltt1 bin]$ ...

  7. /dev/root: No such file or directory

    /*************************************************************************** * /dev/root: No such fi ...

  8. qemu 出现Could not access KVM kernel module: No such file or directory failed to initialize KVM: No such file or directory

    使用qemu命令 qemu-system-x86_64 -hda image/ubuntu-test.img -cdrom ubuntu-16.04.2-server-amd64.iso -m 102 ...

  9. 使用PuTTY软件远程登录root被拒:access denied

    PuTTY是一个Telnet.SSH.rlogin.纯TCP以及串行接口连接软件. 使用PuTTY软件远程登录root时,提示:ACCESS DENIED,很有可能是由sshd的默认配置造成的. 可以 ...

随机推荐

  1. ue4 动画相关方法杂记

    加载资源文件中的 骨骼,动画,蒙太奇----------------------------------------- FName anis("/Game/FirstPerson/Anima ...

  2. ugui获取text宽

    http://www.xuanyusong.com/archives/3587 void Start () { Font font = Resources.Load<Font>(" ...

  3. LOL数值分析

    http://blog.sina.com.cn/s/blog_704133cb01018hud.html 为了从理论层面提高自己打<英雄联盟>的水平,再加上自己也有这方面的兴趣,所以我最近 ...

  4. 洛谷P1228 地毯填补问题

    P1228 地毯填补问题 题目描述 相传在一个古老的阿拉伯国家里,有一座宫殿.宫殿里有个四四方方的格子迷宫,国王选择驸马的方法非常特殊,也非常简单:公主就站在其中一个方格子上,只要谁能用地毯将除公主站 ...

  5. jsp学习与提高(一)——JSP生命周期、三大指令及动作

    1.jsp定义: 1.1以java语言为脚本语言,运行在服务端的程序: 1.2处理客户请求,生成页面 1.3其本质是个sevlet会生成.java文件编译后再生成.class文件 2.jsp生命周期( ...

  6. jQuery EasyUI/TopJUI创建文本验证框(不写js,纯HTML实现!!!)

    jQuery EasyUI/TopJUI创建文本验证框(不写js,纯HTML实现!!!) validatebox(验证框)的设计目的是为了验证输入的表单字段是否有效.如果用户输入了无效的值,它将会更改 ...

  7. 洛谷 P3381 【模板】最小费用最大流

    题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行包含四个正整数\(N.M.S.T\) ...

  8. [TCP/IP]TCP的三次握手和四次挥手

    概述 总结一下TCP中3次握手过程,以及其原生的缺陷 引起的SYN Flood的介绍 1.TCP连接建立--三次握手 几个概念: seq:序号,占4个字节,范围[0,4284967296],由于TCP ...

  9. TYVJ P2032 「Poetize9」升降梯上 spfa最短路

    %%%暴搜出奇迹%%%@SiriusRen 其实我刚开始题读错了,才导致我写图论... spfa跑最短路,开一个node记录状态(pair当然滋磁):所在楼层和槽的位置 以层数为1,槽在0的位置 为初 ...

  10. Linux中/etc/passwd 和 /etc/shadows 详解

    linux操作系统上的用户如果需要登录主机,当其输入用户名和密码之后: 首先在/etc/passwd文件中查找是否有你的账号,如果没有无法登录,如果有的话将该用户的UID和GID读出来,此外将此用户的 ...