firewall-cmd (firewalld) (2/2)
Inside a firewalld zone you refer to reusable objects by name: ipsets, services, helpers and ICMP types. This part covers those objects, the deprecated direct interface, lockdown with its whitelist, panic mode and firewall-cmd exit codes.
108.1 Why is it used?
- An ipset gives one name to thousands of IPs; bind it to a zone and one binding checks the whole list.
- Services bundle ports/protocols under a name (ssh, myapp) so zones stay readable.
- Helpers let protocols like FTP open their extra data ports safely.
- Lockdown stops random apps changing the firewall; panic mode drops everything in an emergency.
- Objects are XML files: built-in in /usr/lib/firewalld, your own in /etc/firewalld. Create/edit needs --permanent; at runtime mostly only --info-* works.
108.2 How it works: ipset + drop zone
- Step 1
1.ipset blocklist holds bad IPs like 192.168.128.66
- Step 2
2.drop zone source = ipset:blocklist
- Step 3
3.Attacker packet: source matches the ipset -> drop zone -> silently dropped
- Step 4
4.Client 192.168.128.20: not in the ipset -> normal zone rules
- Step 5
5.Client packet reaches server 192.168.128.10
Lab: firewalld needs nft_ct / nft_reject modules not loaded in the Docker VM, so the daemon exited (Operation not permitted, exit 252 for --state). The lab uses F=firewall-offline-cmd; on a real server write firewall-cmd --permanent instead.
$ firewalld --nofork --nopid --debug=1ERROR: 'python-nftables' failed: netlink: Error: cache initialization failed: Operation not permittedERROR: Failed to load full stock configuration. ... All hope is lost. Exiting.$ firewall-cmd --state; echo $?not running252$ firewall-offline-cmd --version1.3.3108.3 IPSets
F=firewall-offline-cmd# on a server: firewall-cmd --permanent$F --new-ipset=blocklist --type=hash:ip$F --ipset=blocklist \ --add-entry=192.168.128.66$F --ipset=blocklist \ --add-entries-from-file=/tmp/bad-ips.txt$F --ipset=blocklist --get-entries$F --ipset=blocklist \ --query-entry=192.168.128.20; echo $?$F --zone=drop --add-source=ipset:blocklist$F --info-ipset=blocklist$ cat /tmp/bad-ips.txt192.168.128.67# office scan192.168.128.68$ $F --ipset=blocklist --get-entries192.168.128.66192.168.128.67192.168.128.68$ $F --ipset=blocklist --query-entry=192.168.128.20no1$ $F --zone=drop --add-source=ipset:blocklistsuccess$ $F --info-ipset=blocklistblocklist type: hash:ip options: entries: 192.168.128.66 192.168.128.67 192.168.128.68- --add-entries-from-file skips # comment lines.
- --family=inet6 makes an IPv6 ipset; --option sets timeout or maxelem; --get-ipset-types lists types.
- An ipset with a timeout cannot take permanent entries: firewalld does not track them.
$ $F --new-ipset=tmpblock --type=hash:ip \ --option=timeout=600success$ $F --ipset=tmpblock --add-entry=192.168.128.70Error: IPSET_WITH_TIMEOUT(exit 32)108.4 Services
F=firewall-offline-cmd$F --info-service=ssh$F --new-service=myapp$F --service=myapp --add-port=9000/tcp$F --service=myapp --add-protocol=gre$F --service=myapp \ --add-source-port=9000/udp$F --service=myapp \ --set-destination=ipv4:192.168.128.10/32$F --service=myapp --add-include=https$F --info-service=myapp$F --zone=public --add-service=myapp$ $F --info-service=sshssh ports: 22/tcp protocols: source-ports: modules: destination: includes: helpers:$ $F --info-service=myappmyapp ports: 9000/tcp protocols: gre source-ports: 9000/udp modules: destination: ipv4:192.168.128.10/32 includes: https helpers:$ $F --zone=public --add-service=myappsuccess$ $F --zone=public --list-servicesdhcpv6-client myapp ssh- All 34 service options follow one pattern: --add|remove|query- for port, protocol, source-port, helper, include; --set|remove|query-destination; --get-* to list.
- set-destination limits the service to one destination IP; add-include pulls another service in.
108.5 Helpers
- Step 1
1.FTP client 192.168.128.20 opens control connection to port 21
- Step 2
2.Firewall allows it (ftp service)
- Step 3
3.Server announces a new data port that no rule mentions
- Step 4
4.Conntrack helper nf_conntrack_ftp reads control traffic, marks that port RELATED
- Step 5
5.Data connection to FTP server 192.168.128.30 passes
F=firewall-offline-cmd$F --get-helpers$F --info-helper=ftp$F --new-helper=myftp \ --module=nf_conntrack_ftp$F --helper=myftp --add-port=2121/tcp$F --helper=myftp --set-family=ipv4$F --info-helper=myftp$ $F --get-helpersQ.931 RAS amanda ftp h323 irc netbios-ns pptpproto-gre sane sip snmp tftp$ $F --info-helper=ftpftp family: module: nf_conntrack_ftp ports: 21/tcp$ $F --info-helper=myftpmyftp family: ipv4 module: nf_conntrack_ftp ports: 2121/tcp108.6 ICMP types
F=firewall-offline-cmd$F --get-icmptypes | wc -w$F --info-icmptype=echo-request$F --new-icmptype=myicmp$F --icmptype=myicmp --add-destination=ipv4$F --icmptype=myicmp \ --remove-destination=ipv6$F --icmptype=myicmp \ --query-destination=ipv6; echo $?$F --info-icmptype=myicmp$ $F --get-icmptypes | wc -w45$ $F --info-icmptype=echo-requestecho-request destination: ipv4 ipv6$ $F --new-icmptype=myicmpsuccess$ $F --icmptype=myicmp --add-destination=ipv4Warning: ALREADY_ENABLED: ipv4success$ $F --icmptype=myicmp --remove-destination=ipv6success$ $F --icmptype=myicmp --query-destination=ipv6no1$ $F --info-icmptype=myicmpmyicmp destination: ipv4108.7 Direct interface (deprecated)
- Puts raw iptables rules into firewalld; deprecated: use policies in new setups.
- Arguments: ipv (ipv4 | ipv6 | eb), table, chain; in add-rule priority 0 is the top.
- --passthrough is untracked (forgotten); --add-passthrough is tracked and comes back after reload.
F=firewall-offline-cmd$F --direct --add-chain ipv4 filter mychain$F --direct --add-rule ipv4 filter INPUT \ 0 -p tcp --dport 9000 -j ACCEPT$F --direct --get-all-rules$F --direct --add-passthrough ipv4 \ -A INPUT -s 192.168.128.66 -j DROPcat /etc/firewalld/direct.xml$ $F --direct --get-all-rulesipv4 filter INPUT 0 -p tcp --dport 9000 -j ACCEPT$ cat /etc/firewalld/direct.xml<direct> <chain ipv="ipv4" table="filter" chain="mychain"/> <rule ipv="ipv4" table="filter" chain="INPUT" priority="0">-p tcp --dport 9000 -j ACCEPT</rule> <passthrough ipv="ipv4">-A INPUT -s 192.168.128.66 -j DROP</passthrough></direct>108.8 Lockdown and whitelist
F=firewall-offline-cmd$F --query-lockdown; echo $?$F --list-lockdown-whitelist-commands$F --list-lockdown-whitelist-uids$F --add-lockdown-whitelist-user=admin$F --query-lockdown-whitelist-user=admin$F --lockdown-on# on a server: firewall-cmd --lockdown-on# firewall-cmd --lockdown-off$ $F --query-lockdown; echo $?no1$ $F --list-lockdown-whitelist-commands/usr/bin/python3 /usr/bin/firewall-config$ $F --list-lockdown-whitelist-uids0$ $F --add-lockdown-whitelist-user=adminsuccess$ $F --query-lockdown-whitelist-user=adminyes$ $F --lockdown-on'Firewall' object has no attribute 'enable_lockdown'(exit 2)- Lockdown (off by default) lets only whitelisted apps change the firewall over D-Bus.
- The default whitelist command is firewall-config, not firewall-cmd: whitelist firewall-cmd first or you lock yourself out.
- Check order: SELinux context, uid, user, command ('cmd*' = prefix match). --lockdown-on failed in the offline tool due to a bug; run it with firewall-cmd.
108.9 Panic mode and exit codes
- Step 1
1.Server under attack: firewall-cmd --panic-on
- Step 2
2.Incoming packet from the Internet -> dropped
- Step 3
3.Server's own DNS request to 1.1.1.1 -> dropped
- Step 4
4.Admin SSH session 192.168.128.20 -> dropped too
- Step 5
5.Recover from the console: --panic-off
Runtime only; use it only when you have console access
firewall-cmd --panic-on; echo $?firewall-cmd --query-panic; echo $?firewall-cmd --bogus-option; echo $?$ firewall-cmd --panic-on; echo $?FirewallD is not running252$ firewall-cmd --query-panic; echo $?FirewallD is not running252$ firewall-cmd --bogus-option; echo $?usage: 'firewall-cmd --help' for usage informationfirewall-cmd: error: unrecognized arguments: --bogus-option2| Exit code | Meaning |
|---|---|
| 0 | Success / query: yes |
| 1 | Query: no |
| 2 | Bad usage |
| 11 / 12 | ALREADY_ENABLED / NOT_ENABLED |
| 13 / 16 | COMMAND_FAILED / ZONE_ALREADY_SET |
| 251 / 252 / 254 | RUNNING_BUT_FAILED / NOT_RUNNING / UNKNOWN_ERROR |
108.10 Comparison: firewalld objects
| Object | Holds | Used as | Runtime options |
|---|---|---|---|
| ipset | IPs / nets (hash:ip, hash:net) | --add-source=ipset:NAME | --info-ipset, entries |
| service | Ports, protocols, source ports, helpers, includes | --add-service=NAME | --info-service |
| helper | Conntrack module + ports | Inside a service | --info-helper, --get-helpers |
| icmptype | Name + families (ipv4/ipv6) | --add-icmp-block=NAME | --info-icmptype |
108.11 Common mistakes
- New ipset created with --permanent but not visible at runtime: you forgot firewall-cmd --reload.
- Thinking panic mode only blocks incoming: it drops in and out, so it locks out your SSH.
- Turning on lockdown without whitelisting firewall-cmd.
- Building new setups on the direct interface instead of policies.
108.12 Interview tips
- Block thousands of IPs: ipset hash:ip/hash:net + --add-entries-from-file + drop zone source ipset:blocklist + reload; one binding, rules unchanged.
- Objects are XML files: /usr/lib/firewalld (built-in) and /etc/firewalld (yours).
- Helpers (nf_conntrack_ftp) make FTP data ports RELATED.
- Exit 252 = firewalld not running; query options return 0/1.