linux 添加用户sudoer,centos8上添加sudoer用户

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

1,查询rpm包列表

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

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

[root@yjweb ~]# yum install sudo

说明:作者:刘宏缔 邮箱: 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/passwdwebop: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 forwebop:

webop is notin 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, --groupsGROUP1[,GROUP2,...[,GROUPN]]]

A list of supplementarygroups which the user is also a member of. Each group is separated from the next by a comma,

with no intervening whitespace. The groupsare subject to the same restrictions as the group given with the-g option.

If the user is currently a member of a groupwhich is not listed, the user will be removed from the group.

This behaviour can be changed via the -a option, whichappends the user to the current supplementary group list.

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

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

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

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

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

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

[webop@yjweb ~]$ groupswebop wheel

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

3,测试sudo

[webop@yjweb ~]$ sudo -i

[sudo] password forwebop:

[root@yjweb~]#

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

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

[root@yjweb ~]# grep wheel /etc/sudoers

## Allows peopleingroup 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 文件中的命令格式说明:

ALL=(ALL:ALL) NOPASSWD:ALL

说明:

用户名,如果前面加%则表示是一个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 release8.0.1905 (Core)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值