VPS 服务器上线后,安全配置是重中之重。本教程详细介绍 10 个必须完成的安全加固步骤,保护你的服务器免受攻击。
一、为什么 VPS 安全如此重要?
服务器暴露在公网,每分钟都面临各种攻击尝试:暴力破解、漏洞扫描、DDoS 攻击等。一个被入侵的服务器可能导致数据泄露、服务中断,甚至成为攻击他人的跳板。
二、安全加固 10 步骤详解
步骤 1:系统更新到最新版本
及时更新系统可以修复已知安全漏洞:
# Ubuntu/Debian
apt update && apt upgrade -y
# CentOS/RHEL
yum update -y
# 检查需要重启的更新
[ -f /var/run/reboot-required ] && echo "需要重启"
步骤 2:创建普通用户并禁用 root 登录
# 创建新用户
adduser admin
# 添加 sudo 权限
usermod -aG sudo admin # Ubuntu/Debian
usermod -aG wheel admin # CentOS/RHEL
# 测试新用户登录
su - admin
编辑 SSH 配置文件禁用 root 登录:
vim /etc/ssh/sshd_config
# 修改以下配置:
PermitRootLogin no
PasswordAuthentication no # 推荐使用密钥登录
# 重启 SSH 服务
systemctl restart sshd # Ubuntu/Debian
systemctl restart ssh # CentOS/RHEL
步骤 3:配置 SSH 密钥登录
# 在本地生成密钥对
ssh-keygen -t ed25519 -C "[email protected]"
# 复制公钥到服务器
ssh-copy-id admin@你的服务器 IP
# 验证密钥登录
ssh admin@你的服务器 IP
步骤 4:修改 SSH 默认端口
# 编辑 SSH 配置
vim /etc/ssh/sshd_config
# 修改端口(选择 1024-65535 之间的端口)
Port 22345
# 重启 SSH
systemctl restart sshd
# 更新防火墙规则
ufw allow 22345/tcp
ufw delete allow 22/tcp # Ubuntu
步骤 5:配置防火墙(UFW)
# 安装 UFW(如未安装)
apt install ufw -y
# 设置默认策略
ufw default deny incoming
ufw default allow outgoing
# 开放必要端口
ufw allow 22345/tcp # SSH
ufw allow 80/tcp # HTTP
ufw allow 443/tcp # HTTPS
# 启用防火墙
ufw enable
# 查看状态
ufw status verbose
步骤 6:安装 Fail2ban 防止暴力破解
# 安装 Fail2ban
apt install fail2ban -y # Ubuntu/Debian
yum install fail2ban -y # CentOS/RHEL
# 复制配置文件
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
# 编辑配置
vim /etc/fail2ban/jail.local
# 添加以下配置:
[sshd]
enabled = true
port = 22345
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600
启动 Fail2ban:
systemctl enable fail2ban
systemctl start fail2ban
systemctl status fail2ban
步骤 7:禁用 IPv6(如不需要)
# 编辑 sysctl 配置
vim /etc/sysctl.conf
# 添加以下内容:
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
# 应用配置
sysctl -p
# 验证
ip a | grep inet6
步骤 8:配置自动安全更新
# Ubuntu/Debian 安装自动更新
apt install unattended-upgrades -y
dpkg-reconfigure --priority=low unattended-upgrades
# 编辑配置
vim /etc/apt/apt.conf.d/50unattended-upgrades
# 确保以下行为启用:
Unattended-Upgrade::Allowed-Origins {
"${distro_id}:${distro_codename}-security";
};
# CentOS/RHEL 配置自动更新
yum install yum-cron -y
systemctl enable yum-cron
systemctl start yum-cron
步骤 9:限制服务访问
# 检查开放端口
netstat -tulpn
ss -tulpn
# 关闭不必要的服务
systemctl stop 服务名
systemctl disable 服务名
# 例如关闭不需要的服务
systemctl stop cups # 打印服务
systemctl disable cups
步骤 10:配置日志监控
# 查看安全日志
journalctl -u sshd --since "1 hour ago"
# 查看失败登录尝试
grep "Failed password" /var/log/auth.log | tail -20
# 查看成功登录
grep "Accepted" /var/log/auth.log | tail -20
# 安装日志分析工具
apt install logwatch -y
logwatch --detail high --mailto [email protected] --service all
三、高级安全加固(可选)
1. 配置双因素认证(2FA)
# 安装 Google Authenticator
apt install libpam-google-authenticator -y
# 为用户配置
google-authenticator
# 按提示操作,保存恢复码
# 然后编辑 PAM 配置启用 2FA
2. 安装 Rootkit 检测工具
# 安装 rkhunter
apt install rkhunter -y
# 更新特征库
rkhunter --update
# 执行检查
rkhunter --check
四、安全维护清单
- 每周检查系统更新
- 每月检查登录日志
- 定期审查开放端口
- 定期备份重要数据
- 监控磁盘空间使用
- 检查 Fail2ban 日志
五、应急响应
如果怀疑服务器被入侵:
- 立即断开网络连接
- 修改所有密码和密钥
- 检查异常进程和连接
- 分析系统日志
- 考虑重装系统并恢复备份
总结
完成以上 10 个步骤可以极大提升 VPS 服务器的安全性。安全是一个持续的过程,定期检查和维护同样重要。记住:没有绝对安全的系统,但我们可以让攻击者知难而退。