温馨提示×

如何配置debian防火墙

小樊
53
2026-09-11 07:16:43
栏目: 智能运维

在 Debian 系统中,常用的防火墙方案主要有 UFW(简单) 和 iptables / nftables(底层)。下面按使用场景给你一个清晰配置指南。


一、使用 UFW(推荐,简单安全)

1. 安装 UFW

sudo apt update
sudo apt install ufw

2. 基本安全策略

sudo ufw default deny incoming
sudo ufw default allow outgoing

3. 放行必要端口

sudo ufw allow 22/tcp      # SSH
sudo ufw allow 80/tcp      # HTTP
sudo ufw allow 443/tcp     # HTTPS

4. 启用防火墙

sudo ufw enable

5. 查看状态

sudo ufw status verbose

6. 常用命令

sudo ufw disable
sudo ufw delete allow 22/tcp
sudo ufw reset

二、使用 nftables(Debian 10+ 推荐底层方案)

1. 安装 nftables

sudo apt install nftables

2. 启用并开机启动

sudo systemctl enable nftables
sudo systemctl start nftables

3. 示例规则(/etc/nftables.conf)

table inet filter {
    chain input {
        type filter hook input priority 0;
        policy drop;

        ct state established,related accept
        iif "lo" accept
        ip protocol icmp accept

        tcp dport 22 accept
        tcp dport 80 accept
        tcp dport 443 accept
    }

    chain forward {
        type filter hook forward priority 0;
        policy drop;
    }

    chain output {
        type filter hook output priority 0;
        policy accept;
    }
}

4. 应用规则

sudo nft -f /etc/nftables.conf

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

sudo apt install iptables
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables -P INPUT DROP

保存规则(Debian):

sudo apt install iptables-persistent
sudo netfilter-persistent save

四、选择建议

  • ✅ 新手 / 普通服务器:UFW
  • ✅ 高级用户 / 性能要求:nftables
  • ⚠️ 老系统兼容:iptables

如果你告诉我:

  • 是 云服务器还是本地
  • 是否运行 Web / SSH / Docker
  • Debian 版本号

我可以给你一套更精准的防火墙配置。

0 踩