Skip to content
Visual explainer 3h to learn 4 steps

firewalld & ufw

Open only the required services and ports and make the rules permanent.

By Invicly Tech, , in Linux Administration (RHCSA-style). About 10 minutes to read.

Topic 26 of 42

In short

A host firewall allows only the traffic your services need. On RHEL, firewalld organises rules into zones and uses predefined services such as http and ssh; on Ubuntu, ufw offers a simpler front end. Both sit on top of nftables in the kernel.

Explained simply

A firewall is a guard at the door of your server. It checks every incoming connection and lets in only the kinds you allowed, such as SSH or HTTPS. Everything else is turned away, so a forgotten test service is not reachable from the network.

On RHEL, the guard is called firewalld, and you talk to it with firewall-cmd. On Ubuntu, the usual tool is ufw, the Uncomplicated Firewall. Both are friendly front ends: the real filtering happens inside the Linux kernel, in a system called nftables.

A key idea for firewalld is runtime versus permanent. A rule you add normally works at once but is forgotten at the next reload or reboot. A permanent rule is saved to disk, but only starts working after a reload. Mixing these up is the most common firewall mistake.

The golden rule on a remote server: always allow SSH before you turn the firewall on or tighten it, or you can lock yourself out.

Press Play on a diagram to walk through it one step at a time.

Comparefirewalld vs ufw
firewalld (RHEL)
  • Zones such as public and internal
  • firewall-cmd --add-service=http
  • --permanent plus --reload to persist
  • --list-all shows the active zone
ufw (Ubuntu)
  • Default deny incoming, allow outgoing
  • ufw allow 80/tcp or ufw allow OpenSSH
  • Rules persist automatically
  • ufw status verbose shows rules
CompareRuntime vs permanent in firewalld
Runtime (no --permanent)
  • Takes effect immediately
  • Lost on --reload or reboot
  • Good for testing
  • Save with --runtime-to-permanent
Permanent (--permanent)
  • Saved to /etc/firewalld
  • No effect until --reload
  • Survives reboots
  • Check with --permanent --list-all
LayersFrom your command to the kernel
  1. 1
    firewall-cmd or ufw

    What you type

  2. 2
    firewalld daemon or ufw scripts

    Zones, services, saved rules

  3. 3
    nftables rule set

    nft list ruleset shows it

  4. 4
    Netfilter in the kernel

    Checks every packet

In depth

Zones: how firewalld decides which rules apply

firewalld groups rules into zones. Each zone is a trust level with its own list of allowed services and ports. Built-in zones range from drop (silently drop everything incoming) and block (reject with an error), through public (the default: only listed services, typically ssh, dhcpv6-client and cockpit on RHEL), to trusted, which allows everything.

When a new connection arrives, firewalld picks one zone for it. First it checks sources: if the client address matches a source bound to a zone, that zone wins. Otherwise it uses the zone of the interface the packet came in on. If the interface has no zone, the default zone applies. That lets you, for example, allow SSH only from the admin network by binding 10.0.10.0/24 to the internal zone and leaving ssh out of public.

firewall-cmd --get-active-zones shows which zones are in use and why, and --get-default-zone shows the fallback.

  • --set-default-zone=public
  • --zone=internal --add-source=10.0.10.0/24
  • --zone=internal --change-interface=ens192
FlowHow firewalld picks a zone for a new connection
  1. 1
    Packet arrives

    From 10.0.10.7 on ens160 to port 22

  2. 2
    Source match?

    10.0.10.0/24 is bound to zone internal

  3. 3
    Else: interface zone

    ens160 belongs to public

  4. 4
    Else: default zone

    Used when nothing else matches

  5. 5
    Apply zone rules

    Allowed service or port, or reject

Runtime, permanent and reload

firewalld keeps two configurations. Runtime is what the kernel enforces right now. Permanent is what is saved in /etc/firewalld/ and loaded at start or on --reload. Without --permanent, a command changes runtime only. With --permanent, it changes the saved config only, and nothing happens until you reload.

Two safe workflows exist. Change runtime first, test, then copy everything with --runtime-to-permanent. Or add rules with --permanent and run --reload. A reload throws away runtime-only changes, which is useful as an undo but surprising if you forgot to save.

You can also add a temporary rule with --timeout=5m, which removes itself after five minutes: a good safety net when changing SSH access remotely. --list-all shows the runtime state of a zone, and --permanent --list-all shows the saved one; comparing them reveals unsaved changes.

  • --runtime-to-permanent saves tested rules
  • --permanent --list-all shows saved rules
  • --timeout=5m adds a self-removing runtime rule

Services, ports and rich rules

A firewalld service is a named bundle of ports and protocols. http is TCP 80, https TCP 443, and some services open several ports at once. firewall-cmd --get-services lists them, and --info-service=NAME shows what each contains. The definitions live in /usr/lib/firewalld/services/ as XML; custom ones go in /etc/firewalld/services/.

If no service fits, open a port directly: --add-port=8443/tcp or a range like --add-port=60000-60100/udp. Prefer services when one exists, because the name says why the rule is there.

For conditions, use rich rules. For example, --add-rich-rule='rule family="ipv4" source address="10.0.10.0/24" service name="ssh" accept' allows SSH only from one network. Rich rules can also reject, drop, log or rate-limit. Remember that opening a non-standard port for a service on RHEL may also need an SELinux port label, added with semanage port.

  • --info-service=https
  • --list-services and --list-ports
  • nft list ruleset shows the real kernel rules

ufw on Ubuntu

ufw starts disabled. When enabled, its defaults deny incoming and allow outgoing connections. Rules are saved automatically; there is no runtime versus permanent split.

Simple rules read like English: ufw allow 443/tcp, ufw allow OpenSSH (an application profile; ufw app list shows them), or ufw allow from 10.0.10.0/24 to any port 22 proto tcp. ufw limit OpenSSH allows SSH but denies an address that opens 6 or more connections within 30 seconds, a cheap defence against password guessing. ufw deny drops traffic silently, ufw reject sends an error back.

Rules are checked in order and the first match wins. ufw status numbered shows the order, ufw delete 3 removes rule 3, and ufw insert 1 allow from 10.0.10.5 puts a rule at the top. One famous trap: Docker publishes container ports with its own rules that are evaluated before ufw's, so a published container port can be reachable even when ufw seems to block it.

  • sudo ufw allow OpenSSH before sudo ufw enable
  • sudo ufw status verbose
  • sudo ufw status numbered, then sudo ufw delete N

Learn it in 4 short steps

About 45m each. Track them with a timer in your plan.

  1. 1
    Explain zones in firewalld~45m
  2. 2
    Allow services and ports permanently~45m
  3. 3
    Reload and verify active rules~45m
  4. 4
    Manage rules with ufw on Ubuntu~45m

Key points

  • Without --permanent, firewalld changes are lost on reload or reboot.
  • Use firewall-cmd --runtime-to-permanent to save tested runtime rules.
  • Allow SSH before enabling ufw on a remote machine.
  • A service may also need an SELinux port label if it listens on a non-standard port.

Key terms

Tap a card to flip it. 0/5 known

Examples

bash
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --permanent --add-service=http --add-service=https
sudo firewall-cmd --permanent --add-port=8080/tcp
sudo firewall-cmd --reload && sudo firewall-cmd --list-all
# Ubuntu
sudo ufw allow OpenSSH && sudo ufw allow 80/tcp && sudo ufw enable
sudo ufw status verbose

Open web ports permanently with firewalld, or with ufw on Ubuntu.

A web server that only admins can SSH into

Goal: anyone may reach HTTP and HTTPS, but SSH is allowed only from the admin network 10.0.10.0/24. Step 1: add the web services permanently to public. Step 2: bind the admin network as a source to the internal zone, which allows ssh by default. Step 3: before removing ssh from public, make sure you are connected from the admin network, or keep a console open. Step 4: reload and verify both zones.

bash
sudo firewall-cmd --permanent --zone=public --add-service=http --add-service=https
sudo firewall-cmd --permanent --zone=internal --add-source=10.0.10.0/24
sudo firewall-cmd --permanent --zone=internal --list-services   # includes ssh
sudo firewall-cmd --permanent --zone=public --remove-service=ssh
sudo firewall-cmd --reload
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --zone=public --list-all

Lock down a fresh Ubuntu server with ufw

A new Ubuntu VM should accept only SSH (rate-limited) and HTTPS. Step 1: set the defaults explicitly. Step 2: allow SSH first, using limit to slow down brute force. Step 3: allow 443. Step 4: enable ufw; it warns that this may disrupt SSH, which is fine because SSH is already allowed. Step 5: check the numbered list.

bash
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw limit OpenSSH
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status numbered

Quick reference

  • firewall-cmd --get-active-zones shows which zones apply to which interfaces and sources
  • firewall-cmd --permanent --add-service=https && firewall-cmd --reload opens HTTPS for good
  • firewall-cmd --runtime-to-permanent saves all tested runtime rules
  • firewall-cmd --list-all shows the active zone's services, ports and rich rules
  • firewall-cmd --add-service=ssh --timeout=5m adds a rule that removes itself
  • ufw: allow SSH, then ufw enable; ufw status numbered and ufw delete N manage rules
  • ufw limit OpenSSH blocks an IP after 6 connections in 30 seconds
  • Docker-published ports can bypass ufw rules; check with ss and an outside scan

Common mistakes

  • Adding rules without --permanent and losing them at the next reboot.
  • Enabling ufw over SSH before allowing SSH.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1You ran firewall-cmd --permanent --add-port=8080/tcp, but 8080 is still blocked. What is missing?

Question 2

2A packet's source address is bound to zone internal, and its interface belongs to public. Which zone handles it?

Question 3

3What does firewall-cmd --reload do to rules added without --permanent?

Question 4

4Which ufw command removes the third rule in the list?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Show the default zone, the active zones, and everything allowed in the active zone on your RHEL machine.

  2. Exercise 2medium

    Open TCP 8443 for testing, confirm it works, then save it permanently without typing the rule a second time.

  3. Exercise 3hard

    Allow PostgreSQL (5432/tcp) only from 10.0.20.0/24 with a rich rule, permanently, and show that other sources are still blocked.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Should I use firewalld or ufw?
Use the one your distribution ships with: firewalld on RHEL, Rocky, Alma and Fedora, ufw on Ubuntu. Do not run both, or iptables and nftables rules by hand on top, because they will fight over the rules.
Why do my firewall rules disappear after a reboot?
With firewalld they were probably added without --permanent. Add them again with --permanent and reload, or save the current state with --runtime-to-permanent.
Does ufw protect Docker containers?
Not by default. Docker inserts its own rules for published ports, which are checked before ufw's. Bind published ports to 127.0.0.1 or use Docker's DOCKER-USER chain to restrict them.
How do I check which ports are actually reachable?
Run ss -tulpn on the server to see what listens, then test from another machine with nc -zv host port or nmap, because only an outside test shows the firewall's real effect.

Go deeper

ShareXLinkedInWhatsApp