【linux命令】权限管理命令(chattr、lsattr、sudo)

时间:2019-11-22
本文章向大家介绍【linux命令】权限管理命令(chattr、lsattr、sudo),主要包括【linux命令】权限管理命令(chattr、lsattr、sudo)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

目录

一、chattr命令

  chattr命令用来修改文件系统的权限属性,只有 root 用户可以使用,建立凌驾于 rwx 基础权限之上的授权。

PS:chattr 命令不宜对目录 /、/dev/、/tmp/、/var/ 等进行设置,严重者甚至容易导致系统无法启动。

格式:
chattr [+-=] [选项] 文件或目录名

选项:

+    増加权限
-    删除权限
=    等于某权限
i    如果对文件设置属性,不允许对文件进行删除、改名,也不能添加和修改数据;如果对目录设置 i 属性,只能修改目录下文件中的数据,不允许建立和删除文件
a    如果对文件设置 a 属性,只能在文件中増加数据,不能删除和修改数据;如果对目录设置 a 属性,只允许在目录中建立和修改文件,不允许删除文件
e    Linux 中大多数文件都默认拥有 e 属性,表示该文件是使用 ext 文件系统进行存储的,而且不能使用"chattr -e"命令取消 e 属性

给文件赋予属性:

# 创建测试文件
[root@VM_0_10_centos ~]# mkdir -p /study
[root@VM_0_10_centos ~]# cd /study/
[root@VM_0_10_centos study]# touch tfile.txt
[root@VM_0_10_centos study]# lsattr tfile.txt 
-------------e-- tfile.txt

# 添加属性 +i表示加锁,文件不能被删除,修改,移动
[root@VM_0_10_centos study]# chattr +i tfile.txt 
[root@VM_0_10_centos study]# lsattr tfile.txt 
----i--------e-- tfile.txt
[root@VM_0_10_centos study]# rm -rf tfile.txt 
rm: cannot remove ‘tfile.txt’: Operation not permitted
[root@VM_0_10_centos study]# mv tfile.txt tfile2.txt 
mv: cannot move ‘tfile.txt’ to ‘tfile2.txt’: Operation not permitted
[root@VM_0_10_centos study]# vi tfile.txt
能输入,但左下方会显示“W10: Warning: Changing a readonly file”
并且也不能保存

给目录赋予权限:

# 创建测试目录
[root@VM_0_10_centos study]# mkdir dtest
[root@VM_0_10_centos study]# touch dtest/test.txt

# 添加i属性,加锁,目录不能被删除,也不能在该目录下创建文件和目录,但能修改该目录下文件的内容
[root@VM_0_10_centos study]# chattr +i dtest/
[root@VM_0_10_centos study]# lsattr dtest/
-------------e-- dtest/test.txt

# 不能创建目录和文件
[root@VM_0_10_centos study]# mkdir -p dtest/dir2
mkdir: cannot create directory ‘dtest/dir2’: Permission denied
[root@VM_0_10_centos study]# touch dtest/ftest2.txt
touch: cannot touch ‘dtest/ftest2.txt’: Permission denied

# 能修改目录下文件内容
[root@VM_0_10_centos study]# echo hello >> dtest/test.txt 
[root@VM_0_10_centos study]# cat dtest/test.txt 
hello

# 不能删除目录及目录下的文件
[root@VM_0_10_centos study]# rm -rf dtest/
rm: cannot remove ‘dtest/test.txt’: Permission denied
[root@VM_0_10_centos study]# rm -rf dtest/test.txt 
rm: cannot remove ‘dtest/test.txt’: Permission denied

PS:root用户也不能删除,修改它。如果要修改和删除,需去掉i属性

[root@VM_0_10_centos study]# chattr -i dtest/
[root@VM_0_10_centos study]# lsattr dtest/
-------------e-- dtest/test.txt
[root@VM_0_10_centos study]# chattr -i tfile.txt 
[root@VM_0_10_centos study]# lsattr tfile.txt 
-------------e-- tfile.txt

案例:比如将备份的日志放入一个只能添加数据,不能删除数据的目录下

# 创建备份目录
[root@VM_0_10_centos study]# mkdir baklog

# 赋予a属性
[root@VM_0_10_centos study]# chattr +a baklog/

# 添加数据进去
[root@VM_0_10_centos study]# cp tfile.txt baklog/
[root@VM_0_10_centos study]# ls baklog/
tfile.txt

# 不能删除该目录下的数据
[root@VM_0_10_centos study]# rm -rf baklog/tfile.txt 
rm: cannot remove ‘baklog/tfile.txt’: Operation not permitted

# 能修改该目录的文件内容
[root@VM_0_10_centos study]# echo test >> baklog/tfile.txt 
[root@VM_0_10_centos study]# cat baklog/tfile.txt 
test

二、lsattr命令

  lsattr命令用于查看文件和目录的属性。

格式:

lsattr [选项]  文件或目录名

选项:

-a    显示所有文件和目录,包括隐藏文件
-d    如果目标是目录,则仅列出目录本身的属性,而不会列出文件的属性
-R  递归列出所有子目录中文件的属性

案例:查看目录属性

# 查看目录属性,需要加上-d
[root@VM_0_10_centos study]# lsattr -d baklog/
-----a-------e-- baklog/

三、sudo命令

  管理员作为特权用户,可授权普通用户协助完成日常管理。现在较为流行的工具是 sudo,几乎所有 Linux 都已默认安装。sudo 的操作对象是系统命令,也就是 root 把本来只能由超级用户执行的命令赋予普通用户执行。简单的说,sudo 是一种权限管理机制,管理员可以授权于一些普通用户去执行一些 root 执行的操作,而不需要知道 root 的密码。

sudo 使用简单,管理员 root 使用 visudo 命令即可编辑其配置文件 /etc/sudoers 进行授权。命令如下:

[root@VM_0_10_centos study]# visudo

格式说明:

user ALL=(ALL) ALL 
%userg ALL=(ALL) ALL 
user ALL=(ALL) NOPASSWD: ALL 
%userg ALL=(ALL) NOPASSWD: ALL

# 第一行:允许用户youuser执行sudo命令(需要输入密码). 
# 第二行:允许用户组youuser里面的用户执行sudo命令(需要输入密码). 
# 第三行:允许用户youuser执行sudo命令,并且在执行的时候不输入密码. 
# 第四行:允许用户组youuser里面的用户执行sudo命令,并且在执行的时候不输入密码. 

原文地址:https://www.cnblogs.com/HeiDi-BoKe/p/11911684.html