SSH 安全加固是保护服务器免受未授权访问和攻击的重要措施。通过合理的配置和最佳实践,可以显著提高 SSH 服务器的安全性。
基础安全配置
1. 修改默认端口
bash# /etc/ssh/sshd_config Port 2222 # 修改为非标准端口
优势:
- 减少自动化扫描和暴力破解攻击
- 降低日志噪音
- 增加攻击难度
2. 禁用 root 登录
bash# /etc/ssh/sshd_config PermitRootLogin no
最佳实践:
- 使用普通用户登录后通过 sudo 提权
- 限制 sudo 权限范围
- 定期审查 sudo 配置
3. 禁用密码认证
bash# /etc/ssh/sshd_config PasswordAuthentication no PubkeyAuthentication yes
实施步骤:
- 配置公钥认证
- 测试公钥登录
- 禁用密码认证
- 重启 SSH 服务
4. 限制登录用户
bash# /etc/ssh/sshd_config # 仅允许特定用户 AllowUsers user1 user2 # 仅允许特定组 AllowGroups sshusers # 拒绝特定用户 DenyUsers guest test # 拒绝特定组 DenyGroups nogroup
高级安全配置
1. 多因素认证
bash# /etc/ssh/sshd_config AuthenticationMethods publickey,keyboard-interactive # 或使用 Google Authenticator AuthenticationMethods publickey,keyboard-interactive:pam
配置 Google Authenticator:
bash# 安装 sudo apt-get install libpam-google-authenticator # 为用户配置 google-authenticator # 配置 PAM # /etc/pam.d/sshd auth required pam_google_authenticator.so
2. 连接限制
bash# /etc/ssh/sshd_config # 最大认证尝试次数 MaxAuthTries 3 # 最大会话数 MaxSessions 2 # 最大启动会话数 MaxStartups 10:30:100 # 登录超时时间 LoginGraceTime 60
3. 网络安全
bash# /etc/ssh/sshd_config # 仅监听特定地址 ListenAddress 192.168.1.100 ListenAddress 127.0.0.1 # 禁用端口转发 AllowTcpForwarding no GatewayPorts no # 禁用 X11 转发 X11Forwarding no # 禁用代理转发 AllowAgentForwarding no
4. 加密算法优化
bash# /etc/ssh/sshd_config # 使用安全的加密算法 Ciphers aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes256-ctr # 使用安全的密钥交换算法 KexAlgorithms curve25519-sha256@libssh.org,ecdh-sha2-nistp256 # 使用安全的 MAC 算法 MACs hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com # 禁用不安全的算法 # Ciphers -3des-cbc,-aes128-cbc,-aes192-cbc,-aes256-cbc
访问控制
1. TCP Wrappers
bash# /etc/hosts.allow sshd: 192.168.1.0/24 : ALLOW sshd: 10.0.0.0/8 : ALLOW # /etc/hosts.deny sshd: ALL : DENY
2. 防火墙配置
bash# 使用 iptables iptables -A INPUT -p tcp --dport 2222 -s 192.168.1.0/24 -j ACCEPT iptables -A INPUT -p tcp --dport 2222 -j DROP # 使用 ufw ufw allow from 192.168.1.0/24 to any port 2222 ufw deny 2222
3. Fail2Ban 集成
bash# 安装 Fail2Ban sudo apt-get install fail2ban # 配置 SSH 监控 # /etc/fail2ban/jail.local [sshd] enabled = true port = 2222 filter = sshd logpath = /var/log/auth.log maxretry = 3 bantime = 3600 findtime = 600
日志和监控
1. 详细日志配置
bash# /etc/ssh/sshd_config LogLevel VERBOSE # 记录登录信息 SyslogFacility AUTHPRIV
2. 日志分析
bash# 查看登录失败 grep "Failed password" /var/log/auth.log # 查看成功登录 grep "Accepted" /var/log/auth.log # 查看异常登录 grep "Invalid user" /var/log/auth.log
3. 实时监控
bash# 实时监控 SSH 连接 tail -f /var/log/auth.log | grep sshd # 监控活跃连接 watch "ss -tlnp | grep :2222"
密钥管理
1. 密钥轮换
bash# 定期生成新密钥 ssh-keygen -t ed25519 -f ~/.ssh/new_key -C "user@hostname" # 更新服务器上的公钥 ssh-copy-id -i ~/.ssh/new_key.pub user@server # 删除旧密钥 rm ~/.ssh/old_key
2. 密钥权限
bash# 设置正确的文件权限 chmod 700 ~/.ssh chmod 600 ~/.ssh/config chmod 600 ~/.ssh/id_rsa chmod 644 ~/.ssh/id_rsa.pub chmod 600 ~/.ssh/authorized_keys
3. 密钥撤销
bash# 从 authorized_keys 中删除特定密钥 sed -i '/old_key/d' ~/.ssh/authorized_keys # 或手动编辑 nano ~/.ssh/authorized_keys
定期维护
1. 系统更新
bash# 定期更新 SSH 软件 sudo apt-get update sudo apt-get upgrade openssh-server # 检查当前版本 ssh -V
2. 安全审计
bash# 检查 SSH 配置 sshd -T | grep -i "permitroot\|passwordauthentication" # 检查支持的算法 ssh -Q cipher ssh -Q kex ssh -Q mac # 使用 nmap 扫描 nmap --script ssh2-enum-algos -p 2222 hostname
3. 备份配置
bash# 备份 SSH 配置 sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup # 备份密钥 tar -czf ssh_keys_backup.tar.gz ~/.ssh/
最佳实践总结
- 最小权限原则:仅授予必要的访问权限
- 深度防御:多层安全控制
- 定期更新:保持软件和配置最新
- 监控审计:持续监控和定期审计
- 应急响应:制定安全事件响应计划
SSH 安全加固是一个持续的过程,需要根据具体环境和威胁模型进行调整和优化。