How to restrict root user to access or modify a file and directory in Linux
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的更多相关文章
- 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 - ...
- python文件和文件夹訪问File and Directory Access
http://blog.csdn.net/pipisorry/article/details/47907589 os.path - Common pathname manipulations 都是和路 ...
- 命令stat anaconda-ks.cfg会显示出文件的三种时间状态(已加粗):Access、Modify、Change。这三种时间的区别将在下面的touch命令中详细详解:
7.stat命令 stat命令用于查看文件的具体存储信息和时间等信息,格式为"stat 文件名称". stat命令可以用于查看文件的存储信息和时间等信息,命令stat anacon ...
- hadoop 异常 ls: Cannot access .: No such file or directory.
bin/hadoop dfs -lsls: Cannot access .: No such file or directory. bin/hadoop dfs -ls /用这个命令代替试试 原因是格 ...
- [转] 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 ...
- 初次启动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]$ ...
- /dev/root: No such file or directory
/*************************************************************************** * /dev/root: No such fi ...
- 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 ...
- 使用PuTTY软件远程登录root被拒:access denied
PuTTY是一个Telnet.SSH.rlogin.纯TCP以及串行接口连接软件. 使用PuTTY软件远程登录root时,提示:ACCESS DENIED,很有可能是由sshd的默认配置造成的. 可以 ...
随机推荐
- gulp --watch直接退出,并没有监听
1.在es6(彩票项目)搭建环境时遇到gulp --watch 只运行一次就退出了不能监听: D:\nodejs\es6-base>gulp --watch [::] Failed to loa ...
- Insert into a Binary Search Tree
Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...
- c++弱引用与强引用
https://www.zhihu.com/question/26851369 智能指针一个很重要的概念是"所有权",所有权意味着当这个智能指针被销毁的时候,它指向的内存(或其它资 ...
- 一些我推荐的和想上的网络课程(Coursera, edX, Udacity)
从面向找工作的角度出发,我觉得以下课程有很大帮助: 首推Robert Sedgewick,也是我觉得对我帮助最大的老师,讲课特点是能把复杂的算法讲解清楚(典型例子:红黑树,KMP算法) 他在Cours ...
- Uva1377
/* 在n个刻度和他们的差里挑不超过7个刻度,0是固定的,最大的刻度肯定是最大值,然后剩下的dfs挑. */ #include<bits/stdc++.h> #define inf 0x3 ...
- Django -- 权限初识
待 需求分析-场景 假设需要为公司设计一个人员管理系统,并为各级领导及全体员工分配系统登录账号.有如下几个要求: 1.权限等级不同 公司领导登录后可查看所有员工的信息,部门领导登陆后之可查看本部门员工 ...
- 从wireshake分析http和https的通信过程
参考文章: Wireshark基本介绍和学习TCP三次握手 [技术流]Wireshark对HTTPS数据的解密 Wireshark/HTTPS Journey to HTTP/2 以TCP/IP协议为 ...
- 关于AQS——独占锁特性+共享锁实现(二)
五.可中断获取锁的实现(独占锁的特性之一) 我们知道lock相较于synchronized有一些更方便的特性,比如能响应中断以及超时等待等特性,现在我们依旧采用通过学习源码的方式来看看能够响应中断是怎 ...
- windows下使用MYSQL的mysqldumpslow进行慢日志分析
1.首先安装好perl环境. 2.在dos环境中,切换到perl目录中,例如我的目录是 dos 命令 cd c:\Perl\bin 3.在此目录输入perl mysqldumpslow的路径\mysq ...
- webpack.config.js====CSS相关:css和scss配置loader
1. 安装: //loader加载器加载css和sass模块 cnpm install style-loader css-loader node-sass sass-loader --save-dev ...