SSH作为远程管理服务器的核心协议,其安全性直接影响系统防护能力。本文提供7项专业级SSH安全加固方案,包括端口修改、密钥认证、防火墙策略等实战配置,帮助管理员有效抵御暴力破解和中间人攻击,同时保持运维效率。
一、修改默认SSH端口
默认22端口是自动化攻击的主要目标,建议更改为1024-65535之间的高位端口:
编辑SSH配置文件
sudo nano /etc/ssh/sshd_config
修改以下参数
Port 49213 自定义端口号
需同步更新防火墙规则并重启服务:
sudo ufw allow 49213/tcp
sudo systemctl restart sshd
二、禁用root直接登录
强制攻击者需要同时破解用户名和密码:
PermitRootLogin no
AllowUsers adminuser 指定允许登录的普通账户
三、启用密钥认证
相比密码认证,ECDSA密钥可提供更强的安全性:
PasswordAuthentication no
PubkeyAuthentication yes
HostKeyAlgorithms ecdsa-sha2-nistp384
四、配置入侵防御机制
使用fail2ban自动封禁恶意IP:
安装配置fail2ban
sudo apt install fail2ban
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
修改SSH防护配置
[sshd]
enabled = true
port = 49213 与SSH端口保持一致
maxretry = 3
bantime = 1h
五、启用双因素认证
通过Google Authenticator增加动态验证码层:
安装PAM模块
sudo apt install libpam-google-authenticator
google-authenticator 生成密钥
编辑SSH配置
ChallengeResponseAuthentication yes
AuthenticationMethods publickey,keyboard-interactive
六、限制访问源IP
企业环境建议结合防火墙做IP白名单:
使用TCP Wrappers
echo "sshd : 192.168.1.0/24" >> /etc/hosts.allow
echo "sshd : ALL" >> /etc/hosts.deny
或使用UFW防火墙
sudo ufw allow from 203.0.113.45 to any port 49213
七、定期更新加密算法
禁用已淘汰的弱加密协议:
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com
KexAlgorithms curve25519-sha256
MACs hmac-sha2-512-etm@openssh.com
最佳实践检查清单
- 每月检查/var/log/auth.log异常登录
- 使用ssh-audit工具检测配置漏洞
- 关键服务器配置证书过期提醒
- 备份authorized_keys文件到安全位置
评论