Linux禁止非wheel组的用户su切换root

1、引言

  默认情况下,任何普通用户都能通过su命令切换到root用户。众所周知,root对于Linux系统拥有最高权限!因此,任何用户不在允许范围内切换到root都会威胁到整个系统的安全。增加指定用户才能su到root的系统管理设置相当于给系统安全提升了一道安全屏障。

2、系统环境

  系统环境:RedHat 8.0

  主要配置文件:/etc/pam.d/su

3、编辑配置文件

  执行命令: vim /etc/pam.d/su ,找到“auth  required  pam_wheel.so use_uid” 所在行,将前面的“#”去掉(若没有此行,则添加auth  required  pam_wheel.so),保存退出。

 1 #%PAM-1.0
 2 auth            sufficient      pam_rootok.so
 3 # Uncomment the following line to implicitly trust users in the "wheel" group.
 4 #auth           sufficient      pam_wheel.so trust use_uid
 5 # Uncomment the following line to require a user to be in the "wheel" group.
 6 auth            required        pam_wheel.so use_uid               #禁止非wheel组用户su切换到root
 7 auth            substack        system-auth
 8 auth            include         postlogin
 9 account         sufficient      pam_succeed_if.so uid = 0 use_uid quiet
10 account         include         system-auth
11 password        include         system-auth
12 session         include         system-auth
13 session         include         postlogin
14 session         optional        pam_xauth.so

4、测试 

4.1、测试前准备

  新建两个测试账户,执行命令: useradd test 和 useradd test1 

  将test账户加入wheel组,执行命令: usermod -aG wheel test 

  查询wheel组用户成员,执行命令: cat /etc/group | grep test 或 id test ,此时可以看到test已加入到了wheel组,而test1为非wheel组成员

  

  

4.2、测试截图

  非wheel组用户使用su切换root: 

  

   wheel组用户使用su切换root:

  

4.3、测试结论

  wheel组用户成员可以正常使用su,输入正确的密码即可切换到root,而非wheel组成员则无法使用su切换到root。

  

 

posted @ 2025-03-25 16:36  BK小君  阅读(323)  评论(1)    收藏  举报