此教程的作用:设置SSH只允许特定用户从特定的IP登录,其它未经允许的用户和IP都不能登录
推荐使用下面的方法
1.编辑文件 /etc/ssh/sshd_config vi /etc/ssh/sshd_config
2.root用户只允许在如下ip登录 AllowUsers root@203.212.4.117
3.重启ssh生效
systemctl restart sshd
4.取消ip登录限制
如果取消ip登录限制,则删除AllowUsers项即可
示例1:只允许192.168.0.222登录192.168.1.81
# vim /etc/hosts.allow,最后一行加入:
sshd:192.168.0.222:allow //多个IP可以按照此格式写多行
# vim /etc/hosts.deny,最后一行加入:
sshd:ALL //除了上面允许登录的IP,其它IP都拒绝登录
# systemctl restart sshd
示例2:只允许192.168.1网段的主机登录192.168.1.81
# vim /etc/hosts.allow,最后一行加入:
sshd:192.168.1.*:allow
# vim /etc/hosts.deny,最后一行加入:
sshd:ALL
# systemctl restart sshd
示例3:只允许192.168.0.222以keyso用户身份、192.168.1.135以root用户身份登录192.168.1.81
# vim /etc/ssh/sshd_config,最后一行加入:
AllowUsers keyso@192.168.0.222 root@192.168.1.135 //多个用户名@IP之间使用空格分隔
# systemctl restart sshd