温馨提示×

Debian防火墙怎么配置

小樊
36
2026-07-06 18:12:34
栏目: 智能运维

Debian 本身没有默认的防火墙服务,通常是使用 iptables(传统)或 nftables(新一代,默认推荐)来配置防火墙。下面按常见场景 + 推荐方案给你说明。


一、Debian 防火墙体系说明

方式 说明 推荐
iptables 传统防火墙 旧系统
nftables 新一代防火墙(Debian 10+ 默认) ✅ 推荐
ufw iptables/nftables 的简化前端 ✅ 新手首选
firewalld RHEL 系常用 ❌ Debian 不推荐

二、最推荐方案:使用 UFW(简单、安全)

适合:新手 / 服务器 / 桌面用户

1️⃣ 安装 UFW

sudo apt update
sudo apt install ufw

2️⃣ 基本规则示例

# 允许 SSH(一定要先允许,否则可能断连)
sudo ufw allow 22/tcp

# 允许 HTTP / HTTPS
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

# 允许某个 IP
sudo ufw allow from 1.2.3.4

# 允许某段 IP
sudo ufw allow from 192.168.1.0/24

3️⃣ 启用防火墙

sudo ufw enable

4️⃣ 查看状态

sudo ufw status verbose

5️⃣ 常用命令

sudo ufw disable          # 关闭
sudo ufw reset            # 重置
sudo ufw delete allow 22  # 删除规则

UFW 本质仍然是使用 iptables / nftables,只是更易用


三、进阶方案:直接使用 nftables(推荐服务器)

Debian 10(Buster)及以后默认使用 nftables

1️⃣ 安装 nftables

sudo apt install nftables

2️⃣ 启用 nftables

sudo systemctl enable nftables
sudo systemctl start nftables

3️⃣ 示例规则(只开放 SSH + Web)

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 accept
sudo nft add rule inet filter input tcp dport 80 accept
sudo nft add rule inet filter input tcp dport 443 accept
sudo nft add rule inet filter input drop

4️⃣ 保存规则(重启不丢)

sudo nft list ruleset > /etc/nftables.conf

四、传统方式:iptables(不推荐新系统)

sudo apt install iptables

# 示例
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -P INPUT DROP

⚠️ Debian 新版本 iptables 实际是 nftables 的兼容层


五、安全建议(非常重要)

配置前先允许 SSH(22端口)
✅ 不要直接 DROP INPUT 前没允许 SSH
✅ 云服务器还需检查 安全组 / 防火墙策略
✅ 最好保留一个允许所有 IP 的 SSH 规则,直到确认无误


六、如何选择?

场景 推荐
新手 / 快速配置 ✅ UFW
服务器 / 高性能 ✅ nftables
老系统 iptables
云服务器 UFW + 云安全组

如果你愿意,可以告诉我:

  • ✅ Debian 版本(如 11 / 12)
  • ✅ 是 本地服务器 / 云服务器
  • ✅ 想开放哪些端口

我可以给你一份可直接使用的完整防火墙配置

0