在CentOS上配置SSH安全策略,可以通过以下步骤来实现:
首先,确保你的系统是最新的:
sudo yum update -y
使用firewalld来管理防火墙规则:
sudo yum install firewalld -y
sudo systemctl start firewalld
sudo systemctl enable firewalld
允许SSH端口(默认22)通过防火墙:
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
编辑SSH配置文件/etc/ssh/sshd_config:
sudo vi /etc/ssh/sshd_config
进行以下配置更改:
更改默认端口(可选):
Port 2222
禁用root登录:
PermitRootLogin no
使用公钥认证(推荐):
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
限制用户登录:
AllowUsers your_username
启用SSH密钥认证(如果尚未启用):
PasswordAuthentication no
设置连接超时:
ClientAliveInterval 300
ClientAliveCountMax 2
限制SSH访问来源(可选):
ListenAddress 0.0.0.0
应用配置更改:
sudo systemctl restart sshd
如果你启用了SELinux,可能需要调整一些策略:
sudo setsebool -P ssh_sysadm_login on
sudo setsebool -P ssh_keysign on
安装Fail2Ban:
sudo yum install fail2ban -y
启动并启用Fail2Ban服务:
sudo systemctl start fail2ban
sudo systemctl enable fail2ban
配置Fail2Ban以监控SSH登录尝试:
sudo vi /etc/fail2ban/jail.local
添加以下内容:
[sshd]
enabled = true
port = sshd
filter = sshd
logpath = /var/log/secure
maxretry = 3
bantime = 600
定期检查系统更新和安全补丁,并及时应用:
sudo yum update -y
通过以上步骤,你可以显著提高CentOS上SSH服务的安全性。