防火墙管理:`ufw`、`firewalld`、`iptables`
学习配置和管理防火墙,保护 Linux 系统免受未授权访问。
防火墙管理:`ufw`、`firewalld`、`iptables` 是 CoddyKit 上的免费 Linux Command Line Mastery 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Linux Command Line Mastery 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Linux Command Line Mastery 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What's a Firewall?
A firewall acts as a security guard for your computer or network. It controls incoming and outgoing network traffic by analyzing data packets and deciding whether to allow or block them based on a set of predefined rules.
Think of it as a filter that protects your system from unauthorized access and malicious threats.
Introducing UFW
UFW, which stands for "Uncomplicated Firewall," is a user-friendly front-end for iptables, the complex default firewall utility in Linux. It's designed to make firewall management much simpler, especially for users on Debian-based systems like Ubuntu.
UFW simplifies common tasks like opening or closing ports.
UFW Status & Control
Before making changes, it's good practice to check UFW's current status. You can also easily enable or disable it.
Try running these commands:
sudo ufw status
sudo ufw enable
sudo ufw disableUFW: Allow Services
UFW allows you to open ports for common services by their name, which is very convenient. For example, to allow SSH or HTTP traffic, you just use the service name.
Let's try allowing SSH and HTTP:
sudo ufw allow ssh
sudo ufw allow http
sudo ufw status verboseUFW: Allow Custom Ports
Sometimes you need to open a specific port that isn't a named service. You can do this by specifying the port number and the protocol (TCP or UDP).
Here's how to allow port 8080 for TCP and 53 for UDP:
sudo ufw allow 8080/tcp
sudo ufw allow 53/udp
sudo ufw status verboseIntroducing Firewalld
Firewalld is another popular firewall management tool, often found on Red Hat-based systems like CentOS and Fedora. Unlike UFW, Firewalld is a dynamic firewall manager.
It uses zones to define different trust levels for network connections, allowing you to apply rules based on where the connection originates.
Firewalld Status & Zones
With Firewalld, you can check its status and explore the various zones it uses. Zones like public, home, or internal have different default rules.
Check the status and active zones:
sudo systemctl status firewalld
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --get-zonesFirewalld: Manage Services
To manage services with Firewalld, you specify the zone and the service name. Changes are temporary by default; use --permanent to make them stick after a reboot, then --reload to apply.
Let's add HTTP service to the public zone permanently:
sudo firewall-cmd --zone=public --add-service=http --permanent
sudo firewall-cmd --reload
sudo firewall-cmd --list-services --zone=publicFirewalld: Manage Ports
Similar to services, you can open or close specific port numbers in a Firewalld zone. Remember to specify the protocol (TCP or UDP) and use --permanent for lasting changes.
Here's how to open port 8080 for TCP permanently:
sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent
sudo firewall-cmd --reload
sudo firewall-cmd --list-ports --zone=publicFirewall Commands Check
Time for a quick check! Which of the following statements about firewall management in Linux are true?
Recap: Firewall Essentials
In this lesson, we explored how to manage firewalls on Linux systems to enhance security. We covered two main tools:
- UFW (Uncomplicated Firewall): A simple interface for
iptables, popular on Debian/Ubuntu, allowing easy control over services and ports. - Firewalld: A dynamic firewall manager used on RHEL/CentOS systems, which organizes rules into zones for flexible security policies.
Understanding these tools is crucial for securing your Linux server or workstation.
常见问题解答
「防火墙管理:`ufw`、`firewalld`、`iptables`」课时是免费的吗?
是的 — 「防火墙管理:`ufw`、`firewalld`、`iptables`」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Linux Command Line Mastery 课程的其余内容,请升级到 CoddyKit PRO。 Linux Command Line Mastery 课程共包含 4 节课。
「防火墙管理:`ufw`、`firewalld`、`iptables`」这节课中我会学到什么?
学习配置和管理防火墙,保护 Linux 系统免受未授权访问。 你通过在浏览器中直接运行的动手代码来练习 Linux Command Line Mastery,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Linux Command Line Mastery 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Linux Command Line Mastery 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「防火墙管理:`ufw`、`firewalld`、`iptables`」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Linux Command Line Mastery 课中编写并运行代码吗?
能。每节 Linux Command Line Mastery 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 网络诊断:`traceroute`、`nslookup`、`dig`
- 防火墙管理:`ufw`、`firewalld`、`iptables`
- 安全 Shell 密钥管理
- 使用 tcpdump 捕获并检查流量