限制用户su
禁止普通用户su到root用户,只允许指定的用户su到root用户。(需要root权限)
为禁止普通用户su至root,需要分别修改/etc/pam.d/su和/etc/login.defs两个配置文件。
1、只有wheel组用户能够su
1.1、配置
首先:将用户加入到wheel组(后面会设置只有wheel组的用户才能su到root)
| 01 02 03 | [root@localhost ~]# usermod -G wheel doubles [root@localhost ~]# id doubles uid=1002(doubles) gid=1002(doubles) groups=1002(doubles),10(wheel) |
去除/etc/pam.d/su文件中如下行的注释:(表示要求用户在wheel组下才能root)
| 01 02 | [root@localhost ~]# vim /etc/pam.d/su auth required pam_wheel.so use_uid |
在/etc/login.defs文件中加入如下配置项:(表示只有wheel组能够su到root)
| 01 02 | [root@localhost ~]# vim /etc/login.defs SU_WHEEL_ONLY yes |
1.2、测试
下面我们切换到普通用户su试一下
| 01 02 03 04 05 06 07 | [root@localhost shellscript]# su dd [dd@localhost shellscript]$ id uid=1001(dd) gid=1001(dd) groups=1001(dd) [dd@localhost shellscript]$ su Password: su: Permission denied [dd@localhost shellscript]$ |
上面用户dd没有在wheel组下,所以即使输入了正确的密码也不能su,权限限制。而doubles是在wheel组下的,所以下面我们用doubles试一下
| 01 02 03 04 | [root@localhost shellscript]# su doubles [doubles@localhost shellscript]$ su Password: [root@localhost shellscript]# |
doubles在wheel组下,成功su到root。
2、自定义用户组能够su
除了wheel组,也可以配置只有指定的组的用户能够su到root。
参考:Permitting or Restricting a User's `su` Access to Privileged Accounts - Red Hat Customer Portal
https://www.cnblogs.com/kevingrace/p/8671964.html
A、创建用户组groupa
| 01 | [root@localhost ~]# groupadd groupa |
B、创建/etc/security/su-groupa-access文件,里面指定允许su到的用户
| 01 02 03 | [root@localhost ~]# vim /etc/security/su-groupa-access doubles root |
这就表示groupa的用户可以su到root和doubles
保证文件权限不是所有人都可以修改的。
| 01 02 | [root@localhost ~]# ll /etc/security/su-groupa-access -rw-r--r-- 1 root root 13 Sep 8 19:46 /etc/security/su-groupa-access |
C、在/etc/pam.d/su下加入下面三行
| 01 02 03 04 | [root@localhost ~]# vim /etc/pam.d/su auth [success=2 default=ignore] pam_succeed_if.so use_uid user notingroup groupa auth required pam_wheel.so use_uid group=groupa auth required pam_listfile.so item=user sense=allow onerr=fail file=/etc/security/su-groupa-access |