هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
Know it before you install
Every network interface (and every source network) belongs to one zone. A zone lists what is allowed in. A service is a named set of ports (ssh = 22/tcp, https = 443/tcp). firewalld writes the real rules into nftables.
| Item | Value |
|---|---|
| Package | firewalld |
| Service | firewalld.service |
| Command | firewall-cmd (and firewall-config GUI, firewall-offline-cmd) |
| Main config | /etc/firewalld/firewalld.conf |
| Your zones and services | /etc/firewalld/zones/*.xml, /etc/firewalld/services/*.xml |
| Built-in defaults | /usr/lib/firewalld/ (do not edit) |
| Backend | nftables |
تذكّر
Runtime rules work at once but are lost at reload or reboot. Permanent rules (--permanent) are saved but work only after --reload. The normal way: add with --permanent, then --reload.
Install on RHEL, Rocky, Alma and Fedora
It is usually installed and running already:
dnf -y install firewalld
systemctl enable --now firewalld
firewall-cmd --state
runningInstall on Ubuntu and Debian
Ubuntu uses UFW by default. Use only one firewall tool — turn UFW off first:
ufw disable
apt-get update
apt-get install -y firewalld
systemctl enable --now firewalld
firewall-cmd --add-service=ssh --permanent && firewall-cmd --reloadFirst configuration: know your zones
firewall-cmd --get-default-zone
public
firewall-cmd --get-active-zones
public
interfaces: eth0
firewall-cmd --list-all
public (active)
target: default
interfaces: eth0
services: cockpit dhcpv6-client ssh
ports:
sources:
rich rules:The first settings on a new server:
- Remove services you do not need (for example
cockpit,dhcpv6-clienton a server with a fixed IP). - Open only the services this server offers.
- Put admin access (SSH) on a zone that only your admin network can reach.
- Turn on logging of denied packets while you build the rules.
firewall-cmd --permanent --remove-service=cockpit
firewall-cmd --permanent --remove-service=dhcpv6-client
firewall-cmd --permanent --add-service={http,https}
firewall-cmd --reload
firewall-cmd --list-services
http https sshLimit access by source network
Allow SSH only from the admin network: put that network in the internal zone with SSH, and remove SSH from public:
firewall-cmd --permanent --zone=internal --add-source=10.0.0.0/24
firewall-cmd --permanent --zone=internal --add-service=ssh
firewall-cmd --permanent --zone=public --remove-service=ssh
firewall-cmd --reload
firewall-cmd --get-active-zones
internal
sources: 10.0.0.0/24
public
interfaces: eth0انتبه
Do this from a session inside 10.0.0.0/24, or test first with a timed runtime rule (below). Otherwise you lock yourself out.
Ports, rich rules and forwarding
A port that has no service name, and a custom service for your app:
firewall-cmd --permanent --add-port=8443/tcp
firewall-cmd --permanent --new-service=shopapi
firewall-cmd --permanent --service=shopapi --set-description="Shop API"
firewall-cmd --permanent --service=shopapi --add-port=9000/tcp
firewall-cmd --permanent --add-service=shopapiRich rules for "this IP to this port", rate limits and blocks:
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.1.0/24" port port="5432" protocol="tcp" accept'
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.50" drop'
firewall-cmd --permanent --add-rich-rule='rule service name="ssh" accept limit value="10/m"'
firewall-cmd --reload
firewall-cmd --list-rich-rulesPort forwarding and NAT (for a gateway or a container host):
firewall-cmd --permanent --add-forward-port=port=80:proto=tcp:toport=8080
firewall-cmd --permanent --add-masqueradeTest safely and log
A runtime rule with a timeout removes itself — perfect to test without risk:
firewall-cmd --add-service=http --timeout=5m
firewall-cmd --set-log-denied=unicast
journalctl -k -f | grep -i 'filter_'
firewall-cmd --runtime-to-permanent--runtime-to-permanent saves the runtime rules you tested. Set --set-log-denied=off again when you are done (logs can get big).
Daily use: the commands and the real uses
firewall-cmd --list-all
firewall-cmd --list-all-zones | less
firewall-cmd --info-service=https
firewall-cmd --get-services | tr ' ' '\n' | grep -i sql
firewall-cmd --zone=public --query-service=http
firewall-cmd --permanent --zone=internal --change-interface=eth1
nmcli connection modify eth1 connection.zone internal
firewall-cmd --panic-on
firewall-cmd --panic-off--panic-on drops all traffic at once (an emergency stop — also your SSH session).
Real uses at work:
- Every RHEL server: only the needed services open; admin ports only from the admin network.
- Database servers: rich rules for the app subnet only.
- RHCSA exam tasks: open services and ports permanently, with SELinux ports.
Make it safe
Security checklist:
- Default zone
public(ordropfor very strict servers); only needed services. - SSH and admin ports only from admin networks (sources or rich rules).
- Always
--permanent+--reload; check with--list-allafter every change. - Use one firewall tool only (not firewalld + UFW + manual nftables).
- Docker writes its own rules and publishes ports past firewalld zones — bind container ports to
127.0.0.1when a proxy is in front. - Keep the config in Git (the XML files in
/etc/firewalld/) or in Ansible (ansible.posix.firewalld).
Update and remove
Fix common problems
| Problem | Cause | Fix |
|---|---|---|
| Rule is gone after reload or reboot | It was a runtime rule only. | Add with --permanent (or --runtime-to-permanent), then --reload. |
--permanent rule does not work | No reload yet. | firewall-cmd --reload. |
| Service works on the server but not from other hosts | The port is not open in the zone of the interface. | firewall-cmd --get-active-zones; add the service to that zone. |
| Rule in a zone has no effect | The interface or source is in another zone. | --get-active-zones; move the interface (--change-interface or nmcli … connection.zone). |
Error: INVALID_SERVICE: 'xyz' | No service with that name. | firewall-cmd --get-services; use --add-port or create the service. |
| Locked out of SSH | SSH removed from the zone of your source. | Use the console; test changes with --timeout first next time. |
| Container ports open although the zone does not allow them | Docker's own rules. | Bind with -p 127.0.0.1:…; use the docker zone rules carefully. |
Next steps
- Quick how-tos: Open a port in the firewall, Allow a port only from one IP, Test if a remote port is open, Write rules with nftables.
- Command page: firewall-cmd, Command page: nft, Command page: nmcli.
- Learn it for RHCSA: RHCSA — Firewall, RH134 — Network security.
- Practise: Lab: configure firewalld zones and services.
- The Ubuntu tool: Install and set up UFW.
1You added a rule with --permanent and it does not work yet. Why?
Permanent rules work only after firewall-cmd --reload.
2How do you test a rule so it removes itself if something goes wrong?
Add it as a runtime rule with --timeout=5m.