前 30 分钟:VPS 加固检查清单
一台新 VPS 在获得 IP 后几分钟内就会收到首次 SSH 探测。以下是我在每台新机器上执行的精确步骤——适用于 Debian 13,可直接复制粘贴,末尾附有检查清单表格。
前提:从你的 VPSRento 部署中获取的全新 Debian 13 镜像,你以 root 身份通过 SSH 登录,示例 IP 为 185.220.101.14 — 请替换为你自己的。总耗时约半小时。按顺序执行步骤;第 2 步如果操之过急可能会把你锁在外面。
1. 创建你的 sudo 用户
以 root 身份操作是让拼写错误变成事故的根源。创建一个真实用户,赋予 sudo 权限,然后从该用户开始所有操作:
# create the user (you'll set a password — still useful for sudo)
adduser ops
# grant sudo
usermod -aG sudo ops
以下所有内容均以 ops 身份运行,除非另有说明。
2. 仅使用 SSH 密钥——无密码,无 root 登录
密码认证是面向互联网的机器上最大的漏洞。机器人全天候用凭据列表猛攻 22 端口。密钥让整个攻击类别变得无关紧要。在 你的本地机器上:
# generate a keypair if you don't have one (ed25519, no passphrase tradeoff is yours)
ssh-keygen -t ed25519 -C "ops@atlas"
# push the public key to the server
ssh-copy-id [email protected]
在 新 终端 — ssh [email protected] — 然后再修改配置。保持现有的 root 会话打开作为逃生通道。然后编辑 /etc/ssh/sshd_config:
# /etc/ssh/sshd_config — the three lines that matter
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin prohibit-password
# validate the config, then reload (Debian 13 uses ssh.service)
sshd -t && systemctl reload ssh
prohibit-password 而不是一个扁平的 no 特意为 root 设置:root 仍然可以用你工作站的密钥进行认证,这在提供商控制台恢复时偶尔有用——但任何密码都不会再有效,对任何人、从任何地方都不行。用 sshd -T | grep -E 'passwordauthentication|permitrootlogin'.
确认。
3. 防火墙:默认拒绝,只允许你运行的服务
apt update && apt install -y ufw
ufw default deny incoming
ufw default allow outgoing
# SSH stays open — add this BEFORE enabling
ufw allow 22/tcp comment 'SSH'
# whatever your box actually serves, e.g.:
ufw allow 80/tcp comment 'HTTP'
ufw allow 443/tcp comment 'HTTPS'
ufw enable
ufw status verbose
Debian 的 ufw 是 nftables 之上一个精简、合理的层。策略才是安全核心;规则只是策略的例外:
你打开的每个端口都应有名称和理由。如果你说不出一个端口是干什么的,它就不该有规则。
4. fail2ban:把机器人拖慢到爬行
apt install -y fail2ban
# /etc/fail2ban/jail.local
[sshd]
enabled = true
port = ssh
maxretry = 5
findtime = 10m
bantime = 1h
systemctl enable --now fail2ban
# watch it work
fail2ban-client status sshd
禁用密码认证后,暴力破解已经失效——但 fail2ban 能保持日志可读,并封禁扫描其他一切内容的探测者:
5. 自动安全更新
apt install -y unattended-upgrades
# answer Yes to auto-install updates
dpkg-reconfigure -plow unattended-upgrades
未修补的 CVE 比重启风险更大。Debian 的 unattended-upgrades 会自动安装安全更新: /etc/apt/apt.conf.d/50unattended-upgrades 中的默认配置只从安全套件拉取——这正是服务器上你想要的:安全修复自动进行,其他一切由你选择时机。
6. 关闭你不运行的服务
每个运行中的服务都是你必须修补的攻击面。看看实际在监听什么:
# who is listening, on what, as which process
ss -tulpn
# everything enabled at boot
systemctl list-unit-files --state=enabled
# example: you don't run a print server — nobody does
systemctl disable --now cups
规则:如果你说不出某个监听器是干什么的,在让它存活之前先搞清楚。在最小化的 VPSRento 镜像上,除了 sshd 和你自己的应用之外,应该几乎没有其他东西。
7. 可选但推荐:WireGuard 管理平面
SSH 安全的最强形态是让 SSH 根本不面向互联网。在服务器上运行 WireGuard,仅允许通过隧道进行 SSH 访问,端口 22 将从全球所有扫描器中消失:
apt install -y wireguard
# /etc/wireguard/wg0.conf — minimal management tunnel
# [Interface] Address = 10.8.0.1/24, ListenPort = 51820
# open the tunnel, not the shell
ufw allow 51820/udp comment 'WireGuard'
ufw allow in on wg0 to any port 22 proto tcp comment 'SSH via WG'
ufw delete allow 22/tcp
systemctl enable --now wg-quick@wg0
之后,您可以通过隧道执行 ssh [email protected] ,而公网 IP 仅在单个 UDP 端口上响应。完整的 WireGuard 指南——密钥、对端、客户端配置、手机二维码——请参阅 配套指南.
10 点检查清单
每次新部署后,以及每次添加服务后,请逐项核对此表:
| # | 项目 | 所在位置 | 验证命令 |
|---|---|---|---|
| 1 | 非 root sudo 用户 | /etc/passwd | id ops |
| 2 | 仅密钥 SSH 认证 | sshd_config | sshd -T | grep passwordauth |
| 3 | 禁用 root 密码登录 | sshd_config | sshd -T | grep permitrootlogin |
| 4 | 防火墙默认拒绝入站 | ufw | ufw status verbose |
| 5 | 仅开放指定端口 | ufw | ufw status numbered |
| 6 | fail2ban sshd 监狱已启用 | jail.local | fail2ban-client status sshd |
| 7 | 安全更新自动安装 | apt | systemctl status unattended-upgrades |
| 8 | 无异常监听端口 | — | ss -tulpn |
| 9 | 通过 WireGuard 管理(可选) | wg0.conf | wg show |
| 10 | 加固后创建快照 | 客户端区域附加组件, $1.99/月 | 服务 → 您的 VPS → 快照 |
第 10 项值得多说一句:先加固,后快照,顺序不能颠倒——干净且加固后的服务器快照是您应对未来所有失误的回滚保障。每日异地快照是 $1.99/月 的附加组件,可在 客户端区域 中启用。三十分钟的预防,一键的保险。