最近入手了一台新的 VPS,系统是 Debian 12 (bookworm),记录一下初始化配置过程,方便以后参考。
1. 系统更新
apt update && apt upgrade -y
apt install -y curl wget git vim ufw fail2ban
2. 创建普通用户
adduser gong
usermod -aG sudo gong
3. 配置 SSH 密钥登录
本地生成密钥对:
ssh-keygen -t ed25519 -C "gong@vps"
把公钥上传到服务器:
ssh-copy-id -i ~/.ssh/id_ed25519.pub gong@server_ip
修改 /etc/ssh/sshd_config:
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
Port 22222
重启 SSH 服务:
systemctl restart sshd
4. 配置防火墙
ufw default deny incoming
ufw default allow outgoing
ufw allow 22222/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw enable
5. 配置 fail2ban
编辑 /etc/fail2ban/jail.local:
[sshd]
enabled = true
port = 22222
maxretry = 3
bantime = 86400
systemctl enable fail2ban
systemctl restart fail2ban
6. 时区与时间同步
timedatectl set-timezone Asia/Shanghai
apt install -y chrony
systemctl enable chrony
小结
服务器初始化的关键是减少攻击面:禁用 root 登录、强制密钥认证、关闭不必要的端口、配置自动封禁。下次配置新服务器时照这个流程走一遍就行。