0Pricing
Linux Networking & TCP/IP for Developers · 课时

使用 nftables 配置 Linux 防火墙

使用 nftables 和易用的 ufw 前端控制 Linux 主机的入站和出站流量,保护网络服务。

使用 nftables 配置 Linux 防火墙 是 CoddyKit 上的免费 Linux Networking & TCP/IP for Developers 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Linux Networking & TCP/IP for Developers 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Linux Networking & TCP/IP for Developers 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why a Host Firewall?

A host firewall decides which packets are allowed in or out of a single machine. It is your last line of defense, protecting services even when the network perimeter is breached.

From iptables to nftables

Modern Linux uses nftables as the kernel packet-filtering framework, replacing the older iptables. It uses tables, chains, and rules but with a cleaner, unified syntax.

Tables, Chains, and Rules

In nftables a table holds chains, a chain holds ordered rules, and each rule matches packets and takes an action like accept or drop. Chains hook into traffic at points such as input and output.

Listing the Ruleset

See the entire active configuration with a single command. This is always your first step before changing anything.

sudo nft list ruleset

A Simple Input Policy

Create a table and an input chain with a default drop policy, then allow only what you need. Default-deny is the secure baseline.

sudo nft add table inet filter
sudo nft add chain inet filter input { type filter hook input priority 0 \; policy drop \; }

Allowing SSH

Add a rule to accept incoming TCP on port 22 so you do not lock yourself out before allowing anything else.

sudo nft add rule inet filter input tcp dport 22 accept

Allowing Established Traffic

Permit replies to connections you initiated by accepting established and related traffic. Without this, outbound requests get no responses.

sudo nft add rule inet filter input ct state established,related accept

The Easier Way: ufw

For everyday use, ufw (Uncomplicated Firewall) is a friendly front-end. It manages the underlying rules with simple commands.

sudo ufw allow 22/tcp
sudo ufw enable

Checking ufw Status

Verify which rules ufw has applied and whether it is active.

sudo ufw status verbose

Persisting Rules

nftables rules added at the command line vanish on reboot. Save them to /etc/nftables.conf and enable the service so they reload automatically.

sudo nft list ruleset | sudo tee /etc/nftables.conf
sudo systemctl enable nftables

Logging Dropped Packets

Add a logging rule before the final drop so you can see what is being blocked. This is invaluable when a service mysteriously cannot be reached.

sudo nft add rule inet filter input log prefix "dropped: "

Quick Check

Test your firewall knowledge.

Recap

You learned to configure the Linux firewall.

  • nftables uses tables, chains, and rules with a default-deny baseline.
  • Always allow SSH and established traffic before locking down.
  • ufw simplifies common rules; persist nftables to survive reboots.

常见问题解答

「使用 nftables 配置 Linux 防火墙」课时是免费的吗?

是的 — 「使用 nftables 配置 Linux 防火墙」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Linux Networking & TCP/IP for Developers 课程的其余内容,请升级到 CoddyKit PRO。 Linux Networking & TCP/IP for Developers 课程共包含 4 节课。

「使用 nftables 配置 Linux 防火墙」这节课中我会学到什么?

使用 nftables 和易用的 ufw 前端控制 Linux 主机的入站和出站流量,保护网络服务。 你通过在浏览器中直接运行的动手代码来练习 Linux Networking & TCP/IP for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Linux Networking & TCP/IP for Developers 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Linux Networking & TCP/IP for Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「使用 nftables 配置 Linux 防火墙」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Linux Networking & TCP/IP for Developers 课中编写并运行代码吗?

能。每节 Linux Networking & TCP/IP for Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 管理网络接口
  2. 路由表与网关
  3. DNS 配置与解析
  4. 使用 nftables 配置 Linux 防火墙
← 返回 Linux Networking & TCP/IP for Developers