Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

只准 wheel 組切換 root:兩種限制 su 的做法

一般情況下,普通使用者只要執行 su - 並輸入正確的 root 密碼,就能取得 root 身分做管理員等級的設定。

但為了更進一步收緊安全,值得建立一個管理員群組,只允許這個組的成員用 su - 切換成 root;其他組的成員即使密碼打對了也進不去。在 UNIX 與 Linux 世界裡,這個組通常就叫 wheel。

一、禁止非 wheel 組成員切換到 root

1、修改 /etc/pam.d/su

[root@db01 ~]# vi /etc/pam.d/su
#auth required /lib/security/$ISA/pam_wheel.so use_uid  ← 找到這行,去掉行首的「#」

2、修改 /etc/login.defs

[root@db01 ~]# echo "SU_WHEEL_ONLY yes" >> /etc/login.defs

以上兩步完成後,可以建個新帳號來驗證:沒有加入 wheel 組的使用者,執行 su - 時就算輸入正確的 root 密碼也無法登入。

3、建立測試帳號 woo

[root@db01 ~]# useradd woo
[root@db01 ~]# passwd woo
Changing password for user woo.
New UNIX password:
BAD PASSWORD: it is WAY too short
Retype new UNIX password:
passwd: all authentication tokens updated successfully

4、用 woo 登入嘗試切換

[woo@db01 ~]$ su - root   ← 即使密碼正確也切不過去
Password:
su: incorrect password
[woo@db01 ~]$

5、把 woo 加進 wheel 組後再試

[root@db01 ~]# usermod -G wheel woo
[root@db01 ~]# su - woo
[woo@db01 ~]$ su - root   ← 這次可以了
Password:
[root@db01 ~]#

二、建立管理員帳號並禁止普通使用者 su

建立 admin 帳號並加入 wheel 組,搭配後續的 OpenSSH/OpenSSL 強化遠端管理安全:

[root@db01 ~]# useradd admin
[root@db01 ~]# passwd admin
Changing password for user admin.
New UNIX password:
BAD PASSWORD: it is too short
Retype new UNIX password:
passwd: all authentication tokens updated successfully.

# usermod -G wheel admin(或 usermod -G10 admin,10 是 wheel 組的 GID)
[root@db01 ~]# usermod -G wheel admin
[root@db01 ~]# su - admin
[admin@db01 ~]$ su - root
Password:
[root@db01 ~]#

方法一:指定其他群組

wheel 也可以換成別的組。編輯 /etc/pam.d/su,加入兩行:

[root@db01 ~]# vi /etc/pam.d/su
auth sufficient /lib/security/pam_rootok.so debug
auth required /lib/security/pam_wheel.so group=wheel

方法二:直接取消註解

[root@db01 ~]# vi /etc/pam.d/su
#RedHat#auth required /lib/security/$ISA/pam_wheel.so use_uid  ← 去掉行首「#」
#CentOS5#auth required pam_wheel.so use_uid                    ← 去掉行首「#」

存檔退出,再補上 login.defs 的設定:

[root@db01 ~]# echo "SU_WHEEL_ONLY yes" >> /etc/login.defs

兩種方法的差別在於判定邏輯:方法二靠 pam_wheel 的 use_uid 直接比對,方法一則用 group= 明確指定允許的群組,彈性稍高一些。無論採用哪一種,都建議同時確認 root 帳號本身有強密碼、且 SSH 已關閉 root 直接登入,這道限制才有意義。

Copyright © Linux教程網 All Rights Reserved