2026-10-24 · 服务器 / 腾讯云 / 运维

腾讯云轻量服务器初始化与安全加固清单

新买了一台腾讯云轻量应用服务器(Debian 13,2 核 2G,50G 云硬盘),准备长期跑点小网站和小工具。这篇文章记录我从零把它变成「安全、可维护」状态的全过程,照着做大约 20 分钟。

第一步:先升级系统

拿到机器第一件事不是装软件,而是把系统补到最新。Debian 上一条命令的事:

apt-get update
DEBIAN_FRONTEND=noninteractive apt-get -y \
  -o Dpkg::Options::="--force-confdef" \
  -o Dpkg::Options::="--force-confold" dist-upgrade

noninteractive + confdef/confold 的作用是:不弹交互界面、配置文件冲突时保留旧配置,适合远程无人值守升级。

我这次升了 113 个包,顺带把内核从 6.12.107 换到了 6.12.113。内核升级后要重启才生效,别看到 dpkg -l 里有新内核就以为已经在用了,用 uname -r 确认。

第二步:加 Swap(2G 内存必做)

2G 内存的机器不配 Swap,遇到编译、批量处理或者内存小高峰很容易直接被 OOM 杀掉进程。加 2G Swap 成本几乎为零:

fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo '/swapfile none swap sw 0 0' >> /etc/fstab
echo 'vm.swappiness=10' > /etc/sysctl.d/99-swap.conf
sysctl -q -p /etc/sysctl.d/99-swap.conf

swappiness=10 是让系统优先用内存、只在真吃紧时才用 Swap,比默认的 60 更适合服务器。

第三步:SSH 加固(最重要)

三件事:用密钥登录、禁用密码登录、收紧 root 登录。

# 本地生成密钥
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_server -N ''

# 把公钥写到服务器
mkdir -p /root/.ssh && chmod 700 /root/.ssh
echo 'ssh-ed25519 AAAA...yourkey... you@local' >> /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys

服务器端 /etc/ssh/sshd_config:

PasswordAuthentication no
KbdInteractiveAuthentication no
ChallengeResponseAuthentication no
PermitRootLogin prohibit-password
X11Forwarding no

改完先 sshd -t 校验,再 systemctl reload ssh。注意:UsePAM yes 时只关 PasswordAuthentication 不够,KbdInteractiveAuthentication 也要关,否则 PAM 的键盘交互还能用密码登录。

第四步:自动安全更新

个人服务器最容易犯的错是"装完就忘",半年后满屏待升级。装个 unattended-upgrades 只自动打安全补丁:

apt-get install -y unattended-upgrades
# 只保留 security 和 updates 源,避免自动升级主仓库的所有包

再配置 20auto-upgrades 打开每日检查即可。

收尾检查清单

检查项 命令
内核版本uname -r
Swap 是否挂载swapon --show
SSH 是否只剩公钥认证`sshd -T \grep passwordauthentication`
失败的服务systemctl --failed
监听端口ss -tulnp

这套流程做完,机器才算「可以长期放东西」。如果你不想自己折腾,或者手上有一台跑得不太稳的服务器,可以找我做一次初始化/加固,通常一小时内搞定。