Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Before You Change the Firewall

  • SSH stays allowed: the default zone allows the ssh service, which is port 22. If SSH uses another port, add that port before you remove the service or change zones; see how to change the SSH port.
  • Keep your session open and test a new login from a second terminal.
  • Know the way back: find the server console in your provider's control panel.
  • Try with a timeout: a runtime rule added with --timeout=10m removes itself after ten minutes; the option cannot be combined with --permanent.
  • One tool: while firewalld runs it owns the rule set; do not add iptables or nft rules by hand next to it.

Check That firewalld Is Running

sudo firewall-cmd --state
sudo systemctl status firewalld

A standard AlmaLinux or Rocky Linux installation has it active; some cloud images ship without it. To install it:

sudo dnf install firewalld
sudo firewall-offline-cmd --add-port=2222/tcp
sudo systemctl enable --now firewalld

The middle line is only needed when SSH uses a port other than 22: firewall-offline-cmd changes the permanent configuration while the service is stopped, so the port is open from the first second. firewalld also exists for Debian and Ubuntu, but never run it next to ufw.

Zones

firewalld groups rules into zones. Each network interface, and optionally each source address, belongs to one zone, and that zone's rules apply to its traffic.

sudo firewall-cmd --get-default-zone
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --list-all
ZoneWhat it does
publicThe usual default: only listed services pass.
dropDrops incoming traffic without a reply.
blockRejects incoming traffic with an error.
internalFor private networks; allows a few more services.
trustedAccepts everything. Use with care.

--list-all shows the services, ports, and rich rules of the default zone. On a fresh server, the services line usually holds ssh and dhcpv6-client, and often cockpit.

Open a Service or a Port

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --permanent --add-port=8080/tcp
sudo firewall-cmd --reload

A service is a named set of ports; sudo firewall-cmd --get-services lists them all. Use a service when one exists and a port when it does not. Add --zone=internal to change a zone other than the default.

Runtime and Permanent Rules

RuntimePermanent
Set withNo flag--permanent
Takes effectAt onceAfter --reload
Survives a reload or rebootNoYes

There are two ways to work: add rules with --permanent and reload, or test them at runtime first and, once they work, save the whole runtime state with sudo firewall-cmd --runtime-to-permanent. A reload discards runtime rules that were never saved.

Rich Rules

For anything more specific than "open this port to everyone", use a rich rule. This one lets one address reach MySQL:

sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.10" port port="3306" protocol="tcp" accept'
sudo firewall-cmd --reload
sudo firewall-cmd --list-rich-rules

The values inside the rule are quoted, and the whole rule is in single quotes. To remove it, repeat it with --remove-rich-rule. For a network, write the source as 203.0.113.0/24; for IPv6, use family="ipv6" with an IPv6 address.

Remove Rules

sudo firewall-cmd --permanent --remove-service=http
sudo firewall-cmd --permanent --remove-port=8080/tcp
sudo firewall-cmd --reload

Troubleshooting

  • An error at once: the default zones reject unmatched packets, so a client usually sees an error such as "No route to host" rather than a timeout. A timeout points to another firewall, such as one at your provider, or to the drop zone.
  • Rule added, nothing changed: it was added with --permanent and not reloaded, or to a zone the interface does not use; check --get-active-zones.
  • A service on a new port will not start: SELinux lets each service use only the ports labeled for it; see what is SELinux.
  • Docker containers: ports published by Docker bypass firewalld's zone rules; see Docker port mapping.

To confirm what is listening and reachable, see how to check open ports in Linux.

Was this answer helpful?
Loading...