温馨提示×

怎样安装debian防火墙

小樊
67
2026-09-02 08:01:55
栏目: 智能运维

在 Debian 系统中,“防火墙”通常是指 iptables / nftables,而现代 Debian(如 Debian 10 及以后)更推荐使用 nftables,或者通过 ufw(简单前端) 来管理。下面按常见需求给你几种安装和配置方式。


一、使用 ufw(最推荐,新手友好)

1. 安装 ufw

sudo apt update
sudo apt install ufw

2. 基本设置

# 默认拒绝入站,允许出站
sudo ufw default deny incoming
sudo ufw default allow outgoing

# 允许 SSH(非常重要,防止断连)
sudo ufw allow ssh

# 如需开放其他端口
sudo ufw allow 80
sudo ufw allow 443

3. 启动防火墙

sudo ufw enable

4. 查看状态

sudo ufw status

二、使用 nftables(Debian 官方推荐)

1. 安装 nftables

sudo apt update
sudo apt install nftables

2. 启用并开机启动

sudo systemctl enable nftables
sudo systemctl start nftables

3. 示例规则(允许 SSH、HTTP、HTTPS)

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

4. 保存规则

sudo nft list ruleset > /etc/nftables.conf

三、使用 iptables(传统方式,不推荐新系统)

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

四、选择建议

  • 新手 / 服务器管理:✅ ufw
  • 专业 / 复杂网络:✅ nftables
  • 老系统兼容:⚠ iptables

如果你告诉我:

  • Debian 版本(如 11 / 12)
  • 是本地机器还是云服务器
  • 需要开放哪些端口

我可以给你一套精确可用的防火墙配置。

0 踩