Intermediate · Software and networking · topic 23 of 57 · 30 min
firewalld: zones, services and ports
Explanation
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.
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.
# 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
# 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 --reloadRuntime 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 run | Active now? | After reboot? |
|---|---|---|
| --add-service=http | Yes | No |
| --add-service=http --permanent | No | Yes |
| --add-service=http --permanent, then --reload | Yes | Yes |
| --add-service=http, then --runtime-to-permanent | Yes | Yes |
Sources and other zones
# 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-allFor 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.
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 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.
# firewall-cmd --add-port=8443/tcp --permanent
success
# firewall-cmd --reload
success
# firewall-cmd --list-ports
8443/tcpPractice (0/3 done)
Which pair of commands opens HTTPS now AND after reboot?
Which line has the bug? 1: firewall-cmd --add-port=8080 --permanent 2: firewall-cmd --reload
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Repositories, BaseOS, AppStream and module streamsSoftware and networking · same module
- FirewallsCyber Security: from zero to SOC analyst · Identity and network defence
- Linux networking for DevOpsDevOps: Git, Docker, Kubernetes and CI/CD · Linux for DevOps
- Ports and protocolsCyber Security: from zero to SOC analyst · Networking for security
- Firewalls, IPS and VPNsComputer Networking (CCNA level) · Network security
- Ports, services and what scanning doesEthical Hacking and Penetration Testing (legal labs) · Scanning with nmap
Questions people ask about this
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
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.
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
- This is the last topic of “Software and networking”. The module exam comes next.
- Module 4 exam: Software and networking · 14 questions · 25 min, without help. 5 topics of this module still to finish first.
- 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