Sub topic 1
How firewalld and firewall-cmd fit together
Prerequisites
This chapter assumes you can run commands with sudo on a Linux server, know what a TCP or UDP port is, manage services with systemctl, and read IPv4 addresses in CIDR notation, such as 203.0.113.0/24.
How firewalld and firewall-cmd fit together
firewalld is a firewall service that runs in the background. It decides which incoming traffic is accepted, rejected, or dropped. firewall-cmd is the command-line client that reads and changes firewalld's settings. You do not edit firewalld's rules directly. You send requests to the running service through firewall-cmd.
Start the service and confirm it runs:
sudo systemctl enable --now firewalld
sudo firewall-cmd --stateThe second command prints running when firewalld is active.
Zones: how traffic is matched to a rule set
A zone is a named set of rules. Each network interface, such as eth0, belongs to one zone, and traffic arriving on that interface follows that zone's rules. A source, such as a specific network, can also be assigned to a zone. Traffic that matches no rule falls through to the zone's default behavior.
Checking what is currently allowed
Use these commands to see the state of the firewall before you change it:
sudo firewall-cmd --get-default-zone
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --list-all--get-active-zones lists each zone in use and the interfaces assigned to it. --list-all shows the services, ports, and rich rules for the default zone.
Runtime and permanent configuration
Runtime configuration applies immediately, but it is lost when firewalld reloads or restarts. Permanent configuration is saved to disk and loads at boot. A change made with --permanent does not take effect until you run --reload.
A safe pattern is to test a change at runtime first, then save it:
sudo firewall-cmd --add-port=8080/tcp --timeout=5m
sudo firewall-cmd --list-ports
sudo firewall-cmd --runtime-to-permanentThe --timeout=5m option removes the rule after five minutes if you do not save it.
Exercise
Exercise
You added a port at runtime and confirmed that it works. Which command saves it to the permanent configuration without retyping the rule?
Show answer
sudo firewall-cmd --runtime-to-permanent