在 Debian 系统中,常用的防火墙方案是 UFW(Uncomplicated Firewall),它是对 iptables 的简化封装,适合大多数用户。如果你需要更底层控制,也可以直接使用 iptables 或 nftables。下面我按推荐程度给你讲。
sudo apt update
sudo apt install ufw
sudo ufw status
sudo ufw default deny incoming
sudo ufw default allow outgoing
含义:
sudo ufw allow 22 # SSH
sudo ufw allow 80 # HTTP
sudo ufw allow 443 # HTTPS
也可以指定协议:
sudo ufw allow 22/tcp
sudo ufw allow from 192.168.1.100
sudo ufw delete allow 22
sudo ufw enable
sudo ufw disable
⚠️ 注意:启用前确保 SSH 端口已放行,否则可能被锁在外面。
sudo iptables -L
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
sudo apt install iptables-persistent
sudo netfilter-persistent save
sudo apt install nftables
sudo nft list ruleset
sudo nft add table inet filter
sudo nft add chain inet filter input { type filter hook input priority 0 \; }
sudo nft add rule inet filter input tcp dport 22 accept
sudo systemctl enable nftables
| 场景 | 推荐 |
|---|---|
| 新手 / 服务器 | ✅ UFW |
| 高级定制 | nftables |
| 老系统 | iptables |
如果你愿意,可以告诉我:
cat /etc/debian_version)我可以帮你给出完整可用的防火墙配置方案。