Mastering firewall-cmd: The Complete Guide to firewalld in Linux

Now that we have covered IPtables and UFW, let’s dive into firewalld.

📚 Table of Contents

  1. What is firewalld?
  2. Why use firewalld over iptables or UFW?
  3. firewalld Zones Explained
  4. Permanent vs Runtime Rules
  5. Basic Commands: firewall-cmd Usage
  6. Allowing and Blocking Services and Ports
  7. Using Rich Rules
  8. Masquerading and Port Forwarding
  9. Using Firewall Zones with Interfaces
  10. Reloading, Saving, and Viewing Configurations
  11. firewalld Logging
  12. Conclusion

1. What is firewalld?

firewalld is a dynamic firewall daemon that manages rules using zones and services instead of raw chains and rules like iptables.

  • Uses iptables or nftables in the backend.
  • Dynamic – applies rules without restarting services or flushing the entire firewall.
  • Works with zones, which are collections of rules.

firewall-cmd is the command-line frontend to interact with firewalld.


2. Why Use firewalld Over iptables or UFW?

Featureiptablesufwfirewalld
Dynamic rules❌ Flushes rules❌ Flushes rules✅ No reset required
Zone-based❌ Manual management❌ Basic grouping✅ Zone architecture
CLI ease-of-use⚠️ Complex syntax✅ Simple✅ Moderate + powerful
Service awareness❌ Ports only✅ Supports services✅ Extensive service config

3. firewalld Zones Explained

Zones define levels of trust for network connections.

Common Zones:

ZoneDescription
dropAll incoming connections dropped
blockAll incoming rejected with icmp-host-prohibited
publicFor untrusted networks
externalFor NAT/firewall gateway
dmzIsolated computers
workInternal work environment
homeTrusted home environment
trustedAll network connections are accepted

View active zone:

firewall-cmd --get-active-zones

4. Permanent vs Runtime Rules

  • Runtime rules: Lost after reboot
  • Permanent rules: Persist across reboots

Use –permanent flag for permanent rules.

Example:

firewall-cmd --permanent --add-service=ssh
firewall-cmd --reload

5. Basic Commands: firewall-cmd Usage

Check service status:

sudo firewall-cmd --state

List all zones:

sudo firewall-cmd --get-zones

Get default zone:

sudo firewall-cmd --get-default-zone

Set default zone:

sudo firewall-cmd --set-default-zone=home

6. Allowing and Blocking Services and Ports

Allow SSH service:

sudo firewall-cmd --zone=public --add-service=ssh

Allow HTTP port:

sudo firewall-cmd --zone=public --add-port=80/tcp

Remove service/port:

sudo firewall-cmd --zone=public --remove-service=ssh
sudo firewall-cmd --zone=public --remove-port=80/tcp

Make permanent:

sudo firewall-cmd --zone=public --permanent --add-service=http
sudo firewall-cmd --reload

7. Using Rich Rules

Rich rules provide fine-grained control, such as logging, source IP filtering, or interface matching.

Example: Block an IP

sudo firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=203.0.113.5 reject'

Example: Allow from specific subnet to a port

sudo firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=192.168.1.0/24 port port=22 protocol=tcp accept'

Enable logging:

sudo firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=203.0.113.10 log prefix="FW-LOG " level=info accept'

8. Masquerading and Port Forwarding

Enable NAT Masquerade:

sudo firewall-cmd --zone=external --add-masquerade

Port Forwarding Example:

Forward external port 8080 to internal 80:

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

With internal IP:

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

9. Using Firewall Zones with Interfaces

Assign an interface to a zone:

sudo firewall-cmd --zone=home --change-interface=eth0

Make it permanent:

sudo firewall-cmd --permanent --zone=home --add-interface=eth0

10. Reloading, Saving, and Viewing Configurations

Reload configuration:

sudo firewall-cmd --reload

View zone config:

sudo firewall-cmd --zone=public --list-all

List services and ports:

sudo firewall-cmd --list-services
sudo firewall-cmd --list-ports

11. firewalld Logging

Enable logging via rich rule (as shown above), or configure firewalld’s logging level in the configuration:

Edit /etc/firewalld/firewalld.conf:

LogDenied=all  # other options: off, unicast, broadcast, multicast

Restart the service:

sudo systemctl restart firewalld

Logs will appear in /var/log/messages or /var/log/firewalld


12. Conclusion

firewalld with firewall-cmd provides a modern, flexible, and user-friendly way to manage Linux firewalls. With zone-based architecture and dynamic configuration, it fits well in both server and enterprise environments.

Key Takeaways:

✅ Zone-based security model
✅ Dynamic and persistent rule handling
✅ Support for NAT, port forwarding, and logging
✅ Rich rules offer granular control

Learning firewall-cmd equips you to manage security professionally on RHEL, CentOS, AlmaLinux, Fedora, and beyond.

Scroll to Top