Sub topic 4
Runtime and permanent configuration
This chapter assumes you can use sudo on a Linux server and manage services with systemctl. It also assumes you know that firewalld groups rules into zones, which the earlier chapters covered. Here you will learn that firewalld keeps two separate copies of its rules, how each firewall-cmd option affects them, and how to move changes between them without surprises.
Two configurations, not one
firewalld holds its rules in two places at once.
- The runtime configuration is what the firewall is enforcing right now. It lives in memory inside the
firewallddaemon. It is lost when the daemon restarts, when the server reboots, and when you runfirewall-cmd --reload. - The permanent configuration is what firewalld loads when it starts. It is stored as XML files. Your changes go in
/etc/firewalld/. The defaults shipped with the package live in/usr/lib/firewalld/and you should not edit them.
Most firewall mistakes in this area come from forgetting that these two copies can disagree. A rule that works in a test and disappears after a reboot was added to runtime only. A rule that you saved but cannot reach was added to permanent only.
The default behavior of firewall-cmd is the opposite of what many people expect.
| Command form | Changes runtime | Changes permanent | Effect on traffic |
|---|---|---|---|
| `firewall-cmd --add-service=http` | Yes | No | Immediate, lost on reload or reboot |
| `firewall-cmd --permanent --add-service=http` | No | Yes | None until a reload or restart |
| `firewall-cmd --reload` | Replaced by permanent | Unchanged | Runtime now matches permanent |
| `firewall-cmd --runtime-to-permanent` | Unchanged | Replaced by runtime | None, saves what is already active |
Read the diagram as two stores with one-way copy operations between them. No option writes to both stores in a single command. The exception is a few settings, such as --set-default-zone, which firewalld applies to both at once.
Seeing the difference
Without --permanent, the --list-* and --query-* options show runtime. With --permanent, they show the saved files. Compare the two to find drift:
sudo firewall-cmd --list-services
sudo firewall-cmd --permanent --list-services
diff <(sudo firewall-cmd --list-all) <(sudo firewall-cmd --permanent --list-all)If diff prints nothing, the two configurations match for the default zone. Any line starting with < exists only in runtime. Any line starting with > exists only in permanent. To compare another zone, add --zone=NAME to both commands.
Here is example output on a server where http was added to runtime only:
$ sudo firewall-cmd --list-services
cockpit dhcpv6-client http ssh
$ sudo firewall-cmd --permanent --list-services
cockpit dhcpv6-client sshThe http service is allowed now but will vanish at the next reload. Run this comparison before any reboot or reload on a server where several people have worked, and whenever behavior differs from what the saved files suggest.
Workflow A: test in runtime, then save
This workflow applies a change live, tests it, and saves it only after it works. It suits a change that could break access, because a mistake vanishes on the next reload. The --timeout option adds a safety net by removing the rule automatically after the given time.
sudo firewall-cmd --add-service=http --timeout=5m
sudo firewall-cmd --list-services
sudo firewall-cmd --runtime-to-permanentWhat each step does:
--add-service=http --timeout=5mallows HTTP in the default zone's runtime configuration. After five minutes firewalld removes the rule by itself. The time is a number with an optional unit:sfor seconds,mfor minutes,hfor hours. A bare number means seconds.--list-servicesconfirms the rule is active. This is also the point to test from a client.--runtime-to-permanentwrites the whole current runtime configuration to the permanent files.
Keep three points in mind.
--timeoutworks only on runtime changes. firewalld rejects it combined with--permanent, because saved rules have no expiry.- Saving does not cancel the timeout. If you run
--runtime-to-permanentwhile the timed rule is still active, the permanent files get the rule, but the runtime copy still expires and disappears after the time runs out. The rule returns at the next reload or reboot. For a rule you intend to keep, either re-add it without--timeoutbefore saving, or run--permanent --add-service=httpexplicitly. --runtime-to-permanentsaves everything in runtime, not only your latest change. If you or a colleague added a temporary rule an hour ago, it becomes permanent too. Run thediffcommand above first and read what you are about to save.
Workflow B: write permanent, then reload
This workflow edits the saved configuration first, then loads it. The result is a runtime state that exactly matches the files. It suits changes you are already sure about, and it is the usual way to script a firewall.
sudo firewall-cmd --permanent --add-port=8080/tcp
sudo firewall-cmd --reload
sudo firewall-cmd --query-port=8080/tcp
sudo firewall-cmd --list-portsWhat each step does:
--permanent --add-port=8080/tcprecords TCP port 8080 in the default zone's XML file. Traffic is not affected yet. The command printssuccess.--reloadmakes firewalld read the permanent files and make them the runtime configuration. This also discards every runtime-only change.--query-port=8080/tcpqueries runtime and printsyesorno. Ayesproves the rule is live. It also sets the exit status, which is useful in scripts.--list-portsshows the whole list so you can see what else is open.
If you skip the reload, the permanent file has the port but traffic is still blocked. This is the most common cause of "I added the rule and it still does not work." A plain --query-port=8080/tcp without --permanent would have said no.
Choosing between the workflows
| Situation | Better choice | Reason |
|---|---|---|
| Rule could cut off your own access | Workflow A with `--timeout` | A mistake disappears by itself |
| Rule is well understood, such as opening a known service | Workflow B | Runtime ends up identical to the files |
| Several rules to apply together | Workflow B | One reload applies all of them |
| Configuration is managed by a script or a tool such as Ansible | Workflow B | Repeatable and does not depend on runtime state |
| You are unsure what runtime currently contains | Inspect, then Workflow B | Avoids saving stray experiments |
Workflow A has a trap beyond the ones above. Once you save, you have a permanent rule that may have been tested only from one client. Test from the same place your real users connect from, not only from the server itself.
What reload does and does not do
--reload is gentle. It re-reads the permanent files and applies the difference, and it keeps connection tracking state. Existing connections that the new rules still allow continue without interruption. An established SSH session stays up through a normal reload as long as SSH is still allowed afterward.
--complete-reload also throws away the connection tracking state. Established connections can be dropped, including your SSH session. Use it only when a normal reload does not fix a problem, such as after a kernel module change, and only from a console or with another way back in.
Restarting the service with systemctl restart firewalld has the same effect as a reload for configuration purposes: runtime is rebuilt from the permanent files. It also briefly interrupts filtering, so prefer --reload.
Two more behaviors are worth knowing:
- A reload does not warn you about runtime-only rules it is about to discard. They are simply gone. If you suspect unsaved work, run the
diffcommand first. --permanentcommands are checked when you run them, but an error in a hand-edited XML file may only show up at reload. Read the output of--reload. It should printsuccess. If it prints an error, fix the file and reload again before you rely on the firewall.
When the daemon is not running
firewall-cmd without --permanent talks to the running daemon and fails if it is stopped, with an error such as FirewallD is not running. Check first with sudo firewall-cmd --state. The permanent edits made with firewall-offline-cmd work without the daemon, which is useful when preparing a server image. After systemctl enable --now firewalld, the daemon loads the permanent files as its starting runtime.
A short checklist
- Run
sudo firewall-cmd --stateto confirm the daemon is up. - Run the
diffcomparison to see whether runtime and permanent already disagree. - Make the change with Workflow A or B.
- Confirm with a runtime
--query-*or--list-*command. - Confirm the permanent copy with the same command plus
--permanent. - Test from a client machine.
The next chapter builds on step 3 and covers how to apply changes without locking yourself out of SSH.
Exercise
You run sudo firewall-cmd --permanent --add-port=8443/tcp on a server, then test from a client and the connection is refused. sudo ss -tlnp shows the application is listening on 8443. What is the most likely cause, which command shows it, and what fixes it?
Show answer
The change was written to the permanent configuration only, so the running firewall does not have it yet. sudo firewall-cmd --query-port=8443/tcp prints no, while sudo firewall-cmd --permanent --query-port=8443/tcp prints yes. Run sudo firewall-cmd --reload so runtime is rebuilt from the permanent files, then query again and retest from the client.
Exercise
A colleague opened port 9999/tcp in runtime for a quick test and forgot about it. You then add http with sudo firewall-cmd --add-service=http and run sudo firewall-cmd --runtime-to-permanent. What ends up in the permanent configuration, and how could you have caught the problem before saving?
Show answer
Both http and port 9999/tcp are saved, because --runtime-to-permanent copies the entire runtime state. Before saving, run diff <(sudo firewall-cmd --list-all) <(sudo firewall-cmd --permanent --list-all) and read the lines starting with <. The 9999/tcp line would appear there, and you could remove it with sudo firewall-cmd --remove-port=9999/tcp first.
Exercise
You run sudo firewall-cmd --add-service=http --timeout=10m and, after confirming it works, immediately run sudo firewall-cmd --runtime-to-permanent. Ten minutes later HTTP traffic stops being accepted. Why, and what would you do differently?
Show answer
The timeout is a property of the runtime rule and is not cancelled by saving. After ten minutes firewalld removed the runtime rule. The permanent files still contain it, so it returns at the next reload or reboot. Either add the service again without --timeout before saving, or use sudo firewall-cmd --permanent --add-service=http followed by sudo firewall-cmd --reload.