linux极简小知识:8、Centos下sudo权限提升,sudoers配置详解,权限的管理限制及最佳示例

1,550 阅读7分钟

本文正在参与 “走过Linux三十年”话题征文活动

sudo 表示 super user do,超级用户所做的

sudo 权限提升

sudo 可以把特定命令的执行权限赋予给某个用户。实际中,要:在保证普通用户完成相应工作的前提下,尽可能少地赋予其额外的权限

通过配置 sudoers 文件,可以实现对用户 sudo 权限的配置。

赋予一个用户拥有和 root 相同的权限

这是一种最简单粗暴,也是最不推荐的方式,直接将 root 一样的权限赋予某个用户。

新建用户指定密码,或者直接使用一个有密码的用户。

adduser root_test

passwd root_test

之后,编辑sudoers文件:

vim /etc/sudoers

对照其中 root ALL=(ALL) ALL 一行,添加如下内容:

root    ALL=(ALL)       ALL
root_test    ALL=(ALL)       ALL

保存并退出。

这样,root_test 在借助 sudo 的情况下,拥有和root一样的权限。可以执行任何命令。

在此用户下执行 sudo whoami 将返回root。[未测试]。

正解 sudoers 配置的语法

配置‘sudo’行的语法如下:

User_name Machine_name=(Effective_user) command

语法中各个部分的说明:

  • User_name:这是“sudo”的用户名。

  • Machine_name:“sudo”命令有效或生效的主机名。当有很多主机时很有用。 一个主机时使用 ALL 即可,或者localhost(本地主机)

  • (Effective_user):表示 User_name 可以以哪个用户执行命令。 ()中也可以指定多个用户。指定 ALL 表示可以作为系统中的任何用户执行命令。此项也可以直接省略。

  • command:用户可以使用 duso 运行的命令或一组命令,多个命令使用逗号(,)分割。命令必须为绝对路径,否则无法识别,也可以借助别名简化命令列表

关于第三个部分 括号() 的含义和作用,找了很久才有一个说的比较清晰的介绍文章(见参考),其具体使用参见下面示例。

sudoers配置示例说明

配置用户执行特定命令

如下,配置 root_test1 用户拥有可以执行软件安装卸载的权限。

sudoers中添加:

root_test1 ALL=(ALL)    /usr/bin/yum, /usr/bin/rpm

然后,可以使用 sudo yum xxxsudo rpm xxx 执行软件的管理。

配置用户能以另一个用户执行命令

设置 root_test2 用户能以 root_test1 用户执行命令。

root_test2 ALL=(root_test1)       ALL

sudo -u <other_user>作为另一个用户运行。

  • 以另一个用户执行命令的含义

关于该项配置,让人困惑的是,什么叫以另一个用户执行命令?或者该项配置是如何起作用及实际使用的。

查看如下测试,root_test2 使用 sudo 打开只有 root_test1 拥有权限查看的文件,会提示无权限

root_test1 家目录下,新建文件,设置其他用户没有任何的权限:

[root_test2@VM_0_15_centos ~]$ su - root_test1
Password:
[root_test1@VM_0_15_centos ~]$ touch root_test1_of_file.txt
[root_test1@VM_0_15_centos ~]$ ls -al root_test1_of_file.txt
-rw-rw-r-- 1 root_test1 root_test1 0 Sep  3 11:57 root_test1_of_file.txt
[root_test1@VM_0_15_centos ~]$ chmod o-r root_test1_of_file.txt
[root_test1@VM_0_15_centos ~]$ ls -al root_test1_of_file.txt
-rw-rw---- 1 root_test1 root_test1 0 Sep  3 11:57 root_test1_of_file.txt

切换到 root_test2 用户,查看 root_test1 家目录目录 ls -alsudo ls -al 均无法查看:

[root_test1@VM_0_15_centos ~]$ su root_test2
Password:
[root_test2@VM_0_15_centos root_test1]$ ls -al
ls: cannot open directory .: Permission denied
[root_test2@VM_0_15_centos root_test1]$ sudo ls -al
[sudo] password for root_test2:
Sorry, user root_test2 is not allowed to execute '/bin/ls -al' as root on VM_0_15_centos.
[root_test2@VM_0_15_centos root_test1]$ sudo ls -al root_test1_of_file.txt      
Sorry, user root_test2 is not allowed to execute '/bin/ls -al root_test1_of_file.txt' as root on VM_0_15_centos.

而如果有管理员权限,是可以查看的,如下使用 root :

[root_test2@VM_0_15_centos root_test1]$ su root
Password:
[root@VM_0_15_centos root_test1]# ls -al
total 40
drwx------  5 root_test1 root_test1 4096 Sep  3 11:57 .
drwxr-xr-x. 7 root       root       4096 Sep  3 10:39 ..
-rw-------  1 root_test1 root_test1  413 Sep  3 12:00 .bash_history
-rw-r--r--  1 root_test1 root_test1   18 Dec  7  2016 .bash_logout
-rw-r--r--  1 root_test1 root_test1  193 Dec  7  2016 .bash_profile
-rw-r--r--  1 root_test1 root_test1  231 Dec  7  2016 .bashrc
drwxrwxr-x  3 root_test1 root_test1 4096 Sep  3 09:52 .cache
drwxrwxr-x  3 root_test1 root_test1 4096 Sep  3 09:52 .config
drwxr-xr-x  4 root_test1 root_test1 4096 Jun 21  2019 .mozilla
-rw-rw----  1 root_test1 root_test1    0 Sep  3 11:57 root_test1_of_file.txt
-rw-------  1 root_test1 root_test1  565 Sep  3 10:00 .viminfo
[root@VM_0_15_centos root_test1]# ls -al root_test1_of_file.txt
-rw-rw---- 1 root_test1 root_test1 0 Sep  3 11:57 root_test1_of_file.txt

要想以 root_test1 执行 root_test1 的权限,必须使用 sudo -u root_test1 使当前用户作为 root_test1 用户执行,也即 sudoers 配置中,第三项(user)的作用。

[root_test2@VM_0_15_centos root_test1]$ sudo -u root_test1 ls -al
[sudo] password for root_test2:
total 40
drwx------  5 root_test1 root_test1 4096 Sep  3 11:57 .
drwxr-xr-x. 7 root       root       4096 Sep  3 10:39 ..
-rw-------  1 root_test1 root_test1  413 Sep  3 12:00 .bash_history
-rw-r--r--  1 root_test1 root_test1   18 Dec  7  2016 .bash_logout
-rw-r--r--  1 root_test1 root_test1  193 Dec  7  2016 .bash_profile
-rw-r--r--  1 root_test1 root_test1  231 Dec  7  2016 .bashrc
drwxrwxr-x  3 root_test1 root_test1 4096 Sep  3 09:52 .cache
drwxrwxr-x  3 root_test1 root_test1 4096 Sep  3 09:52 .config
drwxr-xr-x  4 root_test1 root_test1 4096 Jun 21  2019 .mozilla
-rw-rw----  1 root_test1 root_test1    0 Sep  3 11:57 root_test1_of_file.txt
-rw-------  1 root_test1 root_test1  565 Sep  3 10:00 .viminfo
[root_test2@VM_0_15_centos root_test1]$ sudo -u root_test1 ls -al root_test1_of_file.txt
[sudo] password for root_test2:
-rw-rw---- 1 root_test1 root_test1 0 Sep  3 11:57 root_test1_of_file.txt
  • 不能以另一个用户执行其拥有的sudo权限命令

如下所示,即使指定以 root_test1 用户执行yum,也会提示需要root执行:

[root_test2@VM_0_15_centos ~]$ sudo -u root_test1  yum install axel
[sudo] password for root_test2:
Loaded plugins: fastestmirror, langpacks
Repository mariadb is listed more than once in the configuration
You need to be root to perform this command.
[root_test2@VM_0_15_centos ~]$ sudo -u root_test1 yum remove axel
Loaded plugins: fastestmirror, langpacks
Repository mariadb is listed more than once in the configuration
You need to be root to perform this command.

配置用户只能在某个主机上执行命令

最后说一下,配置 sudoers 时指定主机名。

如果有100台服务器,并且使用相同的远程工具部署 sudoers 到所有的 100 台机器上。

如果想要用户 root_test3 只能在名为 “server” 的主机上执行软件安装。则可以使用以下配置:

root_test3 server=(ALL)    /usr/bin/yum install

然后,相同的 sudoers 脚本配置部署到100台服务器上,用户 root_test3 将仅在 server 上被允许执行 yum install 命令。

在其他主机上执行,将会获取到如下信息:

root_test3 is not allowed to run sudo on server.  This incident will be reported.

当在单台服务器上部署使用时,这个配置没有太多用处,使用 ALL 或 hostname 都可以。

为用户组指定sudo权限

百分号开头,可以为一个组指定 sudo 权限,%user_group。如下

%wheel  ALL=(ALL)       ALL

推荐使用visudo编辑sudoers

前面的演示,使用的是 vim 或 vi 直接编辑。但是实际,推荐使用 visudo 命令编辑 /etc/sudoers 文件。

visudo编辑器是sudo包自带的,并且在退出和保存 sudoers 时,会执行语法检查。

这样避免出现错误,破坏 sudoers 的功能。

visudo的另一优势是防止多个用户同时修改 sudoers 文件。如果在一个会话中使用了 visudo 命令,另一个用户执行 visudo,将会得到 "visudo: /etc/sudoers busy, try again later" 的信息。

visudo的使用和vi/vim完全一样。

同时推荐,在 /etc/sudoers 中默认包含的 /etc/sudoers.d 目录下,实现对用户权限的编辑修改。便于管理,且避免相互之间的影响。

## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)
#includedir /etc/sudoers.d

sudoers文件配置中使用别名

别名介绍

sudoers文件允许为(多个)用户、(多个)主机名、(多个)命令或命令组指定别名,这样避免编辑时冗长混乱。

注:所有的 ALIAS_NAME(别名) 必须使用大写字母,否则会有语法错误。

  • 创建用户别名
User_Alias ROOTTESTUSER = root_test1, root_test2
  • 创建命令别名
Cmnd_Alias ROOTTEST = /usr/bin/yum, /usr/bin/rpm
  • 创建主机别名
Host_Alias     FILESERVERS = fs1, fs2

使用visudo和别名的示例

[root@VM_0_15_centos etc]# touch /etc/sudoers.d/my_custom
[root@VM_0_15_centos etc]# visudo -f /etc/sudoers.d/my_custom

保存如下内容:

User_Alias ROOTTESTUSER = root_test1, root_test2
Cmnd_Alias ROOTTEST = /usr/bin/yum, /usr/bin/rpm

ROOTTESTUSER ALL=(ALL) ROOTTEST

不允许执行指定命令

在 sudoers 中还可以指定不允许的命令。

如下,指定 root_test 可以执行 /usr/bin 内的所有命令,除了 chown 命令之外。

打开自定义sudo文件:

[root@VM_0_15_centos etc]# visudo -f /etc/sudoers.d/my_custom

添加如下:

root_test ALL=(ALL) /usr/bin/*, !/usr/bin/chown

测试,可以执行 chmod ,但是无法执行 chown

[root_test@VM_0_15_centos ~]$ sudo chown
[sudo] password for root_test:
Sorry, user root_test is not allowed to execute '/bin/chown' as root on VM_0_15_centos.
[root_test@VM_0_15_centos ~]$ sudo chmod
[sudo] password for root_test:
chmod: missing operand
Try 'chmod --help' for more information.
[root_test@VM_0_15_centos ~]$

取消sudo时的密码要求

使用sudo时,每次都输入密码进行验证。多数资料都介绍到,默认验证密码后 5 分钟内,不会再次验证密码,但实际使用中,比5分钟要短。

这样如果需要频繁操作,就会很麻烦。

在 sudoers 文件,使用 NOPASSWD 可以取消 sudo 执行时的密码验证要求。

如下所示:

root_test ALL=(ALL) NOPASSWD: /usr/bin/*, !/usr/bin/chown

参考

主要参考自:How to add user to sudoers with best practices & examples