# 开放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

监控日志:定期检查日志,发现问题及时处理

🔧 日常维护建议
 
 
Back to Top © 2025 我的笔记| 由 Cloudflare Pages and Telegram 强力驱动