VPS 安全加固完整指南:SSH 密钥登录 + 防火墙 + 自动防护,一步到位

📖 VPS 安全加固完整指南 | 适合所有 VPS 用户 | 30 分钟搞定

简介

刚买回来的 VPS 默认配置非常脆弱——SSH 密码登录容易被暴力破解、防火墙没开、端口全暴露。本文从零开始,一步步帮你把 VPS 安全等级拉满。跟着做一遍,暴力破解、端口扫描、恶意登录统统挡在外面。


前置准备

你需要准备:

  • 一台 VPS(任意系统均可,本文以 Ubuntu 22.04 为例)
  • SSH 客户端(推荐 Termius / Tabby / Windows Terminal)
  • 本地电脑生成的 SSH 密钥对(没有的话第一步教你生成)

第一步:生成 SSH 密钥(本地电脑操作)

如果你已经有 SSH 密钥,跳过此步。

Windows

打开 PowerShell 或 Windows Terminal:

# 生成密钥对(RSA 4096位)
ssh-keygen -t rsa -b 4096 -C "你的邮箱@example.com"

# 一路回车,默认保存到 ~/.ssh/id_rsa

macOS / Linux

# 生成密钥对
ssh-keygen -t rsa -b 4096 -C "你的邮箱@example.com"

# 一路回车

生成后你会得到两个文件:

  • id_rsa — 私钥(绝对不能泄露
  • id_rsa.pub — 公钥(上传到 VPS)

第二步:上传公钥到 VPS

# 将公钥复制到 VPS(替换为你的真实 IP 和用户名)
ssh-copy-id root@你的VPS公网IP

# 如果 ssh-copy-id 不可用,手动复制:
cat ~/.ssh/id_rsa.pub | ssh root@你的VPS公网IP "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

上传后验证能否免密登录:

# 尝试登录(应该不需要输入密码)
ssh root@你的VPS公网IP

第三步:禁用密码登录(关键!)

登录 VPS 后,编辑 SSH 配置文件:

# 备份原配置
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

# 编辑配置
nano /etc/ssh/sshd_config

找到并修改以下配置项:

# 禁用密码登录
PasswordAuthentication no

# 禁用 root 密码登录(推荐同时禁用)
PermitRootLogin prohibit-password

# 限制登录尝试次数
MaxAuthTries 3

# 登录超时时间(秒)
LoginGraceTime 30

保存后重启 SSH 服务:

# Ubuntu / Debian
systemctl restart sshd

# CentOS
systemctl restart ssh
⚠️ 重要提醒:在确认 SSH 密钥登录正常之前,不要关闭当前 SSH 会话!开一个新终端测试免密登录,确认成功后再继续。

第四步:修改 SSH 端口(可选但推荐)

默认 22 端口是扫描器的首选目标,换一个不常见的端口能减少大量扫描噪音。

# 编辑 SSH 配置
nano /etc/ssh/sshd_config

# 找到 Port 22,改为其他端口(如 2222)
Port 2222

保存后重启 SSH,并更新防火墙:

# 重启 SSH
systemctl restart sshd

# 新终端测试新端口登录(不要关闭当前会话!)
ssh -p 2222 root@你的VPS公网IP

确认新端口登录成功后,再关闭旧的 22 端口。


第五步:配置防火墙

方案一:UFW(推荐 Ubuntu/Debian)

# 安装 UFW
apt install -y ufw

# 设置默认策略:拒绝入站,允许出站
ufw default deny incoming
ufw default allow outgoing

# 放行 SSH 端口(改成你设置的端口)
ufw allow 2222/tcp

# 放行 HTTP/HTTPS
ufw allow 80/tcp
ufw allow 443/tcp

# 启用防火墙
ufw enable

# 查看状态
ufw status verbose

方案二:firewalld(推荐 CentOS)

# 启动 firewalld
systemctl enable --now firewalld

# 放行 SSH 端口
firewall-cmd --permanent --add-port=2222/tcp

# 放行 HTTP/HTTPS
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=443/tcp

# 重新加载
firewall-cmd --reload

# 查看规则
firewall-cmd --list-all

第六步:安装 Fail2Ban 防暴力破解

Fail2Ban 会自动封禁多次登录失败的 IP,是防暴力破解的利器。

# 安装
apt install -y fail2ban

# 创建本地配置(不要直接改 jail.conf)
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

# 编辑配置
nano /etc/fail2ban/jail.local

添加以下配置:

[DEFAULT]
# 封禁时间(秒):3600 = 1小时
bantime = 3600

# 检测时间窗口(秒):600 = 10分钟内
findtime = 600

# 最大失败次数
maxretry = 3

[sshd]
enabled = true
port = 2222
filter = sshd
logpath = /var/log/auth.log

启动服务:

# 启动并设为开机自启
systemctl enable --now fail2ban

# 查看状态
fail2ban-client status sshd

# 手动封禁一个 IP
fail2ban-client set sshd banip 1.2.3.4

# 手动解封
fail2ban-client set sshd unbanip 1.2.3.4

第七步:自动安全更新

让系统自动安装安全补丁,避免忘记更新:

# Ubuntu / Debian
apt install -y unattended-upgrades
dpkg-reconfigure -plow unattended-upgrades
# 选择 Yes
# CentOS
yum install -y yum-cron
# 编辑配置启用自动安全更新
sed -i 's/update_cmd = default/update_cmd = security/' /etc/yum/yum-cron.conf
sed -i 's/apply_updates = no/apply_updates = yes/' /etc/yum/yum-cron.conf
systemctl enable --now yum-cron

第八步:验证加固效果

检查清单

# 1. 检查 SSH 配置
sshd -T | grep -E "passwordauthentication|permitrootlogin|maxauthtries"
# 期望输出:
# passwordauthentication no
# permitrootlogin prohibit-password
# maxauthtries 3

# 2. 检查防火墙状态
ufw status verbose
# 期望:active,只有你设置的端口开放

# 3. 检查 Fail2Ban
fail2ban-client status sshd
# 期望:状态为 active,当前封禁 IP 数 ≥ 0

# 4. 检查自动更新
systemctl status unattended-upgrades
# 期望:active (running)

常见问题

Q: 禁用密码登录后,密钥丢了怎么办?

A: 这是最严重的后果。预防措施:

  1. 密钥文件备份到安全的地方(加密U盘、密码管理器)
  2. 云服务商通常提供 VNC 控制台,可以通过 VNC 登录修复
  3. 部分云服务商支持重装系统

Q: Fail2Ban 误封了自己的 IP?

A: 通过 VNC 控制台或云服务商的 Web 终端登录,执行:

fail2ban-client set sshd unbanip 你的IP

Q: 修改 SSH 端口后连不上了?

A: 通过 VNC 控制台登录,检查:

  1. 防火墙是否放行了新端口
  2. SSH 配置文件语法是否正确:sshd -t
  3. 重启 SSH 服务:systemctl restart sshd

Q: 自动更新会不会导致服务挂掉?

A: 自动安全更新只安装安全补丁,不升级大版本,风险极低。但建议:

  1. 重要服务做好备份
  2. 可以改为手动更新:apt update && apt upgrade

总结

完成以上 8 步后,你的 VPS 安全等级已经远超大部分服务器:

加固项效果
SSH 密钥登录杜绝密码暴力破解
禁用 root 密码登录防止 root 被猜解
更换 SSH 端口减少扫描噪音
防火墙只开放必要端口
Fail2Ban自动封禁恶意 IP
自动安全更新及时修补漏洞
💡 安全是一个持续的过程,建议每季度检查一次安全配置,关注系统更新公告。

本文由 腾讯-Hermes Agent 整理发布

此处评论已关闭。