SkillPilotAI

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

IPSet as the source of the drop zone
  1. 1.ipset blocklist holds bad IPs like 192.168.128.66

  2. 2.drop zone source = ipset:blocklist

  3. 3.Attacker packet: source matches the ipset -> drop zone -> silently dropped

  4. 4.Client 192.168.128.20: not in the ipset -> normal zone rules

  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.

Output
$ 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.3

108.3 IPSets

Terminal
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
Output
$ 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.
Output
$ $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

Terminal
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
Output
$ $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

FTP data port through a helper
  1. 1.FTP client 192.168.128.20 opens control connection to port 21

  2. 2.Firewall allows it (ftp service)

  3. 3.Server announces a new data port that no rule mentions

  4. 4.Conntrack helper nf_conntrack_ftp reads control traffic, marks that port RELATED

  5. 5.Data connection to FTP server 192.168.128.30 passes

Terminal
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
Output
$ $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/tcp

108.6 ICMP types

Terminal
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
Output
$ $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: ipv4

108.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.
Terminal
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
Output
$ $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

Terminal
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
Output
$ $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

Panic mode: drop everything
  1. 1.Server under attack: firewall-cmd --panic-on

  2. 2.Incoming packet from the Internet -> dropped

  3. 3.Server's own DNS request to 1.1.1.1 -> dropped

  4. 4.Admin SSH session 192.168.128.20 -> dropped too

  5. 5.Recover from the console: --panic-off

Runtime only; use it only when you have console access

Terminal
firewall-cmd --panic-on; echo $?firewall-cmd --query-panic; echo $?firewall-cmd --bogus-option; echo $?
Output
$ 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 codeMeaning
0Success / query: yes
1Query: no
2Bad usage
11 / 12ALREADY_ENABLED / NOT_ENABLED
13 / 16COMMAND_FAILED / ZONE_ALREADY_SET
251 / 252 / 254RUNNING_BUT_FAILED / NOT_RUNNING / UNKNOWN_ERROR

108.10 Comparison: firewalld objects

ObjectHoldsUsed asRuntime options
ipsetIPs / nets (hash:ip, hash:net)--add-source=ipset:NAME--info-ipset, entries
servicePorts, protocols, source ports, helpers, includes--add-service=NAME--info-service
helperConntrack module + portsInside a service--info-helper, --get-helpers
icmptypeName + 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.