SSH 安全配置
SSH 是 Linux 服务器远程管理的主要方式,也是攻击者最常尝试突破的入口。本文将系统性地介绍 SSH 安全加固的各项措施,从密钥认证到 Fail2Ban 防暴力破解,帮助你构建安全可靠的远程访问环境。
使用密钥认证替代密码登录
Section titled “使用密钥认证替代密码登录”密钥认证比密码认证更安全且更方便。配置完成后,应完全禁用密码登录。
在客户端生成密钥对
Section titled “在客户端生成密钥对”ssh-keygen -t ed25519 -C "your_email@example.com"按提示操作,建议设置密钥密码短语(passphrase)以增加安全层:
Generating public/private ed25519 key pair.Enter file in which to save the key (/home/user/.ssh/id_ed25519): 回车Enter passphrase (empty for no passphrase): 输入密码短语Enter same passphrase again: 再次输入将公钥传输到服务器
Section titled “将公钥传输到服务器”ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server_ip或者手动复制:
cat ~/.ssh/id_ed25519.pub | ssh user@server_ip "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"验证密钥登录
Section titled “验证密钥登录”ssh user@server_ip如果不再提示输入密码(或只需输入密钥密码短语),说明密钥认证已生效。
加固 sshd_config
Section titled “加固 sshd_config”SSH 服务的主配置文件位于 /etc/ssh/sshd_config。每次修改后需要重启 sshd 服务。
备份原始配置
Section titled “备份原始配置”sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak禁用 root 直接登录
Section titled “禁用 root 直接登录”sudo vi /etc/ssh/sshd_config找到或添加:
PermitRootLogin no禁用密码认证
Section titled “禁用密码认证”确认密钥登录正常后,禁用密码认证:
PasswordAuthentication no同时禁用其他不安全的认证方式:
ChallengeResponseAuthentication noKbdInteractiveAuthentication no更改 SSH 端口
Section titled “更改 SSH 端口”将 SSH 端口从默认的 22 改为其他端口,可以大幅减少自动化扫描攻击:
Port 2222更改端口后需要调整 SELinux 和防火墙:
# 允许 SELinux 使用新端口sudo semanage port -a -t ssh_port_t -p tcp 2222
# 防火墙放行新端口sudo firewall-cmd --permanent --add-port=2222/tcpsudo firewall-cmd --permanent --remove-service=sshsudo firewall-cmd --reload配置 AllowUsers 用户白名单
Section titled “配置 AllowUsers 用户白名单”只允许指定用户通过 SSH 登录:
AllowUsers admin deploy也可以限制来源 IP:
AllowUsers admin@192.168.1.0/24 deploy@10.0.0.0/8类似地,可以使用 AllowGroups 按组授权:
AllowGroups sshusers# 创建组并添加用户sudo groupadd sshuserssudo usermod -aG sshusers adminsudo usermod -aG sshusers deploy其他安全建议
Section titled “其他安全建议”# 禁用空密码登录PermitEmptyPasswords no
# 设置最大认证尝试次数MaxAuthTries 3
# 设置登录超时时间(秒)LoginGraceTime 30
# 禁用 X11 转发(如不需要图形界面)X11Forwarding no
# 指定支持的密钥交换算法(移除弱算法)KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org
# 指定支持的加密算法Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com
# 指定支持的 MAC 算法MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com
# 设置客户端活动检测间隔ClientAliveInterval 300ClientAliveCountMax 2
# 显示上次登录信息PrintLastLog yes
# 使用 SSH 协议 2(现代系统默认)Protocol 2检查配置语法:
sudo sshd -t如果无输出表示语法正确,重启服务:
sudo systemctl restart sshdSSH 配置最佳实践模板
Section titled “SSH 配置最佳实践模板”以下是一份推荐的完整安全配置:
# /etc/ssh/sshd_config 安全配置Port 2222Protocol 2
# 认证设置PermitRootLogin noPasswordAuthentication noPermitEmptyPasswords noChallengeResponseAuthentication noKbdInteractiveAuthentication noPubkeyAuthentication yesAuthorizedKeysFile .ssh/authorized_keys
# 用户控制AllowUsers admin deployMaxAuthTries 3LoginGraceTime 30
# 安全增强X11Forwarding noAllowTcpForwarding noAllowAgentForwarding no
# 会话管理ClientAliveInterval 300ClientAliveCountMax 2
# 日志LogLevel VERBOSE
# 算法KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.orgCiphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.comMACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com安装和配置 Fail2Ban
Section titled “安装和配置 Fail2Ban”Fail2Ban 监控日志文件,自动封禁多次认证失败的 IP 地址,是防暴力破解的利器。
安装 Fail2Ban
Section titled “安装 Fail2Ban”sudo dnf install epel-release -ysudo dnf install fail2ban -ysudo systemctl enable --now fail2ban配置 Fail2Ban
Section titled “配置 Fail2Ban”不要直接修改 /etc/fail2ban/jail.conf,应创建本地覆盖配置:
sudo vi /etc/fail2ban/jail.local添加如下配置:
[DEFAULT]# 封禁时间(秒),-1 为永久封禁bantime = 3600
# 检测时间窗口(秒)findtime = 600
# 最大失败次数maxretry = 3
# 封禁动作(使用 firewalld)banaction = firewallcmd-rich-rulesbanaction_allports = firewallcmd-rich-rules
# 通知邮箱(可选)destemail = admin@example.comsender = fail2ban@example.comaction = %(action_mwl)s
[sshd]enabled = trueport = 2222logpath = /var/log/securebackend = systemdmaxretry = 3bantime = 3600重启 Fail2Ban
Section titled “重启 Fail2Ban”sudo systemctl restart fail2ban查看 Fail2Ban 状态
Section titled “查看 Fail2Ban 状态”# 查看总体状态sudo fail2ban-client status
# 查看 SSH jail 详情sudo fail2ban-client status sshd输出示例:
Status for the jail: sshd|- Filter| |- Currently failed: 2| |- Total failed: 15| `- File list: /var/log/secure`- Actions |- Currently banned: 1 |- Total banned: 3 `- Banned IP list: 203.0.113.50管理封禁的 IP
Section titled “管理封禁的 IP”# 手动解封 IPsudo fail2ban-client set sshd unbanip 203.0.113.50
# 手动封禁 IPsudo fail2ban-client set sshd banip 203.0.113.100
# 查看所有封禁的 IPsudo fail2ban-client banned完整加固流程
Section titled “完整加固流程”-
生成并部署 SSH 密钥:
Terminal window # 客户端执行ssh-keygen -t ed25519 -C "admin key"ssh-copy-id -i ~/.ssh/id_ed25519.pub admin@server_ip -
验证密钥登录正常后,加固 sshd 配置:
Terminal window # 服务端执行sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.baksudo tee /etc/ssh/sshd_config.d/hardening.conf <<'EOF'Port 2222PermitRootLogin noPasswordAuthentication noPermitEmptyPasswords noChallengeResponseAuthentication noMaxAuthTries 3LoginGraceTime 30AllowUsers adminX11Forwarding noClientAliveInterval 300ClientAliveCountMax 2LogLevel VERBOSEEOF -
调整 SELinux 和防火墙:
Terminal window sudo semanage port -a -t ssh_port_t -p tcp 2222sudo firewall-cmd --permanent --add-port=2222/tcpsudo firewall-cmd --reload -
检查配置并重启 SSH:
Terminal window sudo sshd -tsudo systemctl restart sshd -
用新端口测试连接(保持旧会话不关闭):
Terminal window # 新终端执行ssh -p 2222 admin@server_ip -
确认新端口正常后,关闭旧端口:
Terminal window sudo firewall-cmd --permanent --remove-service=sshsudo firewall-cmd --reload -
安装配置 Fail2Ban:
Terminal window sudo dnf install epel-release fail2ban -ysudo tee /etc/fail2ban/jail.local <<'EOF'[DEFAULT]bantime = 3600findtime = 600maxretry = 3banaction = firewallcmd-rich-rules[sshd]enabled = trueport = 2222backend = systemdEOFsudo systemctl enable --now fail2bansudo fail2ban-client status sshd
完成以上步骤后,SSH 服务将具备多层防护:密钥认证、非标准端口、用户白名单以及自动封禁暴力破解 IP。这些措施叠加使用可以显著提升服务器的安全性。