在CentOS系统中,防止SSH暴力破解的方法有很多种。以下是一些建议:
使用防火墙限制访问: 使用iptables或firewalld限制允许访问SSH的IP地址。例如,使用firewalld:
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="允许的IP地址" port protocol="tcp" port="22" accept'
sudo firewall-cmd --reload
更改默认SSH端口:
编辑/etc/ssh/sshd_config
文件,将默认的SSH端口(22)更改为其他端口。
Port 2222
然后重启SSH服务:
sudo systemctl restart sshd
使用fail2ban限制登录尝试次数: 安装fail2ban:
sudo yum install epel-release
sudo yum install fail2ban
创建一个新的fail2ban配置文件:
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
编辑/etc/fail2ban/jail.local
文件,启用并配置相应的参数:
[sshd]
enabled = true
port = 2222
filter = sshd
logpath = /var/log/secure
maxretry = 3
bantime = 600
启动fail2ban服务:
sudo systemctl start fail2ban
sudo systemctl enable fail2ban
使用公钥认证:
禁用密码登录,改为使用SSH密钥对进行身份验证。编辑/etc/ssh/sshd_config
文件:
PasswordAuthentication no
PubkeyAuthentication yes
然后重启SSH服务:
sudo systemctl restart sshd
使用SSH保持连接活跃:
在/etc/ssh/sshd_config
文件中设置以下参数,以防止因网络波动导致的连接中断:
ClientAliveInterval 60
ClientAliveCountMax 3
然后重启SSH服务:
sudo systemctl restart sshd
通过以上方法,可以有效地防止SSH暴力破解。请根据您的实际需求选择合适的方法进行配置。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:如何防止CentOS SSH被暴力破解