SSH 安全配置最佳实践
使用密钥认证替代密码登录 # 本地生成密钥对(推荐 ed25519) ssh-keygen -t ed25519 -C "your_email@example.com" # 上传公钥到服务器 ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server /etc/ssh/sshd_config 关键配置 # 禁用密码登录,强制密钥认证 PasswordAuthentication no PermitEmptyPasswords no # 禁止 root 远程登录 PermitRootLogin no # 更改默认端口,降低自动化扫描命中率(非核心防护手段,只是降噪) Port 22022 # 限制协议版本与登录尝试 Protocol 2 MaxAuthTries 3 LoginGraceTime 30 # 仅允许特定用户/组登录 AllowUsers deploy admin 修改后重启服务: sudo systemctl restart sshd 注意:修改端口或禁用密码登录前,务必保留一个已登录的会话窗口,避免配置错误导致无法远程访问。 使用 fail2ban 防暴力破解 sudo apt install fail2ban # /etc/fail2ban/jail.local [sshd] enabled = true port = 22022 maxretry = 5 bantime = 3600 findtime = 600 sudo systemctl enable --now fail2ban sudo fail2ban-client status sshd 双因素认证(可选增强) 对高安全要求场景,可结合 libpam-google-authenticator 实现 SSH 二次验证,在密钥认证基础上再叠加一层动态口令。 ...