Chapter 18. Packet Filtering and Firewalls
Every firewall task in this chapter reduces to one model: the kernel
offers a handful of well-defined points where packets pass, rules
attached to those points accept, drop, or rewrite packets, and the
first matching rule decides. Once that model is in place, protecting a
single host, rolling rules out to a remote machine without losing
access, filtering traffic that passes through a router, and finding the
rule that ate a connection are all variations of the same few
inspections. On Debian 13 the model is implemented by netfilter in the
kernel and driven by nftables: one command, nft, one configuration
file, /etc/nftables.conf, and one systemd unit to load it at boot.
The chapter builds three rulesets of increasing scope. A host firewall
on fwclient locks a machine down to SSH from two trusted networks and
one web service. A rollout exercise carries a candidate ruleset to a
remote router with a timed automatic rollback, so a mistake costs a
minute rather than a console visit. A routed firewall on fwrouter
combines a forward policy with IPv4 masquerading and destination NAT,
then forwards IPv6 without translation at all. Troubleshooting closes
the loop: counters, rate-limited logs, per-packet traces, and the
connection-tracking table turn "it does not work" into a named rule or
a named route.
The walkthrough uses four Debian 13 machines on three isolated practice networks, with all administrative SSH on a fourth:
| Host | Role | Practice interfaces |
|---|---|---|
fwclient |
host being locked down; runs small HTTP responders on ports 8080, 8081 and 8082 | eth0 - administration, 192.168.121.0/24; eth1 - LAN, 192.168.218.10/24 and 2001:db8:18:1::10/64 |
fwrouter |
router and firewall between the three practice networks | eth0 - administration; eth1 - LAN, 192.168.218.2/24 and 2001:db8:18:1::2/64; eth2 - WAN, 192.168.219.2/24 and 2001:db8:18:2::2/64; eth3 - DMZ, 192.168.220.2/24 and 2001:db8:18:3::2/64 |
fwserver |
web server in the DMZ | eth0 - administration; eth1 - DMZ, 192.168.220.10/24 and 2001:db8:18:3::10/64 |
fwremote |
untrusted WAN peer, also running a web server | eth0 - administration; eth1 - WAN, 192.168.219.20/24 and 2001:db8:18:2::20/64 |
The LAN, WAN and DMZ are isolated from each other except through
fwrouter, so the only way practice traffic crosses between them is a
rule this chapter writes on purpose. Addresses come from RFC 1918
private space and the IPv6 documentation prefix 2001:db8::/32, so
nothing here can disturb a real network. Each host's eth0 sits on the
administration network 192.168.121.0/24, which no exercised ruleset
ever filters or disables: administrative SSH arrives there, exactly the
out-of-band arrangement a production site builds with a separate
management VLAN or a lights-out interface. Every host has the john
account with sudo access, established during installation
(Chapter 2) and governed as
Chapter 5 describes.
Each practice host reaches the prefixes on the far side of the router
through static routes - fwclient sends 192.168.219.0/24 and
192.168.220.0/24 via 192.168.218.2, fwserver sends the LAN and
WAN prefixes via 192.168.220.2, and fwremote sends the LAN and DMZ
prefixes via 192.168.219.2, with equivalent IPv6 routes via each
network's router address. Add them with ip route replace as
Chapter 17 shows, and make them
persistent the way Section 17.3.3 does; the exercises below assume they
exist.
Commands entered in john's ordinary shell begin with $ and commands
entered in a root shell begin with #; the prompt marker is not
typed. Where a single elevated command is more natural than a root
shell, it is written $ sudo command, which asks for john's password
the first time in a session (the password is not echoed). Sustained
ruleset work is easier in a root shell, entered with $ sudo -i and
left with exit; each section says which host and shell it assumes.
Except where a block names another host, the commands run on fwclient,
and multi-host sequences reidentify the target before each block.
Editing files is done with the terminal editor from
Chapter 3; systemd units, transient units and
the journal come from Chapter 8; timers,
including the transient timers used for the rollback, from
Chapter 10; interface and route inspection
from Chapter 17.
Create the workspace before the first example. Ruleset files, the
practice pages served during the exercises, and everything else john
creates stay under it; system configuration goes to /etc:
$ mkdir -p ~/debian-handbook/firewall
The command prints nothing when it succeeds. Make it the working
directory with cd ~/debian-handbook/firewall; relative paths below
are written from inside it, and the same command creates the workspace
on each of the four hosts. One caution applies whenever a root shell is
open: root's home is not john's home, so the examples keep john's
workspace spelled out as /home/john/debian-handbook/firewall whenever
root reads or writes files there.
The exercises need known targets to connect to. On fwclient, as john,
create one directory and one one-line page per responder port - the
pages identify which responder answered:
for port in 8080 8081 8082; do
mkdir -p ~/debian-handbook/firewall/site-$port
printf 'hello from client %s\n' "$port" > ~/debian-handbook/firewall/site-$port/index.html
done
The loop creates three directories and three files; the redirections write files rather than terminal output, so nothing prints. Read one back to check the content:
$ cat ~/debian-handbook/firewall/site-8080/index.html
hello from client 8080
The responders themselves run as small systemd services so they survive
the reboot test in Section 18.3.1. In a root shell on fwclient,
create /etc/systemd/system/http-8080.service with this content:
[Unit]
Description=Demo HTTP responder on port 8080
After=network.target
[Service]
User=john
Group=john
WorkingDirectory=/home/john/debian-handbook/firewall/site-8080
ExecStart=/usr/bin/python3 -m http.server 8080 --bind 0.0.0.0
Restart=on-failure
[Install]
WantedBy=multi-user.target
http-8081.service and http-8082.service are identical apart from
the port number in the description, the WorkingDirectory line and the
ExecStart port. Create all three, then reload systemd and enable the
units; daemon-reload prints nothing:
# systemctl daemon-reload
# systemctl enable --now http-8080.service http-8081.service http-8082.service
Created symlink '/etc/systemd/system/multi-user.target.wants/http-8080.service' → '/etc/systemd/system/http-8080.service'.
Created symlink '/etc/systemd/system/multi-user.target.wants/http-8081.service' → '/etc/systemd/system/http-8081.service'.
Created symlink '/etc/systemd/system/multi-user.target.wants/http-8082.service' → '/etc/systemd/system/http-8082.service'.
Each unit's configuration now has a boot-time symlink, and --now
started the listeners. Confirm all three:
# systemctl is-active http-8080.service http-8081.service http-8082.service
active
active
active
On fwserver and fwremote, nginx serves the Debian default site,
installed as Chapter 23 shows; its listener
answers on both IPv4 and IPv6, which Section 18.4.3 uses. Give each a
one-line page so responses are identifiable. On fwserver, in a root
shell, replace the placeholder page - the redirection writes the file
and prints nothing:
# printf 'hello from server 80\n' > /var/www/html/index.html
On fwremote, in a root shell, the same with its own page text:
# printf 'hello from remote 80\n' > /var/www/html/index.html
The first curl to each server in Section 18.2.4 shows the resulting
pages. Leave the root shell with exit - the next section starts from
john's ordinary shell.