Linux › Section I · Your network and its configuration › Topic I.12
Firewalls
Every Linux kernel contains a packet filter that can accept, drop or reject traffic at several points on its way through the machine. This topic shows how that filter is organised, how to write rules for it with nftables and the older iptables, and how the friendlier ufw and firewalld front-ends fit on top.
Filtering inside the kernel
A firewall lets through only the traffic a policy allows, as the Networking topic Operational security: firewalls and intrusion detection explains for networks in general. On Linux the filtering is done by netfilter, a framework inside the kernel. A packet meets netfilter at fixed hooks: prerouting as it arrives, input if it is for this host, forward if it is being routed onward, output if a local process created it, and postrouting just before it leaves. Rules attached to a hook can match addresses, protocols, ports, interfaces and connection state, and then accept the packet, drop it silently or reject it with an error.
Tables, chains and rules
Rules are grouped into chains, and chains into tables. A packet entering a chain is compared with each rule from the top down, and the first rule that matches decides its fate; if none matches, the chain's policy, accept or drop, applies. In the classic layout, the filter table has three chains: INPUT for packets addressed to this host, FORWARD for routed packets and OUTPUT for locally generated ones. A packet arriving for a local program passes INPUT only; a packet being routed passes FORWARD only; a reply your program sends passes OUTPUT. Connection tracking lets a rule match a packet's state, such as new or established.
iptables, the classic tool
iptables manages the classic tables. iptables -L -n -v --line-numbers lists the filter table with numeric addresses, counters and rule numbers. -A INPUT appends a rule, -I INPUT 1 inserts one at the top, -D INPUT 3 deletes rule 3, and -P INPUT DROP sets the policy. A rule such as iptables -A INPUT -p tcp --dport 22 -j ACCEPT accepts SSH, and -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT accepts replies to existing connections. Rules live only in kernel memory; iptables-save prints them so they can be restored at boot. ip6tables is the separate IPv6 version. On many systems iptables -V reports nf_tables, meaning the command now writes nftables rules underneath.
Worked example: when order goes wrong
A web server's INPUT chain reads: 1, accept TCP port 80 from anywhere; 2, drop everything from 203.0.113.0/24; 3, accept TCP port 22. The policy is drop. An attacker at 203.0.113.66 is flooding the web server. 1. A packet from 203.0.113.66 to port 80 matches rule 1 first, so it is accepted; rule 2 is never reached. 2. A packet from the same host to port 22 skips rule 1, matches rule 2 and is dropped. 3. To block the attacker everywhere, the drop must come first: delete it with iptables -D INPUT 2, then insert it at the top with iptables -I INPUT 1 -s 203.0.113.0/24 -j DROP. Check: after the change, rule 1 matches the attacker's port-80 packets before the accept.
nftables, the current framework
nftables replaces iptables, ip6tables and their relatives with one tool, nft, and one syntax. You create your own tables and chains, choosing each chain's type, hook, priority and policy; a table of family inet covers IPv4 and IPv6 together. nft list ruleset shows everything loaded, nft -c -f FILE checks a rules file without applying it, and nft -f FILE loads it. Distributions run the rules at boot through an nftables service; Debian, for example, reads /etc/nftables.conf. Older iptables rules can be converted with iptables-translate. Whichever tool you use, avoid mixing hand-written rules from both on one machine.
Worked example: a default-deny host firewall
Write an nftables file for a server that should accept only SSH from outside. Inside table inet filter, a chain input holds: type filter hook input priority 0; policy drop; then ct state established,related accept; then iif lo accept; then meta l4proto { icmp, ipv6-icmp } accept; then tcp dport 22 accept. Now test four packets. 1. A new SSH connection from 198.51.100.7: no earlier rule matches, tcp dport 22 does, so accept. 2. A reply to the server's own update download: established, accept. 3. A new connection to port 3306: nothing matches, so the drop policy applies. 4. A ping: ICMP, accept. Check: anything not explicitly allowed falls to the policy.
Strategy: allow what you trust
Two lessons from that example matter more than any syntax. First, start from a default policy of drop and allow only what you need; adding drop rules for each new attacker never ends. Second, a host also needs its own replies, its loopback traffic and essential ICMP; blocking all ICMP breaks path MTU discovery and IPv6 neighbour discovery. For a single machine, put rules on the input hook. For a router protecting a network, put them on the forward hook, typically allowing established traffic both ways and new connections only from the inside, which completes the NAT router from Linux as a router: private networks and NAT.
ufw on Ubuntu
Writing rules by hand is error-prone, so distributions provide front-ends. Ubuntu's ufw, the Uncomplicated Firewall, is installed but disabled by default, with policies to deny incoming and allow outgoing traffic once enabled. ufw allow 22/tcp, or ufw allow ssh, permits SSH, and ufw limit ssh/tcp also slows repeated connection attempts. ufw enable switches the firewall on now and at every boot; ufw status verbose shows the policies and rules, ufw status numbered numbers them, and ufw delete 3 removes rule 3. ufw default deny incoming sets a policy. ufw enable warns that it may disrupt existing SSH connections and asks for confirmation, a reminder to allow SSH first.
firewalld on Fedora and RHEL
Fedora and RHEL use firewalld, a daemon that sorts interfaces into zones such as public or home, each with its own allowed services and ports; on current releases it programs nftables. firewall-cmd --state checks it is running, --get-active-zones shows which zone each interface is in, and --list-all lists the current zone's settings. firewall-cmd --add-service=ssh or --add-port=8080/tcp opens a service or port in the running configuration only. Add --permanent to save the change, which then takes effect after firewall-cmd --reload, so administrators often run a command both ways. --runtime-to-permanent saves the whole running configuration.
Common mistakes to avoid
First, setting a drop policy, or flushing rules under a drop policy, on a remote machine before allowing SSH: you lock yourself out, so keep console access and allow SSH first. Second, putting a broad accept above a specific drop; the first match wins. Third, forgetting ct state established,related, so replies to your own connections are dropped. Fourth, blocking all ICMP. Fifth, filtering IPv4 only and leaving IPv6 open; use an inet table or ip6tables too. Sixth, making firewalld changes with --permanent and wondering why nothing changed until --reload, or without it and losing them at reload.
Key takeaways
netfilter filters packets in the kernel at five hooks: prerouting, input, forward, output and postrouting. Rules sit in chains within tables, are checked top-down with the first match deciding, and a chain policy covers the rest. iptables uses fixed INPUT, FORWARD and OUTPUT chains with -A, -I, -D, -P and -L; nftables, through nft, lets you build inet tables covering both IP versions and is the current framework. Use default deny, allow established traffic, loopback and essential ICMP, then specific services. ufw on Ubuntu and firewalld, with zones and firewall-cmd, on Fedora and RHEL are front-ends.
Flashcards
Flashcard
The packet-filtering framework inside the Linux kernel
Test your understanding
Question 1 of 5
An INPUT chain has rule 1: accept TCP port 80 from anywhere; rule 2: drop all from 203.0.113.0/24. What happens to a packet from 203.0.113.66 to port 80?
Question 2 of 5
A Linux router forwards traffic between its LAN and the internet. Which chain or hook filters that routed traffic?
Question 3 of 5
A server uses an nftables input chain with policy drop and a rule tcp dport 22 accept, but programs on it can no longer download updates. Which rule is most likely missing?
Question 4 of 5
On a Fedora server, an administrator runs firewall-cmd --permanent --add-port=8080/tcp, but the port is still closed. Why?
Question 5 of 5
You are connected to a remote server only by SSH. Which order of ufw commands is safe?
