SSH 安全配置规范
2026/3/20大约 5 分钟
SSH 安全配置规范
注意
SSH 是服务器的大门,配置不当等于给黑客留了一把钥匙。每天都有无数机器人在扫描弱密码和默认配置。
核心原则
- 禁用密码登录:只允许密钥认证
- 禁止 root 远程登录:通过普通用户 + sudo 操作
- 限制访问来源:IP 白名单或 跳板机
- 启用防暴力破解:fail2ban 或类似工具
密钥认证配置
生成 SSH 密钥对(本地机器)
# 生成 Ed25519 密钥(推荐,更安全更短)
ssh-keygen -t ed25519 -C "your-email@example.com"
# 或生成 RSA 密钥(兼容性更好)
ssh-keygen -t rsa -b 4096 -C "your-email@example.com"
# 密钥会保存在:
# ~/.ssh/id_ed25519 (私钥,绝对保密)
# ~/.ssh/id_ed25519.pub (公钥,上传到服务器)
上传公钥到服务器
# 方式 1:使用 ssh-copy-id(推荐)
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server-ip
# 方式 2:手动复制
cat ~/.ssh/id_ed25519.pub | ssh user@server-ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
# 方式 3:直接在服务器上操作
# 登录服务器后:
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo "ssh-ed25519 AAAA... your-email@example.com" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
验证密钥登录
# 测试密钥登录
ssh -i ~/.ssh/id_ed25519 user@server-ip
# 如果成功,应该不需要输入密码
SSH 服务端配置
配置文件位置
/etc/ssh/sshd_config # 主配置文件
/etc/ssh/sshd_config.d/ # 配置片段目录(推荐)
推荐配置
创建 /etc/ssh/sshd_config.d/hardening.conf:
# /etc/ssh/sshd_config.d/hardening.conf
# SSH 安全加固配置
# ========== 认证设置 ==========
# 禁用密码认证(重要!)
PasswordAuthentication no
# 禁用空密码
PermitEmptyPasswords no
# 启用公钥认证
PubkeyAuthentication yes
# 禁止 root 远程登录
PermitRootLogin no
# 如果需要 root 仅限密钥登录(不推荐)
# PermitRootLogin prohibit-password
# ========== 安全设置 ==========
# 禁用 X11 转发
X11Forwarding no
# 禁用 TCP 转发(如不需要)
AllowTcpForwarding no
# 禁用 Agent 转发
AllowAgentForwarding no
# 禁用 .rhosts 文件
IgnoreRhosts yes
# 禁用基于主机的认证
HostbasedAuthentication no
# ========== 连接设置 ==========
# 登录超时时间
LoginGraceTime 30
# 最大认证尝试次数
MaxAuthTries 3
# 最大并发未认证连接
MaxStartups 10:30:60
# 客户端存活检测
ClientAliveInterval 300
ClientAliveCountMax 2
# ========== 日志设置 ==========
# 日志级别
LogLevel VERBOSE
# ========== 访问控制 ==========
# 只允许特定用户登录
AllowUsers deploy admin
# 或只允许特定组
# AllowGroups ssh-users
# 禁止特定用户
# DenyUsers baduser
# ========== 协议设置 ==========
# 只使用 SSH 协议 2
Protocol 2
# 使用更安全的加密算法
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com
MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com
KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org
应用配置
# 验证配置语法
sudo sshd -t
# 重新加载配置(不断开现有连接)
sudo systemctl reload sshd
# 或重启服务
sudo systemctl restart sshd
[!danger]
修改 SSH 配置前,务必保持一个已登录的终端窗口!如果配置错误,可以用它来恢复。
修改 SSH 端口(可选)
修改端口
# 编辑配置
sudo vim /etc/ssh/sshd_config.d/port.conf
# 添加内容
Port 22222
# 如果使用 SELinux(CentOS/RHEL)
sudo semanage port -a -t ssh_port_t -p tcp 22222
# 开放新端口的防火墙
sudo ufw allow 22222/tcp
# 重新加载 SSH
sudo systemctl reload sshd
# 测试新端口(新开一个终端)
ssh -p 22222 user@server-ip
# 确认成功后,关闭旧端口
sudo ufw delete allow 22/tcp
本地 SSH 配置
# ~/.ssh/config
Host myserver
HostName server-ip
Port 22222
User deploy
IdentityFile ~/.ssh/id_ed25519
# 使用时只需
ssh myserver
Fail2Ban 防暴力破解
安装
# Ubuntu/Debian
sudo apt install fail2ban
# CentOS/RHEL
sudo yum install fail2ban
配置
创建 /etc/fail2ban/jail.local:
[DEFAULT]
# 封禁时间(秒)
bantime = 3600
# 检测时间窗口
findtime = 600
# 最大失败次数
maxretry = 3
# 忽略的 IP(你的固定 IP)
ignoreip = 127.0.0.1/8 ::1 192.168.1.0/24
# 封禁动作
banaction = ufw
[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600
操作命令
# 启动服务
sudo systemctl enable --now fail2ban
# 查看状态
sudo fail2ban-client status
# 查看 SSH 防护状态
sudo fail2ban-client status sshd
# 手动解封 IP
sudo fail2ban-client set sshd unbanip 1.2.3.4
# 手动封禁 IP
sudo fail2ban-client set sshd banip 1.2.3.4
# 查看封禁列表
sudo fail2ban-client get sshd banned
SSH 多密钥管理
场景:不同项目使用不同密钥
# 生成多个密钥
ssh-keygen -t ed25519 -f ~/.ssh/id_work -C "work"
ssh-keygen -t ed25519 -f ~/.ssh/id_personal -C "personal"
ssh-keygen -t ed25519 -f ~/.ssh/id_github -C "github"
配置文件
# ~/.ssh/config
# 工作服务器
Host work-*
IdentityFile ~/.ssh/id_work
User deploy
Host work-prod
HostName prod.company.com
Host work-staging
HostName staging.company.com
# 个人服务器
Host personal
HostName my-server.com
User admin
IdentityFile ~/.ssh/id_personal
# GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_github
常见问题排查
无法登录检查步骤
# 1. 检查 SSH 服务是否运行
sudo systemctl status sshd
# 2. 检查配置语法
sudo sshd -t
# 3. 查看详细日志
sudo tail -f /var/log/auth.log
# 4. 客户端调试模式
ssh -vvv user@server
# 5. 检查密钥权限
ls -la ~/.ssh/
# 正确权限:
# drwx------ .ssh/ (700)
# -rw------- id_ed25519 (600)
# -rw-r--r-- id_ed25519.pub (644)
# -rw------- authorized_keys (600)
# 6. 检查 SELinux(CentOS/RHEL)
sudo restorecon -R ~/.ssh
权限修复
# 修复客户端权限
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_*
chmod 644 ~/.ssh/id_*.pub
chmod 644 ~/.ssh/config
chmod 644 ~/.ssh/known_hosts
# 修复服务端权限
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
"Too many authentication failures" 错误
# 指定使用的密钥,避免尝试所有密钥
ssh -i ~/.ssh/id_ed25519 user@server
# 或在配置中指定
Host server
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
实战脚本
SSH 安全加固一键脚本
#!/bin/bash
# harden-ssh.sh - SSH 安全加固脚本
set -e
echo "=== SSH 安全加固 ==="
# 备份原配置
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
# 创建加固配置
cat > /etc/ssh/sshd_config.d/hardening.conf << 'EOF'
# 禁用密码认证
PasswordAuthentication no
PermitEmptyPasswords no
# 启用公钥认证
PubkeyAuthentication yes
# 禁止 root 远程登录
PermitRootLogin no
# 安全设置
X11Forwarding no
AllowTcpForwarding no
ClientAliveInterval 300
ClientAliveCountMax 2
MaxAuthTries 3
LoginGraceTime 30
# 日志
LogLevel VERBOSE
EOF
# 验证配置
if sshd -t; then
echo "配置验证通过"
systemctl reload sshd
echo "SSH 服务已重新加载"
else
echo "配置有误,已回滚"
rm /etc/ssh/sshd_config.d/hardening.conf
exit 1
fi
echo "=== SSH 加固完成 ==="
echo "请确保你已经配置了密钥登录!"