######## 还可以使用pam规则限制ssh登录 ########
1 1)允许指定的用户(比如kevin、grace账号)进行登录 2 在/etc/pam.d/sshd文件第一行加入,一定要在第一行,因为规则是自上而下进行匹配的。 3 auth required pam_listfile.so item=user sense=allow file=/etc/sshusers onerr=fail 4 5 然后在/etc下建立sshusers文件,编辑这个文件,加入你允许使用ssh服务的用户名,不用重新启动sshd服务。 6 最后重启sshd服务即可! 7 8 操作如下: 9 [root@docker-test1 ~]# vim /etc/pam.d/sshd 10 #%PAM-1.0 11 auth required pam_listfile.so item=user sense=allow file=/etc/sshusers onerr=fail 12 ........ 13 14 [root@docker-test1 ~]# touch /etc/sshusers 15 [root@docker-test1 ~]# vim /etc/sshuserskevingrace 16 kevin 17 grace 18 19 [root@docker-test1 ~]# /etc/init.d/sshd restart 20 21 22 2)pam规则也可以写成deny的。比如拒绝kevin、grace账号进行登录 23 操作如下: 24 [root@docker-test1 ~]# vim /etc/pam.d/sshd 25 #%PAM-1.0 26 auth required pam_listfile.so item=user sense=deny file=/etc/sshusers onerr=succeed 27 ........ 28 29 [root@docker-test1 ~]# touch /etc/sshusers 30 [root@docker-test1 ~]# vim /etc/sshuserskevingrace 31 kevin 32 grace 33 34 35 [root@docker-test1 ~]# /etc/init.d/sshd restart 36 37 3)pam规则可以使用group限制。 38 39 允许规则: 40 auth required pam_listfile.so item=group sense=allow file=/etc/security/allow_groups onerr=fail 41 42 禁止规则: 43 auth required pam_listfile.so item=group sense=deny file=/etc/security/deny_groups onerr=succeed 44 45 46 操作如下: 47 [root@docker-test1 ~]# vim /etc/pam.d/sshd 48 #%PAM-1.0 49 auth required pam_listfile.so item=group sense=allow file=/etc/security/allow_groups onerr=fail 50 51 新建一个组,组名为bobo,然后将kevin和grace添加到这个bobo组内 52 [root@docker-test1 ~]# groupadd bobo 53 [root@docker-test1 ~]# gpasswd -a kevin boboAdding user kevin to group bobo 54 Adding user kevin to group bobo 55 [root@docker-test1 ~]# usermod -G bobo grace 56 [root@docker-test1 ~]# id kevin 57 uid=1000(kevin) gid=1000(kevin)groups=1000(kevin),1002(bobo) 58 [root@docker-test1 ~]# id grace 59 uid=1001(grace) gid=1001(grace) groups=1001(grace),1002(bobo) 60 61 在/etc/security/allow_groups文件按中加入组名(注意如果不加root,则root就不能被允许登录了) 62 [root@docker-test1 ~]# vim /etc/security/allow_groupsbobo 63 bobo 64 65 [root@docker-test1 ~]# /etc/init.d/sshd restart 66 67 如上设置后,则只有kevin用户能被允许登录! 68 如果是禁止规则,则第一行改为下面内容: auth required pam_listfile.so item=group sense=deny file=/etc/security/deny_groups onerr=succeed