On this page
- How does firewalld organise rules?
- How do I install and start firewalld?
- How do I test a change without risking SSH?
- How do I open services and ports?
- How do I allow a port only from trusted addresses?
- What can rich rules do?
- How do containers and SELinux interact with firewalld?
- How do I see what firewalld blocks?
- Questions
How does firewalld organise rules?
firewalld groups rules into zones, and every network interface or source address belongs to exactly one zone. On a server image the public interface lands in the public zone, which rejects incoming connections except for the services listed in it. A service is a named bundle of ports, such as ssh (TCP 22) or https (TCP 443). Rules exist in two copies: the runtime configuration the kernel uses right now, and the permanent configuration stored under /etc/firewalld that loads at boot or on reload.
That split is the safest feature of firewalld on a VPS without a browser console: you change the runtime copy, prove that SSH still works, and only then write it to disk. If something goes wrong, a reload throws away the experiment.
How do I install and start firewalld?
Most Rocky Linux 9 and AlmaLinux 9 images include it. Check, install if needed, and confirm the state:
rpm -q firewalld || sudo dnf install -y firewalld
firewall-cmd --version
sudo firewall-cmd --get-default-zone
sudo firewall-cmd --list-allBefore starting it on a server where it was off, make sure ssh appears in the public zone definition, because starting the daemon applies the permanent rules immediately:
sudo firewall-offline-cmd --zone=public --list-services
sudo firewall-offline-cmd --zone=public --add-service=ssh
sudo systemctl enable --now firewalldThe firewall-offline-cmd tool edits the stored configuration while the daemon is stopped.
How do I test a change without risking SSH?
Make the change at runtime only and keep an automatic reset scheduled. A reload restores the saved permanent rules, so a timer that reloads after a few minutes undoes any runtime mistake:
sudo systemd-run --on-active=5min --unit=fwd-undo /usr/bin/firewall-cmd --reload
sudo firewall-cmd --zone=public --remove-service=cockpit
sudo firewall-cmd --zone=public --add-service=httpsOpen a second SSH session from your computer. If it connects, save and cancel the timer:
sudo firewall-cmd --runtime-to-permanent
sudo systemctl stop fwd-undo.timerIf the new login fails, leave everything alone; the reload runs when the timer fires and your previous rules return. For single rules firewalld also accepts --timeout=5m, which adds a runtime rule that removes itself.
How do I open services and ports?
Prefer service names because they document intent. List the known ones and add what you need:
sudo firewall-cmd --get-services | tr " " "\n" | grep -E "^(http|https|ssh|wireguard)$"
sudo firewall-cmd --permanent --zone=public --add-service={http,https}
sudo firewall-cmd --permanent --zone=public --add-port=51820/udp
sudo firewall-cmd --permanent --zone=public --add-port=25565/tcp
sudo firewall-cmd --reload
sudo firewall-cmd --zone=public --list-allWith --permanent the change goes straight to disk and only applies after --reload, which is fine for ports that cannot affect SSH. Remove an entry with the matching --remove-service or --remove-port. The Cockpit service is open by default on many images; drop it if you do not use the Cockpit web console.
How do I allow a port only from trusted addresses?
Use a dedicated zone bound to source addresses. Traffic from those addresses is handled by that zone, everyone else falls through to public:
sudo firewall-cmd --permanent --new-zone=office
sudo firewall-cmd --permanent --zone=office --add-source=203.0.113.7/32
sudo firewall-cmd --permanent --zone=office --add-source=2001:db8:4f2a::7/128
sudo firewall-cmd --permanent --zone=office --add-service=ssh
sudo firewall-cmd --permanent --zone=office --add-port=5432/tcp
sudo firewall-cmd --reload
sudo firewall-cmd --get-active-zonesOnly once you have logged in from the office address through the new zone should you consider removing ssh from the public zone. Home connections often change address, so many people keep SSH public and protect it with keys and fail2ban instead.
What can rich rules do?
Rich rules express conditions that plain services cannot, such as a single source plus a port, logging, or rate limits:
sudo firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="198.51.100.50" port port="6379" protocol="tcp" accept'
sudo firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.0.2.0/24" drop'
sudo firewall-cmd --permanent --zone=public --add-rich-rule='rule service name="ssh" accept limit value="10/m"'
sudo firewall-cmd --reload
sudo firewall-cmd --zone=public --list-rich-rulesTo delete one, repeat the exact text with --remove-rich-rule. Note that a rich rule which accepts a service with a limit does not stop the plain ssh service entry from accepting too; remove the plain entry if the limit must apply to everyone.
How do containers and SELinux interact with firewalld?
Docker and Podman add their own zones and forwarding rules. On Rocky Linux 9 and AlmaLinux 9, Docker places its bridge in a docker zone, and published ports bypass the public zone. Publish container ports on 127.0.0.1 and expose them through a reverse proxy when they should not be public. SELinux is a separate layer: if you move a service to a new port, firewalld lets packets in but SELinux may still refuse the bind. Check the port label with sudo semanage port -l | grep http_port_t and see SELinux basics for adding a port.
How do I see what firewalld blocks?
Turn on logging of denied packets and read the kernel log:
sudo firewall-cmd --set-log-denied=unicast
sudo journalctl -k --since "15 min ago" | grep -i "filter_IN_public_REJECT"
sudo firewall-cmd --get-log-deniedSet it back to off when you are done, since busy servers produce many lines. Confirm what listens with sudo ss -tulpn. The emergency switch firewall-cmd --panic-on drops every packet, including your own session, so never use it on a remote server.
Questions about firewalld on Rocky Linux and AlmaLinux
Why did my rule disappear after a reload?
It was added without --permanent, so it only lived in the runtime configuration. Run sudo firewall-cmd --runtime-to-permanent to save the current runtime state, or add the rule again with --permanent.
Does firewalld handle IPv6?
Yes. Services and ports apply to both address families. Rich rules with family="ipv4" only match IPv4, so add a second rule with family="ipv6" when needed.
Can I run iptables or UFW next to firewalld?
Avoid it. firewalld uses nftables directly, and a second tool adding its own tables makes the result hard to predict. Choose one: firewalld on Rocky and AlmaLinux, UFW on Ubuntu and Debian.
What is the difference between drop and reject?
Reject answers with an error so the client fails quickly; drop sends nothing and the client waits for a timeout. The public zone rejects by default, which also makes your own troubleshooting faster.
How do I undo everything I changed?
The stored zones live in /etc/firewalld/zones. Removing a zone file there and running sudo firewall-cmd --reload returns that zone to the packaged default. Keep the SSH service in the zone before you reload.
More in Security
- UFW firewall on Ubuntu and Debian
- iptables rules that survive a reboot
- Block brute force logins with fail2ban
- CrowdSec intrusion prevention
- Two-factor SSH logins with TOTP
- Disable or restrict root login
- Alerts for every SSH login
- Advanced OpenSSH settings
- Publish SSH host keys in DNS (SSHFP)
- Audit open ports with nmap
- File integrity checks with AIDE and Tripwire
- CSF firewall (ConfigServer Security)
- First security steps on Rocky and Alma
- Free TLS certificates with Certbot
- Self-signed certificates and a private CA
- Rate and connection limits in Nginx
- Wazuh security monitoring
- Kernel and sysctl hardening