參考自:Allow user1 to “su - user2” without password
https://unix.stackexchange.com/questions/113754/allow-user1-to-su-user2-without-password
需求:
在user1用戶下執行: su - user2 免密登錄。
我的實驗系統版本:
CentOS Linux release 7
方法:
# vim /etc/pam.d/su #在pam_rootok.so那一行之后添加如下兩行。 auth [success=ignore default=1] pam_succeed_if.so user = user2 auth sufficient pam_succeed_if.so use_uid user = user1
可以理解為:對於名為user2的賬號,如果使用su程序的用戶名為user1,即可以免密登錄
PAM模塊文檔:
# less /usr/share/doc/pam-1.1.8/txts/README.pam_succeed_if
首先是 use_uid部分
Evaluate conditions using the account of the user whose UID the application is running under instead of the user being authenticated.
然后看fields格式
Available fields are user, uid, gid, shell, home, ruser, rhost, tty and service field > number Field has a value numerically greater than number. field in item:item:... Field is contained in the list of items separated by colons.
據此,還可以實現從user1免密su到uid 為某個范圍的多個系統用戶
實驗:
[root@MyVm] 17:56:55 ~ # id user1 uid=1004(user1) gid=1004(user1) groups=1004(user1) [root@MyVm] 17:56:59 ~ # id user2 uid=1005(user2) gid=1005(user2) groups=1005(user2) [root@MyVm] 17:57:00 ~ # id user3 uid=1006(user3) gid=1006(user3) groups=1006(user3)
修改/etc/pam.d/su:
auth [success=ignore default=1] pam_succeed_if.so uid >= 1005 auth sufficient pam_succeed_if.so use_uid user = user1
可以理解為:對於UID>=1005的賬號,如果使用su程序的用戶名為user1,即可以免密登錄
[root@MyVm] 17:57:49 ~ # su - user1 Last login: Thu Sep 3 17:55:47 CST 2020 on pts/1 [user1@MyVm] 17:57:50 ~ $ su - user2 [user2@MyVm] 17:57:52 ~ $ logout [user1@MyVm] 17:57:53 ~ $ su - user3 Last login: Thu Sep 3 17:55:54 CST 2020 on pts/1 [user3@MyVm] 17:57:55 ~ $ logout
反之,允許多個賬號su免密到某個(些)賬號,可以配置為:
auth [success=ignore default=1] pam_succeed_if.so uid = 1001
auth sufficient pam_succeed_if.so use_uid uid > 1001
PAM模塊資料:
https://www.cnblogs.com/kevingrace/p/8671964.html
找到這個方法之前,發現一種用利用把ssh免密加入到user1的 .bashrc 來實現自動跳轉user2的方法,勉強滿足需求,但是有點繞遠,而且user1差不多是廢了。
附:利用pam認證模塊實現一個免密登陸后門
https://cloud.tencent.com/developer/article/1047280