温馨提示×

怎么开启debian防火墙服务

小樊
33
2026-09-08 08:51:22
栏目: 智能运维

在 Debian 中,“防火墙”通常指的是 iptables / nftables,不过 Debian 10 及以后更推荐使用 UFW(Uncomplicated Firewall)firewalld 来管理。下面按常见方式说明。


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

1. 安装 UFW

sudo apt update
sudo apt install ufw

2. 设置默认策略(可选)

sudo ufw default deny incoming
sudo ufw default allow outgoing

3. 允许必要端口(如 SSH)

sudo ufw allow ssh
# 或
sudo ufw allow 22/tcp

4. 开启防火墙

sudo ufw enable

5. 查看状态

sudo ufw status

✅ UFW 开启后会在系统启动时自动启用。


二、使用 firewalld(适合服务器/统一管理)

1. 安装 firewalld

sudo apt install firewalld

2. 启动并设置开机自启

sudo systemctl start firewalld
sudo systemctl enable firewalld

3. 查看状态

sudo firewall-cmd --state

4. 开放端口示例

sudo firewall-cmd --permanent --add-port=22/tcp
sudo firewall-cmd --reload

三、直接使用 nftables(底层方式)

1. 安装 nftables

sudo apt install nftables

2. 启动并开机自启

sudo systemctl enable nftables
sudo systemctl start nftables

3. 配置文件

规则写在:

/etc/nftables.conf

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

Debian 新版本已用 nftables 替代 iptables 后端:

sudo apt install iptables
sudo systemctl enable iptables

✅ 推荐选择

  • 个人/新手:UFW
  • 服务器/运维:firewalld 或 nftables

如果你告诉我:

  • Debian 版本(cat /etc/debian_version
  • 是云服务器还是本地机器
  • 是否需要开放 Web / SSH / 数据库端口

我可以帮你写一套具体防火墙规则

0