Skip to content

Intermediate · Software and networking · topic 23 of 57 · 30 min

firewalld: zones, services and ports

Explanation

firewalld decides which incoming network traffic is allowed. Traffic is sorted into zones (public by default), and you allow services (like http) or ports (like 8080/tcp) in a zone with firewall-cmd. Changes are runtime-only unless you add --permanent, and permanent changes need --reload to take effect.

Why a firewall?

A server may run many programs that listen on the network. Some should be reachable from outside (the website on port 443), many should not (a database admin port). A firewall is a filter in the kernel that accepts or drops each incoming packet based on rules. On RHEL, firewalld manages those rules for you, and firewall-cmd is its command-line tool.

Think of it like this: firewalld is the security guard at an office gate with a list per gate. The main gate (public zone) lets in only visitors with an appointment for reception (ssh) and the showroom (http). The staff gate (internal zone) lets in more. You tell the guard which list a visitor belongs to by where they come from.

Zones

A zone is a named set of rules for a level of trust. Each network interface, or each source address range, belongs to one zone. Anything not assigned uses the default zone, normally public. In public, RHEL 9 allows only ssh, dhcpv6-client and cockpit (the web console) by default.

bash
# firewall-cmd --state
running
# firewall-cmd --get-default-zone
public
# firewall-cmd --get-active-zones
public
  interfaces: enp1s0
# firewall-cmd --list-all
public (active)
  target: default
  interfaces: enp1s0
  services: cockpit dhcpv6-client ssh
  ports:
  ...

Allowing services and ports

bash
# firewall-cmd --add-service=http --permanent
success
# firewall-cmd --add-service=https --permanent
# firewall-cmd --add-port=8080/tcp --permanent
# firewall-cmd --reload
success
# firewall-cmd --list-services
cockpit dhcpv6-client http https ssh
# firewall-cmd --list-ports
8080/tcp
# firewall-cmd --remove-service=cockpit --permanent && firewall-cmd --reload
A service is a named set of ports (http = 80/tcp). See them all with firewall-cmd --get-services.

Runtime versus permanent

firewalld keeps two configurations. The runtime one is what is active right now. The permanent one is saved on disk and loaded at boot or on --reload. Without --permanent, a change is active at once but lost at reload or reboot. With --permanent, it is saved but NOT active until you reload.

You runActive now?After reboot?
--add-service=httpYesNo
--add-service=http --permanentNoYes
--add-service=http --permanent, then --reloadYesYes
--add-service=http, then --runtime-to-permanentYesYes

Sources and other zones

bash
# firewall-cmd --zone=internal --add-source=10.10.0.0/16 --permanent   # this range uses zone internal
# firewall-cmd --zone=internal --add-service=mysql --permanent
# firewall-cmd --reload
# firewall-cmd --zone=internal --list-all
Now only addresses in 10.10.0.0/16 reach MySQL (port 3306); the public zone does not allow it.

For finer rules (allow one IP to one port, log and drop something), firewalld has rich rules, for example: firewall-cmd --add-rich-rule='rule family="ipv4" source address="203.0.113.7" service name="ssh" reject' --permanent. Use them when zones and services are not enough.

Common mistakes

  • Adding --permanent and forgetting --reload: the port stays closed until the next reboot, and you think the app is broken.
  • Adding a rule without --permanent: it works today and disappears after the next reboot. In exams, the grader reboots.
  • Opening the port but the service still fails: check that it listens on the right address (ss -tlnp), and, for a non-standard port, that SELinux allows it (module 6).
  • Turning the firewall off to "test". If you must, test with a specific rule instead; never leave it off.
Tip: After every firewall change run firewall-cmd --list-all and, from another machine, a real test such as curl http://server:8080.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

firewalld: zones, services and ports · 1/7

firewalld · firewall-cmd

firewalld filters incoming traffic. firewall-cmd is its command.

Prefer a video by a teacher? Find free videos on “firewalld firewall-cmd zones services permanent reload rhel” on YouTube ↗

In real life: Opening a new API on port 8443 at a logistics company

A logistics company in Chennai launches a tracking API on port 8443. The developer says "it works on the server but not from outside". The admin checks: the app listens on 0.0.0.0:8443 (ss -tlnp), but the public zone allows only ssh and https. He adds 8443/tcp permanently, reloads, and the delivery partners' apps connect.

bash
# firewall-cmd --add-port=8443/tcp --permanent
success
# firewall-cmd --reload
success
# firewall-cmd --list-ports
8443/tcp

Practice (0/3 done)

Task 1 · Question

Which pair of commands opens HTTPS now AND after reboot?

Task 2 · Question

Which line has the bug? 1: firewall-cmd --add-port=8080 --permanent 2: firewall-cmd --reload

Task 3 · Practice

Install httpd, serve a test page, and make it reachable from your host machine through the firewall, permanently. Then remove the cockpit service from the public zone.

Quick quiz

Question 1

1.What is the default zone on a fresh RHEL 9 server?

Question 2

2.You add a rule with --permanent only. When is it active?

Question 3

3.What is a firewalld "service"?

Question 4

4.Which command copies the current runtime rules into the permanent config?

Question 5

5.The port is open in firewalld but clients still cannot connect, and ss shows 127.0.0.1:9000. What is wrong?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

What is the default zone on a fresh RHEL 9 server?

Answer: public

public is the default.

You add a rule with --permanent only. When is it active?

Answer: After firewall-cmd --reload or a reboot

Permanent rules are loaded on reload or boot.

What is a firewalld "service"?

Answer: A named set of ports and protocols, like http = 80/tcp

Service definitions map names to ports.

Which command copies the current runtime rules into the permanent config?

Answer: firewall-cmd --runtime-to-permanent

--runtime-to-permanent saves what is active now.

The port is open in firewalld but clients still cannot connect, and ss shows 127.0.0.1:9000. What is wrong?

Answer: The app listens only on loopback

Change the app to listen on 0.0.0.0 or the server IP.

A cloud rule allows port 3389 from 0.0.0.0/0. What is the risk?Related

Answer: Remote Desktop is open to the whole internet

0.0.0.0/0 is every IPv4 address; exposed RDP is a top ransomware entry point.

From Firewalls

Which flag sets the port for scp?Related

Answer: -P

scp uses capital -P for the port; ssh uses lower-case -p.

From Linux networking for DevOps

Traffic to a server on port 443 is…Related

Answer: Usually HTTPS, but any program can use that port

Port numbers are conventions. Malware often uses 443 to blend in with normal traffic.

From Ports and protocols

What lets a stateful firewall allow a reply from a web server back to an inside user?Related

Answer: Its state table of sessions started from the inside

The firewall remembers the outgoing session and allows only matching replies.

From Firewalls, IPS and VPNs

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. This is the last topic of “Software and networking”. The module exam comes next.
  2. Module 4 exam: Software and networking · 14 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Storage: partitions, file systems and LVM. 57 topics left in the course before the final exam.

← Previous: NetworkManager with nmcli, the hostname and name resolution