前言
很多新手购买VPS后直接使用默认配置运行,殊不知互联网上每天有大量自动化脚本在扫描和爆破服务器。一台裸奔的VPS可能在上线几小时内就遭到入侵。本文介绍VPS到手后必做的安全加固步骤,帮你把服务器从"裸奔"变成"铁桶"。
一、创建普通用户并授予sudo权限
首先要做的就是不再使用root账户操作服务器。root账户权限太大,一旦被入侵后果严重。
# 创建新用户(将 myuser 替换为你想用的用户名)
adduser myuser
# 将用户加入sudo组(Debian/Ubuntu)
usermod -aG sudo myuser
# 验证sudo权限
su - myuser
sudo whoami
# 输出 root 说明权限配置成功
二、配置SSH密钥登录
密码登录是服务器被爆破的主要原因。使用SSH密钥登录可以彻底杜绝暴力破解。
生成SSH密钥对
在你的本地电脑上执行(不是VPS上):
# 生成Ed25519密钥(推荐,比RSA更安全更快)
ssh-keygen -t ed25519 -C "[email protected]"
# 一路回车使用默认设置即可
将公钥上传到VPS
# 将公钥复制到VPS
ssh-copy-id -i ~/.ssh/id_ed25519.pub myuser@你的VPS_IP
如果 ssh-copy-id 不可用(如Windows用户),可以手动操作:
# 在本地查看公钥内容
cat ~/.ssh/id_ed25519.pub
# 登录VPS后执行
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo "粘贴公钥内容" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
测试密钥登录
# 用密钥登录测试
ssh myuser@你的VPS_IP
# 如果能正常登录,说明密钥配置成功
三、加固SSH配置
编辑SSH配置文件:
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
sudo nano /etc/ssh/sshd_config
修改以下关键配置:
# 修改默认端口(选择1024-65535之间的端口)
Port 2222
# 禁止root直接登录
PermitRootLogin no
# 禁止密码登录(确保密钥登录已经可用!)
PasswordAuthentication no
ChallengeResponseAuthentication no
# 禁止空密码
PermitEmptyPasswords no
# 限制最大尝试次数
MaxAuthTries 3
# 登录超时时间(秒)
LoginGraceTime 30
# 仅允许指定用户登录(可选)
AllowUsers myuser
# 禁用X11转发
X11Forwarding no
# 空闲超时断开(5分钟无操作断开)
ClientAliveInterval 300
ClientAliveCountMax 2
重启SSH服务使配置生效:
sudo systemctl restart sshd
重要提醒:修改SSH端口和禁用密码登录前,务必确认密钥登录已经可用,否则可能被锁在服务器外面。建议保留当前SSH连接窗口,新开一个窗口测试新配置。如果出问题可以到VPS商家的VNC控制台恢复。
四、安装配置防火墙
使用UFW(推荐新手)
UFW是Ubuntu/Debian自带的简化防火墙工具,操作简单直观。
# 安装UFW
sudo apt install -y ufw
# 设置默认策略:拒绝入站,允许出站
sudo ufw default deny incoming
sudo ufw default allow outgoing
# 允许SSH端口(务必先开放SSH端口再启用防火墙!)
sudo ufw allow 2222/tcp comment 'SSH'
# 允许HTTP和HTTPS(如果需要建站)
sudo ufw allow 80/tcp comment 'HTTP'
sudo ufw allow 443/tcp comment 'HTTPS'
# 启用防火墙
sudo ufw enable
# 查看状态
sudo ufw status verbose
常用UFW命令:
# 允许特定端口
sudo ufw allow 8080/tcp
# 允许特定IP访问
sudo ufw allow from 192.168.1.100
# 删除规则
sudo ufw delete allow 8080/tcp
# 重置所有规则
sudo ufw reset
使用iptables
如果你的系统不支持UFW,可以直接使用iptables:
# 清空现有规则
sudo iptables -F
# 设置默认策略
sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT ACCEPT
# 允许回环接口
sudo iptables -A INPUT -i lo -j ACCEPT
# 允许已建立的连接
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 允许SSH
sudo iptables -A INPUT -p tcp --dport 2222 -m state --state NEW -m limit --limit 3/min -j ACCEPT
# 允许HTTP/HTTPS
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# 允许ping
sudo iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
# 丢弃其他所有流量
sudo iptables -A INPUT -j DROP
# 持久化规则
sudo apt install -y iptables-persistent
sudo iptables-save > /etc/iptables/rules.v4
五、安装Fail2Ban防暴力破解
Fail2Ban可以自动监控登录日志,当检测到多次失败登录时自动封禁IP。
# 安装
sudo apt install -y fail2ban
# 创建本地配置文件
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo nano /etc/fail2ban/jail.local
在 [DEFAULT] 部分修改:
[DEFAULT]
# 封禁时间(秒),3600=1小时
bantime = 3600
# 检测时间窗口(秒)
findtime = 600
# 最大失败次数
maxretry = 3
[sshd]
enabled = true
port = 2222
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600
启动Fail2Ban:
sudo systemctl enable fail2ban
sudo systemctl start fail2ban
常用管理命令:
# 查看Fail2Ban状态
sudo fail2ban-client status
# 查看sshd jail状态(被封禁的IP数等)
sudo fail2ban-client status sshd
# 手动解封IP
sudo fail2ban-client set sshd unbanip 1.2.3.4
六、开启自动安全更新
保持系统更新是安全的基础。配置自动安装安全更新可以避免已知漏洞被利用。
# 安装自动更新工具
sudo apt install -y unattended-upgrades
# 启用自动更新
sudo dpkg-reconfigure -plow unattended-upgrades
# 选择 Yes
# 编辑配置(可选)
sudo nano /etc/apt/apt.conf.d/50unattended-upgrades
确保配置文件包含:
Unattended-Upgrade::Remove-Unused-Kernel-Packages "true";
Unattended-Upgrade::Remove-New-Unused-Dependencies "true";
Unattended-Upgrade::Remove-Unused-Dependencies "true";
# 不要自动重启(避免服务中断)
Unattended-Upgrade::Automatic-Reboot "false";
手动更新系统:
sudo apt update && sudo apt upgrade -y
七、内核安全加固
编辑 /etc/sysctl.conf 添加以下安全参数:
sudo nano /etc/sysctl.conf
# 防止IP欺骗
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
# 禁用源路由
net.ipv4.conf.all.accept_source_route = 0
net.ipv6.conf.all.accept_source_route = 0
# 忽略ICMP重定向
net.ipv4.conf.all.accept_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
# 启用SYN洪水防护
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 2048
net.ipv4.tcp_synack_retries = 2
# 忽略广播ping
net.ipv4.icmp_echo_ignore_broadcasts = 1
# 记录异常数据包
net.ipv4.conf.all.log_martians = 1
# 限制内核指针泄露
kernel.kptr_restrict = 2
# 限制dmesg访问
kernel.dmesg_restrict = 1
应用配置:
sudo sysctl -p
八、安全加固检查清单
完成以上步骤后,可以用以下清单确认是否全部完成:
- 创建了普通用户,不再直接使用root
- 配置了SSH密钥登录
- SSH端口已修改为非默认端口
- 禁止了root远程登录
- 禁止了密码登录
- 防火墙已启用,只开放必要端口
- Fail2Ban已安装并运行
- 自动安全更新已配置
- 内核安全参数已加固
总结
服务器安全不是一劳永逸的事情,以上这些措施是最基本的安全基线。做好这些基础防护,可以抵御绝大多数自动化攻击。对于刚买到的便宜VPS,花10分钟做完这些设置,就能让你的服务器安全等级提升好几个档次。