环境:CentOS 7.0
引用:
https://blog.csdn.net/qq_34886247/article/details/81351175

方法一:
首先需要限制登录的ip(外网ip)
XXX为外网IP
Vim /etc/hosts.allow
输入

sshd:XXX.XXX.XXX.XXX:allow
vim /etc/hosts.deny
输入(表示除了上面允许的,其他的ip 都拒绝登陆ssh)

sshd:ALL
最后sshd重启
 /bin/systemctl restart sshd.service

方法二:
比如说你只允许XXX.XXX.XXX.XXX这个IP进入,其它都禁止:
vim /etc/ssh/sshd_config
添加一行:
allowusers xxx@XXX.XXX.XXX.XXX
注:xxx为你用来登入服务器的用户名

最后sshd重启

 /bin/systemctl restart sshd.service