前言
刚拿到一台心仪VPS服务器,是不是兴奋?兴奋过后,我总结了一些优化项目,按照这个流程操作,你的服务器就能从”裸机”变成性能强劲、安全可靠。
开始优化之旅
1️⃣ 换源加速
操作步骤
首先备份一下原始配置,这是好习惯:
# 备份原始源配置(万一出问题还能恢复)
cp /etc/apt/sources.list /etc/apt/sources.list.backup
# 安装HTTPS证书支持
apt-get install -y ca-certificates
# 获取系统版本代号
CODENAME=$(lsb_release -cs)
我推荐几个稳定的源,按你的网络环境选择:
国内源(推荐国内用户):
- 阿里云源:速度快,稳定性好,推荐
- 中科大源:教育网用户首选
- 清华源:老牌稳定源
国外源(推荐海外用户):
- 官方源:最稳定,但速度可能较慢
- 香港源:亚洲地区访问较快
- 日本源:东亚地区访问较快
配置国内源(推荐国内用户)
cat > /etc/apt/sources.list <<EOF
deb http://mirrors.aliyun.com/debian/ ${CODENAME} main contrib non-free non-free-firmware
deb-src http://mirrors.aliyun.com/debian/ ${CODENAME} main contrib non-free non-free-firmware
deb http://mirrors.aliyun.com/debian-security ${CODENAME}-security main contrib non-free non-free-firmware
deb-src http://mirrors.aliyun.com/debian-security ${CODENAME}-security main contrib non-free non-free-firmware
deb http://mirrors.aliyun.com/debian/ ${CODENAME}-updates main contrib non-free non-free-firmware
deb-src http://mirrors.aliyun.com/debian/ ${CODENAME}-updates main contrib non-free non-free-firmware
EOF
配置国外源(推荐海外用户)
cat > /etc/apt/sources.list <<EOF
deb https://deb.debian.org/debian/ ${CODENAME} main contrib non-free non-free-firmware
deb-src https://deb.debian.org/debian/ ${CODENAME} main contrib non-free non-free-firmware
deb https://deb.debian.org/debian-security ${CODENAME}-security main contrib non-free non-free-firmware
deb-src https://deb.debian.org/debian-security ${CODENAME}-security main contrib non-free non-free-firmware
deb https://deb.debian.org/debian/ ${CODENAME}-updates main contrib non-free non-free-firmware
deb-src https://deb.debian.org/debian/ ${CODENAME}-updates main contrib non-free non-free-firmware
EOF
配置完成后,更新一下软件包列表:
apt update
如果看到”Get:1″这样的输出,说明换源成功了!速度应该比之前快很多。
2️⃣ 账户安全
设置强密码
首先给root设置一个强密码,别用123456这种弱密码:
# 设置root密码(请替换为你的强密码)
echo "root:YourStrongPassword123!" | chpasswd
密码要求:至少8位,包含大小写字母、数字和特殊字符。
创建普通用户
直接用root操作是很危险的,我们创建一个普通用户:
# 创建用户(替换username为你的用户名)
useradd -m -s /bin/bash username
# 设置用户密码
echo "username:UserPassword123!" | chpasswd
# 给用户sudo权限
usermod -aG sudo username
这样以后就可以用普通用户登录,需要管理员权限时用sudo。
配置SSH密钥认证
密码登录虽然方便,但SSH密钥更安全。如果你有SSH密钥,可以这样配置:
# 创建SSH目录
mkdir -p /root/.ssh
chmod 700 /root/.ssh
# 添加你的公钥(请替换为你的实际公钥)
echo "你的SSH公钥内容" > /root/.ssh/authorized_keys
# 设置正确的权限
chmod 600 /root/.ssh/authorized_keys
配置SSH密钥认证后的sshd_config修改
配置完SSH密钥后,需要修改SSH配置以启用密钥认证:
# 修改SSH配置以支持密钥认证
sed -i 's/^#*PubkeyAuthentication.*/PubkeyAuthentication yes/' /etc/ssh/sshd_config
sed -i 's/^#*PasswordAuthentication.*/PasswordAuthentication yes/' /etc/ssh/sshd_config
sed -i 's/^#*PermitRootLogin.*/PermitRootLogin prohibit-password/' /etc/ssh/sshd_config
# 确保配置生效
systemctl restart ssh
3️⃣ SSH安全加固 –
SSH是服务器的入口,必须加强防护!
修改SSH端口
默认的22端口就像在门上贴了”请进”的牌子,我们换个端口:
# 备份SSH配置(好习惯)
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup
# 修改SSH端口为54321(你可以选择其他端口)
sed -i 's/^#*Port .*/Port 54321/' /etc/ssh/sshd_config
强化SSH安全设置
现在给SSH加几道锁:
# 配置SSH安全参数
cat >> /etc/ssh/sshd_config <<EOF
# 安全配置
PasswordAuthentication yes
ChallengeResponseAuthentication no
PermitRootLogin prohibit-password
PubkeyAuthentication yes
# 登录限速(防止暴力破解)
LoginGraceTime 30
MaxAuthTries 3
# 安全横幅
Banner /etc/issue.net
EOF
# 创建安全横幅(吓唬一下不怀好意的人)
echo "Unauthorized access to this server is prohibited." > /etc/issue.net
# 重启SSH服务
systemctl restart ssh
注意:修改SSH端口后,记得用新端口连接:ssh -p 54321 root@your-server-ip
4️⃣ 防火墙配置
安装nftables
默认的ufw防火墙功能有限,我们换成更强大的nftables:
# 卸载ufw(如果存在)
if command -v ufw >/dev/null 2>&1; then
ufw --force reset
ufw disable
apt-get purge -y ufw
fi
# 安装nftables
apt-get install -y nftables
配置基础防火墙规则
现在配置防火墙规则,只允许必要的流量通过:
# 创建nftables配置文件
cat > /etc/nftables.conf <<EOF
#!/usr/sbin/nft -f
flush ruleset
table inet filter {
chain input {
type filter hook input priority 0;
policy drop;
# 允许本地回环(本机访问)
iif lo accept
# 允许已建立的连接(回复包)
ct state established,related accept
# 开放SSH端口(记得改成你的端口)
tcp dport 54321 accept
udp dport 54321 accept
}
chain forward {
type filter hook forward priority 0;
policy drop;
}
chain output {
type filter hook output priority 0;
policy accept;
}
}
# 流量监控表(用于统计)
table inet mangle {
chain prerouting {
type filter hook prerouting priority mangle;
policy accept;
}
chain output {
type route hook output priority mangle;
policy accept;
}
chain input {
type filter hook input priority mangle;
policy accept;
}
}
EOF
# 启用并启动nftables
systemctl enable nftables
systemctl restart nftables
防火墙配置完成!现在只有SSH端口是开放的,其他端口都被屏蔽了。
5️⃣ 系统性能优化 – 让服务器跑得更快
系统参数调优
# 创建系统参数配置文件
cat > /etc/sysctl.d/99-custom.conf <<EOF
# 网络性能优化
net.ipv4.tcp_congestion_control = bbr
net.core.default_qdisc = fq
net.core.rmem_max = 33554432
net.core.wmem_max = 33554432
net.core.somaxconn = 4096
net.ipv4.tcp_max_syn_backlog = 4096
net.ipv4.tcp_rmem = 4096 1048576 33554432
net.ipv4.tcp_wmem = 4096 1048576 33554432
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_adv_win_scale = 1
net.ipv4.tcp_fastopen = 3
net.ipv4.ip_forward = 1
net.ipv6.conf.all.forwarding = 1
net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_sack = 1
kernel.panic = 1
vm.swappiness = 3
EOF
# 应用系统参数
sysctl --system
虚拟内存配置
小内存服务器必备!当物理内存不够时,系统会使用虚拟内存:
# 创建swap文件(根据服务器内存大小调整)
SWAP_SIZE=2G # 建议为物理内存的1-2倍
# 创建swap文件
fallocate -l $SWAP_SIZE /swapfile
# 设置权限
chmod 600 /swapfile
# 格式化swap文件
mkswap /swapfile
# 启用swap
swapon /swapfile
# 添加到fstab实现开机自动挂载
echo "/swapfile none swap sw 0 0" >> /etc/fstab
小贴士:如果你的服务器内存大于4GB,可以不用配置swap,或者配置1GB就够了。
6️⃣ 时间同步 – 让服务器时间准确
服务器时间不准确会导致很多问题,比如日志时间错乱、证书验证失败等。
设置时区
# 设置为上海时区(国内用户)
timedatectl set-timezone Asia/Shanghai
# 验证时区设置
timedatectl
配置NTP时间同步
国内用户推荐:
# 安装时间同步服务
apt-get install -y systemd-timesyncd
# 配置国内NTP服务器
cat > /etc/systemd/timesyncd.conf <<EOF
[Time]
NTP=ntp.aliyun.com ntp.ntsc.ac.cn time1.cloud.tencent.com cn.pool.ntp.org
FallbackNTP=ntp1.aliyun.com ntp2.aliyun.com time2.cloud.tencent.com
EOF
海外用户推荐:
# 配置国际NTP服务器
cat > /etc/systemd/timesyncd.conf <<EOF
[Time]
NTP=pool.ntp.org time1.google.com time.apple.com time.cloudflare.com time.windows.com
FallbackNTP=0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org
EOF
启用时间同步:
# 启用并启动时间同步服务
systemctl unmask systemd-timesyncd.service
systemctl enable systemd-timesyncd.service
systemctl restart systemd-timesyncd.service
timedatectl set-ntp yes
7️⃣ 安全加固 – 给服务器穿上防弹衣
安装Fail2Ban防暴力破解
Fail2Ban就像一个智能门卫,发现有人尝试暴力破解就自动封IP:
# 安装Fail2Ban
apt-get install -y fail2ban
# 配置SSH保护
cat > /etc/fail2ban/jail.d/sshd.local <<EOF
[sshd]
enabled = true
port = 54321
filter = sshd
logpath = /var/log/auth.log
maxretry = 5
findtime = 10m
bantime = 30m
backend = systemd
EOF
# 启动Fail2Ban
systemctl enable --now fail2ban
效果:如果有人5次密码错误,IP会被封30分钟。
配置自动安全更新
让系统自动安装安全补丁,省心又安全:
# 安装自动更新工具
apt-get install -y unattended-upgrades
# 配置自动更新
dpkg-reconfigure -plow unattended-upgrades
ICMP Ping控制(可选)
如果你不想让别人ping你的服务器:
# 禁用Ping(可选)
cat > /etc/sysctl.d/99-vpsbox-icmp.conf <<EOF
net.ipv4.icmp_echo_ignore_all = 1
EOF
# 应用设置
sysctl -w net.ipv4.icmp_echo_ignore_all=1
注意:禁用ping后,你自己也ping不了外网了,但不会影响正常网络使用。
8️⃣ 系统清理
# 清理软件包缓存
apt-get clean
apt-get autoremove -y
apt-get autoclean
# 清理日志文件
find /var/log -type f -name "*.gz" -delete
find /var/log -type f -name "*.old" -delete
find /var/log -type f -name "*.1" -delete
journalctl --vacuum-time=7d
# 清理临时文件
find /tmp -type f -atime +7 -delete
find /var/tmp -type f -atime +7 -delete
🎉 验证一下
检查SSH连接
# 使用新端口测试SSH连接
ssh -p 54321 root@your-server-ip
检查防火墙状态
# 查看nftables规则
nft list ruleset
# 检查防火墙服务状态
systemctl status nftables
检查系统状态
# 查看系统资源使用
free -h
df -h
uptime
# 查看网络连接
ss -tuln
检查安全配置
# 查看Fail2Ban状态
fail2ban-client status
# 查看SSH配置
sshd -T | grep -E "port|password|permitroot"
⚠️ 重要提醒
- 备份配置:修改前一定要备份,这是血的教训!
- 测试连接:改SSH端口后,记得用新端口连接
- 防火墙规则:新开服务端口时,记得在防火墙中开放
- 定期更新:每周执行
apt update && apt upgrade - 监控日志:定期检查日志,发现问题及时处理
🔧 日常维护建议
- 每周更新:
apt update && apt upgrade - 监控资源:用
htop、iotop监控系统状态 - 清理日志:定期清理日志文件,避免磁盘满
- 安全检查:定期检查用户权限和SSH密钥
- 备份数据:重要数据一定要定期备份
![[运维世界]-VPS主机评测常用脚本-Puo's 菜园子](http://loc-wp-cdnimg.jiyun.net/blogtest/2026/01/20260118010546482-220x150.png)






博主这篇教程太好了,我这种的菜鸟就喜欢这样的文章,哈哈,重装系统后必须这样咔咔来这么一通操作