指南

10 分钟搞定新 VPS 安全

入门阅读时间 10 分钟更新时间 2026年6月18日
简短回答

五项调整几乎能杜绝所有针对新服务器的自动化攻击:使用密钥认证的 SSH 并禁用密码、禁止直接 root 登录、默认拒绝的防火墙(同时覆盖 IPv4 和 IPv6)、自动安全更新,以及 fail2ban。这些加在一起大约需要十分钟,远比任何提供商的特性都更重要。

01 生成并安装 SSH 密钥

Do this from your own machine, not the server. Ed25519 keys are shorter and faster than RSA and are supported everywhere that matters.

ssh-keygen -t ed25519 -C "onionvps-$(date +%Y%m)"
ssh-copy-id -i ~/.ssh/id_ed25519.pub root@YOUR_SERVER_IP

02 创建非 root 用户

Working as root all the time removes a useful safety net and makes every mistake maximally expensive.

adduser --gecos "" ops
usermod -aG sudo ops
rsync --archive --chown=ops:ops ~/.ssh /home/ops

03 加固 SSH 守护进程

Disable password authentication entirely — brute force against key-only SSH is not possible. Keep a second terminal connected while you do this, so a mistake does not lock you out.

cat >/etc/ssh/sshd_config.d/99-hardening.conf <<'EOF'
PasswordAuthentication no
PermitRootLogin no
KbdInteractiveAuthentication no
AllowUsers ops
EOF
sshd -t && systemctl reload ssh

04 设置默认拒绝的防火墙

Cover IPv6 as well as IPv4. Every OnionVPS instance has a routed /64, so a v4-only ruleset leaves every service publicly reachable over v6.

ufw default deny incoming
ufw default allow outgoing
ufw allow 22/tcp
ufw allow 80,443/tcp
ufw enable
ufw status verbose   # confirm IPv6 shows as enabled

05 启用自动安全更新

Unattended upgrades close the window between a patch being published and you noticing it exists.

apt install -y unattended-upgrades
dpkg-reconfigure -plow unattended-upgrades

06 安装 fail2ban 并创建快照

fail2ban mostly reduces log noise once passwords are disabled, but it is cheap. Then snapshot the configured state — that becomes your known-good baseline.

apt install -y fail2ban && systemctl enable --now fail2ban

常见问题

我应该修改 SSH 端口吗?

这主要是减少噪音而非真正安全,但它确实有效——绝大多数扫描器只会尝试端口 22。结合仅密钥认证,残余风险无论如何都微乎其微。

如果我把自己锁在外面怎么办?

使用控制面板中的带外 VNC 控制台。它连接到虚拟串行控制台,无需任何网络即可工作。