限制ssh的密码登录与登录IP
ssh 远程登录到服务器时,提示有 1000 多次失败登录:
There were 1429 failed login attempts since the last successful login.
对账户案例进行优化:
- 优化一:
启用证书登录,禁用ssh的密码登录,修改 /etc/ssh/sshd_config
PasswordAuthentication no
重启 sshd 服务
- 优化二:
限制允许登录的IP,通过 /etc/hosts.allow
设置允许访问的IP,通过 /etc/hosts.deny
设置不允许通过的IP,hosts.allow
的优先级 高于 hosts.deny
的优先级。
/etc/hosts.allow
:
# 允许所有IP
sshd:ALL
# 允许指定IP
sshd:10.77.0.1:allow
# 允许 10.77.0.1/24 这个IP地址的用户登录
sshd:10.77.0.:allow
# 指定多个IP段,逗号隔开
sshd:10.77.0.,10.77.1.:allow
/etc/hosts.deny
:
# 拒绝所有IP
sshd:ALL
在进行限制时,可以在 hosts.deny
中拒绝所有IP,然后在 hosts.allow
中放行需要登录的IP。
对于 hosts.allow
和 hosts.deny
的限制,直接修改文件即可,无需重启其它服务项。
开启限制后,切换未允许的网络尝试登录:
jason@Jason ~ % ssh dev
kex_exchange_identification: read: Connection reset by peer
jason@Jason ~ %