NewCode CLOUD — 20% off your first month on all cloud VPSClaim

Firewalld Tutorial for AlmaLinux and Rocky Linux: Zones, Services, Ports and Rich Rules

On AlmaLinux, Rocky Linux and other RHEL-family systems, the standard firewall is firewalld. It sits on top of nftables and manages rules through zones and services, which makes common tasks simple once the model clicks. If you use Ubuntu or Debian, see the UFW guide instead.

Check the state

sudo systemctl status firewalld
sudo firewall-cmd --state
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --list-all

If firewalld is not installed or running:

sudo dnf install firewalld
sudo systemctl enable --now firewalld

Before enabling it on a remote server, make sure SSH is allowed (it is in the default public zone), or you will lose access. If that happens, use the VNC console in the control panel.

Runtime vs permanent

firewalld has two configurations:

  • Runtime — active now, lost on reload or reboot.
  • Permanent — saved to disk, applied on reload or reboot.

The safe workflow on a remote server:

sudo firewall-cmd --add-service=https              # runtime: test it
sudo firewall-cmd --runtime-to-permanent           # keep it

Or add --permanent and then --reload:

sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

If a runtime change locks you out, a reload or reboot restores the permanent rules.

Zones

A zone is a trust level applied to network interfaces or source addresses. The most useful ones:

Zone Default behaviour
public Untrusted network; only selected services allowed (default for servers)
internal / trusted More or all traffic allowed — for private interfaces such as WireGuard
drop Silently drop everything incoming
block Reject incoming with an error

Most servers only need public. See which interface is in which zone:

sudo firewall-cmd --get-active-zones

Services and ports

A service is a named set of ports. List them with firewall-cmd --get-services.

# Web server
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https

# Custom port
sudo firewall-cmd --permanent --add-port=8080/tcp
sudo firewall-cmd --permanent --add-port=51820/udp

# Remove
sudo firewall-cmd --permanent --remove-service=cockpit
sudo firewall-cmd --permanent --remove-port=8080/tcp

sudo firewall-cmd --reload
sudo firewall-cmd --list-all

Many minimal images enable cockpit and dhcpv6-client by default. Remove services you do not use.

Allow a port only from specific IPs

Exposing SSH, databases or admin panels to the whole internet is a common mistake. Rich rules allow by source:

# SSH only from the office
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="198.51.100.7/32" service name="ssh" accept'
sudo firewall-cmd --permanent --remove-service=ssh

# MySQL only from the app server
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.20" port port="3306" protocol="tcp" accept'

sudo firewall-cmd --reload

Test SSH from the allowed address in a new session before closing the old one.

Using a dedicated zone for trusted sources

For several admin IPs, put them in a zone:

sudo firewall-cmd --permanent --new-zone=admin
sudo firewall-cmd --permanent --zone=admin --add-source=198.51.100.7
sudo firewall-cmd --permanent --zone=admin --add-source=192.0.2.0/28
sudo firewall-cmd --permanent --zone=admin --add-service=ssh
sudo firewall-cmd --reload

Traffic from those sources is handled by the admin zone; everyone else falls through to public.

Block an IP or network

sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.66" drop'
sudo firewall-cmd --reload

For long blocklists, use an ipset, which handles thousands of entries efficiently:

sudo firewall-cmd --permanent --new-ipset=blocklist --type=hash:net
sudo firewall-cmd --permanent --ipset=blocklist --add-entry=203.0.113.0/24
sudo firewall-cmd --permanent --ipset=blocklist --add-entries-from-file=/root/bad-nets.txt
sudo firewall-cmd --permanent --zone=drop --add-source=ipset:blocklist
sudo firewall-cmd --reload

For automatic blocking of attackers, combine firewalld with Fail2ban (banaction = firewallcmd-rich-rules).

Rate-limit new connections

Rich rules can limit how fast new connections are accepted, which slows brute-force attempts:

sudo firewall-cmd --permanent --add-rich-rule='rule service name="ssh" limit value="10/m" accept'

Remove the plain ssh service so this rule applies. For HTTP, rate limiting in the web server is usually more precise — see Nginx rate limiting.

Port forwarding and NAT

Forward an external port to another port on the same machine:

sudo firewall-cmd --permanent --add-forward-port=port=80:proto=tcp:toport=8080

Forward to another machine (for example over a WireGuard tunnel) requires masquerading:

sudo firewall-cmd --permanent --add-masquerade
sudo firewall-cmd --permanent --add-forward-port=port=8443:proto=tcp:toport=443:toaddr=10.10.0.2
sudo firewall-cmd --reload

Trust a private interface

If you use a WireGuard private network, put wg0 in a more permissive zone so internal services are reachable over the tunnel but not from the internet:

sudo firewall-cmd --permanent --zone=internal --add-interface=wg0
sudo firewall-cmd --permanent --zone=internal --add-port=3306/tcp
sudo firewall-cmd --reload

Docker and firewalld

Docker manages its own firewall rules and creates a docker zone. Published container ports (-p 8080:80) are reachable from the internet even if you did not open them in firewalld’s public zone. To keep a container private, publish it on localhost only: -p 127.0.0.1:8080:80, and put a reverse proxy in front. See installing Docker.

Logging dropped packets

sudo firewall-cmd --set-log-denied=unicast
sudo journalctl -k | grep -i "filter_" | tail

Turn it off again (--set-log-denied=off) when you are done, as it can be noisy.

A sensible baseline for a web server

sudo firewall-cmd --permanent --remove-service=cockpit
sudo firewall-cmd --permanent --remove-service=dhcpv6-client
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
# SSH stays open (or restrict to your IPs as shown above)
sudo firewall-cmd --reload
sudo firewall-cmd --list-all

Then check from outside what is actually reachable:

nmap -Pn 203.0.113.10        # from another machine