Sub topic 1
How firewalld and firewall-cmd fit together
Prerequisites
This chapter assumes you can do the following:
- Use the Linux command line and run commands with
sudo. - Explain what a TCP or UDP port is, and what it means for a service to listen on one.
- Manage services with
systemctl(start, stop, enable, status). - Read IPv4 addresses and CIDR notation such as
203.0.113.0/24.
You also need a Linux server where you have sudo rights. The examples use a Red Hat family system (RHEL, Rocky Linux, AlmaLinux, Fedora), where firewalld is the default firewall. It also works on Debian and Ubuntu if you install it. Do not run it next to another firewall front end such as ufw.
What firewalld is
A firewall decides which network packets may reach your server and which are discarded. On Linux the filtering happens inside the kernel, in a framework called netfilter. You do not talk to the kernel directly. You use a tool that translates your intent into kernel rules.
firewalld is a daemon, meaning a long-running background service. It does three jobs:
- It keeps the firewall configuration: which zones exist, which services and ports are allowed, and which rich rules apply.
- It turns that configuration into rules for the kernel packet filter. On current distributions the backend is nftables. Older releases such as RHEL 7 used iptables.
- It lets you change rules while the system runs, without flushing everything and reloading a whole rule file.
That last point is the main reason to use firewalld. On a plain iptables setup, a typical change means rewriting a rules file and reloading it, which can drop established connections. firewalld applies a small change to the running rule set, so a connection you already have, such as your SSH session, normally keeps working.
What firewall-cmd is
firewall-cmd is the command-line client. It does not edit rules itself. It sends a request to the firewalld daemon over D-Bus, the Linux message bus. The daemon checks that you are allowed to make the change, updates its configuration, and updates the kernel rules.
Other clients talk to the same daemon: the graphical firewall-config tool and the Cockpit web console. All of them see the same state, because the state lives in the daemon.
This design has two consequences you will meet again and again:
- If the daemon is not running,
firewall-cmdhas nothing to talk to and fails withFirewallD is not running. - If something else changes the kernel rules directly, for example a manual
nftcommand, firewalld does not know about it. It may overwrite those changes on the next reload.
Where the configuration lives
firewalld reads two directory trees.
| Location | Purpose | Edit it? |
|---|---|---|
| `/usr/lib/firewalld/` | Defaults shipped by the package: zones, predefined services, ICMP types | No. A package update replaces it. |
| `/etc/firewalld/` | Your changes: the saved zone files, custom services, and `firewalld.conf` | Normally through `firewall-cmd`. Editing XML by hand is possible but easy to get wrong. |
When a file with the same name exists in both places, the one in /etc/firewalld/ wins. This is how a default service definition can be overridden without touching the packaged file.
The main settings file is /etc/firewalld/firewalld.conf. It holds the default zone, the logging option for denied packets, and the choice of backend.
The building blocks
You will use the same few objects throughout this tutorial. Each has its own chapter later. Here is only enough to read a command.
- A zone is a named set of rules, such as
public,internal, ordrop. Each incoming connection is matched to exactly one zone, based on its source address or the network interface it arrived on. - A service is a named group of ports and protocols. The service
httpstands for TCP port 80, andsshstands for TCP port 22. - A port is a port number plus a protocol, written like
8080/tcp. - A source is an IP address or network in CIDR notation that you assign to a zone.
- A rich rule is a more detailed rule that can match a specific source and port, and then accept, reject, or drop.
Most of the time you place a service or port into a zone, and firewalld allows matching traffic that arrives through that zone.
Traffic that no rule allows is handled by the zone's default behavior. In the stock zones, that means the packet is rejected, with an ICMP error sent back to the sender. The drop zone is the exception: it discards packets silently.
Anatomy of a firewall-cmd command
Nearly every command follows the same pattern:
firewall-cmd [--permanent] [--zone=ZONE] ACTION--permanentwrites the change to the saved configuration instead of the running one. The next chapters explain why this matters.--zone=ZONEpicks the zone to act on. If you leave it out, the command uses the default zone.- The action is one of three kinds:
--add-...and--remove-...change something,--list-...shows it, and--query-...answers yes or no.
Query commands are designed for scripts. They print yes or no and also set the exit status, which is 0 for yes and 1 for no.
sudo firewall-cmd --query-service=ssh && echo "ssh is allowed"Commands that change or show the firewall need root rights, so the examples use sudo. A few read-only commands such as --state can run as a normal user, but using sudo consistently avoids surprises.
Starting firewalld and looking around
The first step on any server is to confirm that the daemon is installed and running. If it is not installed, install it with sudo dnf install firewalld on the Red Hat family, or sudo apt install firewalld on Debian and Ubuntu.
This sequence enables the daemon at boot, starts it now, and then inspects it:
sudo systemctl enable --now firewalld
sudo firewall-cmd --state
sudo firewall-cmd --get-default-zone
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --list-allWhat each command tells you:
systemctl enable --now firewalldstarts the daemon immediately and makes it start on every boot. Withoutenable, the server would come back after a reboot with no firewall.--stateprintsrunningwhen the daemon is up. If it is not, you getnot running, and the exit status is non-zero.--get-default-zoneprints the zone that applies to anything not assigned elsewhere. On many Red Hat family installs this ispublic.--get-active-zoneslists zones that currently have an interface or source assigned, and shows which ones.--list-allshows everything configured in the default zone: interfaces, sources, services, ports, and rich rules.
A typical --list-all result on a fresh server looks like this:
public (active)
target: default
icmp-block-inversion: no
interfaces: eth0
sources:
services: cockpit dhcpv6-client ssh
ports:
protocols:
forward: yes
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:The exact list of services differs between distributions and versions. Read this output as a statement: "traffic arriving on eth0 is matched to the zone public, and the only things allowed in are the services cockpit, dhcpv6-client, and ssh." Everything else is refused by the default target.
Runtime and permanent: a first look
firewalld keeps two versions of its configuration at the same time.
- The runtime configuration is what the kernel enforces right now. It is lost when the daemon restarts or the server reboots.
- The permanent configuration is what is saved on disk. It is loaded when the daemon starts, and when you run
firewall-cmd --reload.
A command without --permanent changes only the runtime version. A command with --permanent changes only the saved version, and does not affect the running firewall until a reload. This is the most common source of confusion for new users: you open a port, test it, reboot, and the port is closed again. Or you add a permanent rule, test it right away, and it does not work yet.
Chapter 4 covers this in full, together with the safe ways to change the firewall without losing access. For now, remember that adding a rule has two parts, a runtime change and a permanent one, and that you must make sure both are in the state you want.
Common mistakes at this stage
- Running both firewalld and another firewall tool.
ufw, a hand-writtennftables.service, and firewalld each assume they control the rule set. Choose one. Check the others withsystemctl is-enabled ufw nftables. - Editing files in
/usr/lib/firewalld/. A package update silently reverts your edit. Usefirewall-cmd, or put overrides in/etc/firewalld/. - Assuming the daemon survives a reboot. On some minimal installs firewalld is installed but not enabled. Check with
systemctl is-enabled firewalld. - Looking at the wrong zone.
--list-allwithout--zoneshows the default zone, which may not be the zone your interface uses. Compare it with the output of--get-active-zones. - Running
firewall-cmdinside a container or minimal image. Without the daemon, you get thenot runningerror, and this does not mean the host has no firewall.
Exercises
Exercise
You run sudo firewall-cmd --list-all and see that the services ssh and cockpit are allowed. A colleague says that the whole server therefore allows only SSH and Cockpit. What is wrong with that conclusion?
Show answer
--list-all shows only the default zone, unless --zone is given. Traffic is matched to a zone by its source address or its interface, so some traffic may belong to another zone with different rules. Run --get-active-zones to see which zones are in use and which interfaces or sources they hold, and --list-all-zones to see all of their rules. The output also shows only the runtime configuration, so saved but not yet loaded permanent rules are not included.
Exercise
A script must continue only if the http service is allowed in the default zone. Which firewall-cmd option fits, and how does the script learn the answer?
Show answer
Use firewall-cmd --query-service=http. It prints yes or no and returns exit status 0 for yes and 1 for no, so the script can test it directly, for example with if sudo firewall-cmd --query-service=http; then ....