2026年3月10日

SSH 安全配置指南 - 保护服务器安全

SSH 是远程管理服务器的主要方式,配置不当可能导致服务器被入侵。本文介绍 SSH 的安全配置方法。

SSH 基础

什么是 SSH

SSH(Secure Shell)是一种加密的网络传输协议,用于在不安全的网络中安全地传输数据。

连接方式

# 密码连接
ssh user@192.168.1.100

# 指定端口
ssh -p 2222 user@192.168.1.100

# 使用密钥
ssh -i ~/.ssh/id_rsa user@192.168.1.100

密钥认证配置

生成密钥对

# 生成 RSA 密钥(默认)
ssh-keygen -t rsa -b 4096

# 生成 Ed25519 密钥(推荐)
ssh-keygen -t ed25519 -C "your@email.com"

# 指定文件名
ssh-keygen -t ed25519 -f ~/.ssh/my_key

# 设置密码短语
# 建议设置,增加安全性

密钥类型对比

类型 安全性 性能 兼容性
RSA 4096 一般 最好
Ed25519 最高 最快 较好
ECDSA 一般

部署公钥

# 方法1:使用 ssh-copy-id
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server

# 方法2:手动复制
cat ~/.ssh/id_ed25519.pub | ssh user@server "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

# 方法3:手动编辑
# 将公钥内容添加到服务器的 ~/.ssh/authorized_keys

设置权限

# 服务器端权限设置
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod 600 ~/.ssh/id_ed25519      # 私钥
chmod 644 ~/.ssh/id_ed25519.pub  # 公钥

SSH 配置文件

配置文件位置

  • 服务端:/etc/ssh/sshd_config
  • 客户端:~/.ssh/config

安全配置项

# 编辑配置文件
sudo vim /etc/ssh/sshd_config
# 禁用 root 登录
PermitRootLogin no

# 禁用密码认证
PasswordAuthentication no
PubkeyAuthentication yes

# 修改默认端口
Port 2222

# 限制登录用户
AllowUsers user1 user2
AllowGroups ssh-users

# 禁止空密码
PermitEmptyPasswords no

# 限制认证尝试
MaxAuthTries 3

# 登录超时
LoginGraceTime 60

# 断开空闲连接
ClientAliveInterval 300
ClientAliveCountMax 2

# 禁用 X11 转发
X11Forwarding no

# 禁用端口转发
AllowTcpForwarding no

# 只使用 SSH2 协议
Protocol 2

# 禁用 DNS 解析
UseDNS no

# 限制密钥类型
PubkeyAcceptedKeyTypes ssh-ed25519,ssh-ed25519-cert-v01@openssh.com,ssh-rsa,ssh-rsa-cert-v01@openssh.com

重启服务

# 检查配置语法
sudo sshd -t

# 重启 SSH 服务
sudo systemctl restart sshd

客户端配置

SSH Config 文件

# 编辑配置
vim ~/.ssh/config
# 通用配置
Host *
    ServerAliveInterval 60
    ServerAliveCountMax 3
    TCPKeepAlive yes

# 服务器配置
Host myserver
    HostName 192.168.1.100
    Port 2222
    User admin
    IdentityFile ~/.ssh/id_ed25519
    
# 跳板机配置
Host jump
    HostName jump.example.com
    User jumpuser

Host target
    HostName 10.0.0.100
    User targetuser
    ProxyJump jump

# 多跳配置
Host final
    HostName 10.0.0.200
    User finaluser
    ProxyJump jump,target

使用别名连接

# 配置后可以直接使用别名
ssh myserver

# 代替
ssh -p 2222 -i ~/.ssh/id_ed25519 admin@192.168.1.100

防暴力破解

Fail2ban 安装

# Ubuntu/Debian
sudo apt install fail2ban

# CentOS/RHEL
sudo yum install fail2ban

# 启动服务
sudo systemctl enable fail2ban
sudo systemctl start fail2ban

Fail2ban 配置

# 创建本地配置
sudo vim /etc/fail2ban/jail.local
[DEFAULT]
bantime = 3600
findtime = 600
maxretry = 3
ignoreip = 127.0.0.1/8 ::1 192.168.1.0/24

[sshd]
enabled = true
port = ssh,2222
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 86400

Fail2ban 命令

# 查看状态
sudo fail2ban-client status sshd

# 解封 IP
sudo fail2ban-client set sshd unbanip 192.168.1.100

# 查看日志
sudo tail -f /var/log/fail2ban.log

端口敲门

安装 knockd

sudo apt install knockd

配置敲门

# /etc/knockd.conf
[openSSH]
    sequence    = 7000,8000,9000
    seq_timeout = 5
    command     = /sbin/iptables -A INPUT -s %IP% -p tcp --dport 2222 -j ACCEPT
    tcpflags    = syn

[closeSSH]
    sequence    = 9000,8000,7000
    seq_timeout = 5
    command     = /sbin/iptables -D INPUT -s %IP% -p tcp --dport 2222 -j ACCEPT
    tcpflags    = syn

客户端敲门

# 安装 knock
sudo apt install knockd

# 敲门打开端口
knock 192.168.1.100 7000 8000 9000

# 连接 SSH
ssh user@192.168.1.100 -p 2222

# 关闭端口
knock 192.168.1.100 9000 8000 7000

双因素认证

安装 Google Authenticator

# Ubuntu/Debian
sudo apt install libpam-google-authenticator

# 运行配置
google-authenticator

配置 PAM

# 编辑 PAM 配置
sudo vim /etc/pam.d/sshd

# 添加
auth required pam_google_authenticator.so

配置 SSH

# 编辑 SSH 配置
sudo vim /etc/ssh/sshd_config

# 添加
ChallengeResponseAuthentication yes
AuthenticationMethods publickey,keyboard-interactive

重启服务

sudo systemctl restart sshd

限制登录

限制 IP 访问

# 使用 hosts.allow 和 hosts.deny
# /etc/hosts.allow
sshd: 192.168.1.0/24 : allow
sshd: 10.0.0.100 : allow

# /etc/hosts.deny
sshd: ALL : deny

使用 iptables

# 只允许特定 IP
iptables -A INPUT -p tcp -s 192.168.1.0/24 --dport 2222 -j ACCEPT
iptables -A INPUT -p tcp --dport 2222 -j DROP

# 限制连接频率
iptables -A INPUT -p tcp --dport 2222 -m connlimit --connlimit-above 3 -j DROP

使用 UFW

# 允许特定 IP
sudo ufw allow from 192.168.1.0/24 to any port 2222

# 启用防火墙
sudo ufw enable

日志监控

查看登录日志

# 查看登录尝试
sudo tail -f /var/log/auth.log

# 查看失败登录
sudo grep "Failed password" /var/log/auth.log

# 查看成功登录
sudo grep "Accepted password" /var/log/auth.log
sudo grep "Accepted publickey" /var/log/auth.log

# 统计失败登录 IP
sudo grep "Failed password" /var/log/auth.log | awk '{print $(NF-3)}' | sort | uniq -c | sort -nr

日志配置

# /etc/ssh/sshd_config
SyslogFacility AUTH
LogLevel VERBOSE

SSH 代理转发

启用代理转发

# 客户端配置
# ~/.ssh/config
Host server
    ForwardAgent yes

使用场景

# 通过跳板机连接目标服务器
ssh -A jump-server
ssh target-server

安全注意

代理转发有安全风险,只在必要时启用。

SSH 隧道

本地端口转发

# 将远程服务映射到本地
ssh -L 8080:localhost:80 user@server

# 访问内网服务
ssh -L 3306:db.internal:3306 user@jump-server

远程端口转发

# 将本地服务暴露到远程
ssh -R 8080:localhost:80 user@server

动态端口转发(SOCKS代理)

# 创建 SOCKS 代理
ssh -D 1080 user@server

# 配置浏览器使用代理
# SOCKS5: 127.0.0.1:1080

安全检查清单

基础安全

  • 禁用 root 登录
  • 使用密钥认证
  • 禁用密码认证
  • 修改默认端口
  • 限制登录用户

进阶安全

  • 安装 Fail2ban
  • 配置防火墙规则
  • 启用双因素认证
  • 配置登录通知
  • 定期审计日志

配置检查

# 检查配置
sudo sshd -T

# 检查监听端口
sudo ss -tlnp | grep sshd

# 检查登录用户
who
w
last

常见问题

Q: 密钥登录失败?

检查:

  1. 权限是否正确
  2. 公钥是否正确添加
  3. SSH 配置是否启用密钥认证

Q: 如何测试配置?

# 测试配置语法
sudo sshd -t

# 详细模式连接
ssh -vvv user@server

Q: 如何恢复访问?

如果配置错误导致无法登录:

  1. 通过控制台/VNC 登录
  2. 恢复配置文件
  3. 重启 SSH 服务

总结

SSH 安全配置是服务器安全的基础。建议:

  1. 使用密钥认证替代密码
  2. 禁用 root 登录
  3. 修改默认端口
  4. 安装 Fail2ban
  5. 定期检查日志

安全是一个持续的过程,需要定期审查和更新配置。