7.2 Linux Firewall Configuration: firewalld, Zones, UFW & Packet Filtering

Key Takeaways

  • Netfilter evaluates traffic in the kernel. Check the local or forwarded path, ownership of rules, and connection state.

  • Inspect actual firewalld source/interface bindings, priorities and zone targets. Service names represent definitions.

  • Runtime firewalld rules act now, and permanent rules survive reload. Saving runtime rules as permanent overwrites the stored configuration as a whole.

  • A rich rule’s match, action and limit placement determine the effect. Inspect existing permits and IPv6 before treating a rule as exclusive.

  • Translation and IPv4 forwarding do not guarantee passage. Check routing, filtering, addresses, ports and the return path.

Last updated: November 2026

7.2 Linux Firewall Configuration: firewalld, Zones, UFW & Packet Filtering

Start with the intended traffic, including its source, address family, transport, destination port, interface and whether it terminates locally or traverses the host. Check which tool owns the effective rules before making a change.

Netfilter and packet paths

Linux Netfilter supplies kernel hooks for traffic processing. For a simplified IP path, incoming packets first meet the prerouting hook. Routing directs traffic for this host to input and transit traffic to forward. Routed egress proceeds through postrouting. Traffic generated locally uses output and can then go through postrouting. The input and output hooks are separate paths. Some ingress, bridge and other hooks exist beyond this simplified path.

ToolRole
nftables and nftConfigure packet classification, filtering and translation in the kernel. An inet table can handle both IPv4 and IPv6. Base chains register at a hook with a priority. Native sets allow membership matching. Use nft list ruleset to inspect the effective rules.
iptables and ip6tablesApply IPv4 or IPv6 rules from the iptables interface. The legacy and nft compatibility backends differ. Inspect the installed backend before assuming a rule belongs to a different ruleset. Packet evaluation happens in the kernel.
ipsetManages sets used by compatible tools, such as an iptables set match over a set of source addresses. For a native nftables policy, use nftables sets and compatible types.
firewalld and ufwHigher-level tools that generate firewall rules. Use the frontend that owns the policy. A manual rule outside it can interact with or be lost across a frontend change.

With stateless filtering, a rule can match the current packet's fields, such as protocol and destination port. A stateful policy can also refer to connection tracking. For example, accepting ct state established,related can allow replies for flows already permitted, while rules for new connections remain separately enforced. That state does not establish an application's identity.

Firewalld zones and runtime changes

Inspect the implementation before deciding where a rule belongs:

firewall-cmd --state
firewall-cmd --get-default-zone
firewall-cmd --get-active-zones
firewall-cmd --zone=public --list-all
firewall-cmd --permanent --zone=public --list-all

Zone classification can use source and interface bindings. Unclassified traffic uses the configured default zone. Check both bindings and any explicit zone priorities supported by the installed release. On a NetworkManager-managed interface, the connection profile can own the zone association. Confirm the active classification and the settings that will be used after reconnecting.

Example zoneHow to evaluate it
drop and blockIn the shipped definitions, unmatched incoming connections are silently dropped by drop and rejected by block. Established reply traffic and other earlier rules can be handled separately.
public, work, home, internal, dmzRepresent different intended environments. Inspect actual services, ports, policies, forward settings and zone targets rather than assuming that names imply exact rules.
externalA profile that commonly enables IPv4 masquerading for a router's outward path. Verify the installed configuration and the forwarding policy.
trustedThe shipped zone uses a broadly accepting target. Verify the security effect before binding an interface or source to it.

On a lab host with the intended zone already identified as public, an explicit --add-port=8443/tcp adds that port and protocol. --add-service=https uses the named service's definition, which can include ports and additional properties. Inspect it with firewall-cmd --info-service=https when the exact definition matters. An allowed port does not create a listener.

firewall-cmd --zone=public --add-port=8443/tcp
firewall-cmd --zone=public --query-port=8443/tcp
firewall-cmd --permanent --zone=public --add-port=8443/tcp
firewall-cmd --permanent --zone=public --query-port=8443/tcp

The first command modifies runtime. The third saves a separate persistent setting. A successful ordinary reload makes the permanent configuration the new runtime configuration and discards runtime-only zone rules. Some direct rules have a documented exception when FlushAllOnReload=no. Ordinary reload aims to preserve state information, but the new policy can still affect access. A complete reload is more disruptive. Alternatively --runtime-to-permanent saves the active configuration as a whole, overwriting the permanent configuration. Review both before choosing it.

Rich rules

For an approved lab IPv4 management subnet, this rich rule permits a particular TCP destination port from that source and rate-limits the logging action:

firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="192.0.2.0/24" port port="8443" protocol="tcp" log prefix="LAB8443 " limit value="1/m" accept'

The limit here is on logging. The accept action has no limit of its own. Other rules can still allow the port. Check broad service and port allows and rule placement before treating the source condition as exclusive. Rich rules can also reject or drop, with effective order determined by priority and action chains. Validate on an authorized host and verify IPv6 policy separately if relevant.

Translation and forwarding

MechanismWhat changes
NATAn umbrella term for translating packet addresses. A translated flow generally needs a compatible return path.
SNATChanges the source. IPv4 masquerading is convenient when the outgoing interface address can change.
DNATChanges the destination. A port-forward rule can redirect traffic to a different local port or an intended internal host.
PATTranslates transport ports along with or as part of translation. For example, two private flows sharing an external IPv4 address can be distinguished by translated source ports.

For a gateway, look at sysctl net.ipv4.ip_forward. A value of 1 enables IPv4 forwarding between interfaces. It does not set routes, authorize transit traffic or create NAT. On a lab gateway forwarding from public TCP 443 to an internal HTTPS service, check the destination after DNAT, filter allowance for the transit path, and the return route. A local redirect and a forward to another host exercise different paths.

UFW policy and precedence

On a host using Ubuntu's UFW, start with ufw status verbose and ufw status numbered with appropriate privileges. A simple ufw allow 22/tcp selects a port/protocol, while ufw allow ssh uses a service name. An application profile such as OpenSSH may define ports and protocols. Inspect it with ufw app info OpenSSH if it exists.

Suppose incoming policy is deny, SSH listens on TCP 22, and the existing IPv4 rules do not allow that port. This adds a source-specific incoming allowance:

ufw allow in from 192.0.2.50 to any port 22 proto tcp

If a broad allowance already exists, adding this rule will not remove it. Examine order and existing IPv6 rules before declaring access restricted. Normal rule changes are stored and applied when the firewall is enabled. Use ufw delete with the rule specification, or verify the current number before deleting a numbered rule. Keep authorized management access during policy changes and verify an approved connection on the intended path.

Test Your Knowledge

A running firewalld host accepts a runtime-only zone rule for 8443/tcp. There is no equivalent permanent rule and the ordinary firewall-cmd --reload succeeds. What happens to that explicit port allowance?

A

The runtime rule is saved automatically

B

The explicit rule survives even without a permanent definition

C

The explicit runtime-only rule is removed

D

The reload fails because a rule was unsaved

Test Your Knowledge

Which shipped firewalld zone is designed to silently discard unmatched new incoming connections, assuming its definition was left at the default?

A

drop

B

block

C

public

D

external

Test Your Knowledge

An active Ubuntu UFW policy denies incoming traffic by default. No existing allowance or earlier deny applies to SSH on TCP 22. Which rule permits IPv4 SSH only from 192.0.2.50?

A

ufw allow 22/tcp

B

ufw allow out to 192.0.2.50 port 22 proto tcp

C

ufw allow in from 192.0.2.50 to any port 22 proto udp

D

ufw allow in from 192.0.2.50 to any port 22 proto tcp

Sections you finish are checked off in the contents.