在 Debian 系统中,“防火墙”通常是指 iptables / nftables,而现代 Debian(如 Debian 10 及以后)更推荐使用 nftables,或者通过 ufw(简单前端) 来管理。下面按常见需求给你几种安装和配置方式。
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 80
sudo ufw allow 443
sudo ufw enable
sudo ufw status
sudo apt update
sudo apt install nftables
sudo systemctl enable nftables
sudo systemctl start nftables
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 ct state established,related accept
sudo nft add rule inet filter input iif lo accept
sudo nft add rule inet filter input tcp dport {22,80,443} accept
sudo nft add rule inet filter input drop
sudo nft list ruleset > /etc/nftables.conf
sudo apt install iptables
示例:
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -P INPUT DROP
保存(视版本):
iptables-save > /etc/iptables/rules.v4
如果你告诉我:
我可以给你一套精确可用的防火墙配置。