zl程序教程

您现在的位置是:首页 >  其它

当前栏目

centos8上添加sudoer用户

用户 添加 CentOS8
2023-09-14 08:59:33 时间

一,检查服务器是否已安装sudo的rpm包?

1,查询rpm包列表

[root@yjweb ~]# rpm -qa | grep sudo
libsss_sudo-2.0.0-43.el8_0.3.x86_64
sudo-1.8.25p1-4.el8.x86_64

 

2,如未安装,执行下面的命令安装:

[root@yjweb ~]# yum install sudo

 

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

         对应的源码可以访问这里获取: https://github.com/liuhongdi/

 说明:作者:刘宏缔 邮箱: 371125307@qq.com

 

二,在centos8上面添加sudoer用户的两种方法:

1,把用户账号添加到wheel组
2, 把用户账号添加到sudoers文件

 

三,新建用户webop

1,添加用户webop

[root@yjweb ~]# groupadd webop
[root@yjweb ~]# useradd -g webop webop
[root@yjweb ~]# ls /home/webop/
[root@yjweb ~]# grep webop /etc/passwd
webop:x:1000:1000::/home/webop:/bin/bash
[root@yjweb ~]# passwd webop

 

2,用webop通过ssh登录后,测试sudo 

[webop@yjweb ~]$ sudo -i
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.
[sudo] password for webop:
webop is not in the sudoers file.  This incident will be reported.

 

注意:因为webop没有被添加成为sudoer,所以系统给出报错

 

四,把用户webop添加wheel组,再次重新尝试sudo

1,把webop用户添加到wheel组

[root@yjweb ~]# usermod -aG wheel webop

 

关于参数-aG

       -a, --append
           Add the user to the supplementary group(s). Use only with the -G option.

可以看到-a参数作用是:添加用户到基本的组,仅和 -G选项一起使用

       -G, --groups GROUP1[,GROUP2,...[,GROUPN]]]
           A list of supplementary groups which the user is also a member of. Each group is separated from the next by a comma,
with no intervening whitespace. The groups are subject to the same restrictions as the group given with the -g option. If the user is currently a member of a group which is not listed, the user will be removed from the group.
This behaviour can be changed via the -a option, which appends the user to the current supplementary group list.

-G参数指定用户所属的group

注意它指定group列表时是用逗号隔开

如果用户当前属于未被列出的组的成员,则用户会被从所属的那个group中移出。

如果搭配 -a选项,则仅会被添加,不会有移除的情况

 

2,查看当前用户所属的group

注意:修改用户所属的组之后,需要logout后再重新登录,才能看到效果

[webop@yjweb ~]$ groups
webop wheel

说明:可以看到 webop被添加到了wheel组

 

3,测试sudo

[webop@yjweb ~]$ sudo -i
[sudo] password for webop:
[root@yjweb ~]# 

说明:成功的sudo到了root账户

 

五,为什么用户添加到wheel组后,就成为了sudoer?

[root@yjweb ~]# grep wheel /etc/sudoers
## Allows people in group wheel to run all commands
%wheel  ALL=(ALL)       ALL

说明:可以看到 wheel用户组是被配置为运行所有命令的sudoer

 

六, 把用户webop2添加/etc/sudoers文件,再次重新尝试sudo

说明;新建一个用户webop2,然后把用户添加到/etc/sudoers文件

1,用户添加到/etc/sudoers文件

[root@database2 ~]# visudo

添加一行:

webop2   ALL=(ALL)       ALL

 

说明:为什么要用 visudo?

查看/etc/sudoers的用户权限:

[root@database2 ~]# ll /etc/sudoers
-r--r----- 1 root root 4003 Mar 26  2015 /etc/sudoers

可以看到用户的权限是440,带来的问题就是它是一个只读的文件,

编辑它时需要先添加写权限,编辑完成后再改为只读,

这个过程很不方便 ,

而使用visudo则不存在这个问题

 

2,  /etc/sudoers 文件中的命令格式说明:

<user>      ALL=(ALL:ALL) NOPASSWD:ALL

 

说明:

<user>          用户名,如果前面加%则表示是一个group
ALL=(ALL:ALL)   三个ALL分别是: host 用户 组
NOPASSWD:ALL    执行的命令,ALL表示所有命令 
                NOPASSWD  表示系统不询问密码   

 

七,看一下只允许指定用户执行指定命令的sudo例子:

[root@webserver1 cron]# grep mkdirchmod /etc/sudoers
laoliu   ALL=(ALL)       NOPASSWD:/usr/local/cmd/tools/mkdirchmod.sh

说明:允许laoliu这个用户sudo执行mkdirchmod.sh这个脚本,系统不询问密码

 

八,查看centos的版本:

[root@yjweb ~]# cat /etc/redhat-release
CentOS Linux release 8.0.1905 (Core)