在 Debian 上“装防火墙”通常有两层含义:
下面按最推荐、最简单的方式讲,并补充进阶做法。
sudo apt update
sudo apt install ufw
# 默认拒绝入站,允许出站
sudo ufw default deny incoming
sudo ufw default allow outgoing
# 允许 SSH(非常重要,否则可能断连)
sudo ufw allow ssh
# 或指定端口
sudo ufw allow 22/tcp
# 允许常用服务(按需)
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status verbose
✅ 适合:个人服务器、VPS、新手
sudo apt install firewalld
sudo systemctl enable --now firewalld
常用命令:
firewall-cmd --state
firewall-cmd --add-service=ssh --permanent
firewall-cmd --reload
⚠️ Debian 默认更偏向 ufw / nftables
Debian 10+ 默认使用 nftables
sudo apt install nftables
sudo systemctl enable --now nftables
nft add table inet filter
nft add chain inet filter input { type filter hook input priority 0 ; }
nft add rule inet filter input ct state established,related accept
nft add rule inet filter input iif lo accept
nft add rule inet filter input tcp dport 22 accept
nft add rule inet filter input drop
保存规则:
nft list ruleset > /etc/nftables.conf
Debian 新版本已逐步废弃 iptables 为前端,仅作兼容层。
sudo apt install iptables
fail2ban 防爆破| 用户类型 | 推荐 |
|---|---|
| 新手 / VPS | ✅ UFW |
| 运维 / 自动化 | nftables |
| 熟悉 CentOS | firewalld |
如果你愿意,可以告诉我:
我可以给你一套可直接复制的防火墙配置。