CentOS7 配置sudo并使用

2025-10-10 16:32:44

1、新建普通用户linuxprobe

[root@docker-1 ~]# useradd linuxprobe

[root@docker-1 ~]# echo linuxprobe | passwd --stdin linuxprobe

Changing password for user linuxprobe.

passwd: all authentication tokens updated successfully.

CentOS7 配置sudo并使用

2、对linuxprobe进行提权

使用visudo直接编辑sudoers文件。在第92行添加

linuxprobe  ALL=(ALL)       ALL

# 切换到linuxprobe账户下

[root@docker-1 ~]# su - linuxprobe

[linuxprobe@linuxprobe ~]$ /usr/bin/cat

cat        catchsegv  catman     

[linuxprobe@linuxprobe ~]$ /usr/bin/cat

cat        catchsegv  catman     

[linuxprobe@linuxprobe ~]$ /usr/bin/cat /etc/shadow

/usr/bin/cat: /etc/shadow: 权限不够

[linuxprobe@linuxprobe ~]$ sudo /usr/bin/cat /etc/shadow

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 linuxprobe: 

root:$6$Agm4VSjIjiTeIXK8$3Ie4l40ehSRdIejs4io85ZLONAXEOlfuOf.n2t8gWUj9HFbaz3ae6AVV0/CFZmlGVrJjlSE2YYgjnzuUmMUGe/::0:99999:7:::

bin:*:17110:0:99999:7:::

daemon:*:17110:0:99999:7:::

adm:*:17110:0:99999:7:::

lp:*:17110:0:99999:7:::

CentOS7 配置sudo并使用

3、设置linuxprobe不能执行的一些命令

编辑/etc/sudoers,第49行,添加下面命令

Cmnd_Alias SHUTDOWN = /sbin/halt, /sbin/shutdown, \/sbin/poweroff, /sbin/reboot, /sbin/init

linuxprobe  ALL=(ALL)       ALL,!SHUTDOWN 

注意:中间有逗号

CentOS7 配置sudo并使用

4、转迅披移root的一些权限到特定的组中

编辑/etc/sudoers,第51行,添加下面命令

Cmnd_Alias USERMGR = /usr/sbin/useradd, /usr/sbin/userdel, /usr/sbin/usermod, /usr/bin/passwd

# 最后一行添加下面配醒叮置调艳闲

%usermgr ALL=(ALL) USERMGR

[root@docker-1 ~]# su - linuxprobe

Last login: Fri Sep  8 21:25:48 CST 2017 on pts/0

[linuxprobe@linuxprobe ~]$ sudo /sbin/shutdown -r now

[sudo] password for linuxprobe: 

对不起,用户 linuxprobe 无权以 root 的身份在 linuxprobe 上执行 /sbin/shutdown -r now。

[linuxprobe@linuxprobe ~]$ 登出

[root@docker-1 ~]# visudo

[root@docker-1 ~]# groupadd usermgr

[root@docker-1 ~]# usermod -G usermgr linuxprobe

[root@docker-1 ~]# su - linuxprobe

Last login: Fri Sep  8 21:32:15 CST 2017 on pts/0

[linuxprobe@linuxprobe ~]$ sudo /usr/sbin/useradd testuser

[sudo] password for linuxprobe: 

[linuxprobe@linuxprobe ~]$ sudo /usr/sbin/passwd testuser

sudo:/usr/sbin/passwd:找不到命令

[linuxprobe@linuxprobe ~]$ sudo /usr/bin/passwd testuser

更改用户 testuser 的密码 。

新的 密码:

无效的密码: 密码少于 8 个字符

重新输入新的 密码:

passwd:所有的身份验证令牌已经成功更新。

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢