Sub topic 11
Configuring a Default-Deny Host Firewall
This chapter assumes you can read basic TCP/IP concepts (ports, protocols, connection states), use systemd, and have root or sudo access to a test server or VM with console access. It covers the CIS firewall recommendations on a RHEL 9 style host that uses nftables.
Why a default-deny host firewall
A host firewall filters traffic on the server itself. The CIS Benchmarks ask for a default-deny inbound policy: every packet is dropped unless a rule allows it. A network firewall in front of the server may already exist, but the host firewall still protects you if a service is started by mistake, if an attacker is already on the same network segment, or if the perimeter rules change.
CIS also requires that you run exactly one firewall tool. The common choices are firewalld, nftables and iptables. Each one manages the same kernel packet filter, so running two at once produces rules that overwrite or contradict each other. This chapter uses nftables directly.
How nftables is organized
nftables rules live in a hierarchy:
- A table belongs to an address family. The
inetfamily handles IPv4 and IPv6 in one table. - A chain holds an ordered list of rules. A base chain is attached to a kernel hook such as
input,forwardoroutput, and has a default policy. - A rule matches packets and applies a verdict such as
acceptordrop.
The kernel checks rules in order and stops at the first terminal verdict. If no rule matches, the chain policy applies.
A minimal default-deny ruleset
This ruleset follows the CIS pattern. Save it as /etc/nftables.conf.
# /etc/nftables.conf (minimal default-deny inbound policy)
table inet filter {
chain input {
type filter hook input priority 0; policy drop;
iif "lo" accept
iif != "lo" ip saddr 127.0.0.0/8 drop
ct state established,related accept
ct state invalid drop
tcp dport 22 accept
}
chain forward {
type filter hook forward priority 0; policy drop;
}
chain output {
type filter hook output priority 0; policy accept;
}
}
# Syntax-check before loading, and keep a second session open when working remotely
sudo nft -c -f /etc/nftables.confThe lines after the closing brace are shell commands, not part of the file. Do not put them in /etc/nftables.conf.
Here is what each rule does:
iif "lo" acceptallows loopback traffic. Many local services talk to each other over127.0.0.1, and they break without this rule.iif != "lo" ip saddr 127.0.0.0/8 dropdiscards packets that claim a loopback source address but arrive on a real interface. These packets are spoofed.ct state established,related acceptallows replies to connections the server started, and related traffic such as ICMP errors. The kernel's connection tracking (ct) remembers each flow.ct state invalid dropdiscards packets that do not fit any known connection.tcp dport 22 acceptis the only new inbound connection allowed.
The forward chain drops all routed traffic, which is correct for a host that is not a router. The output chain accepts everything. CIS allows this, and you can tighten it later if your threat model needs egress filtering.
The inet family covers IPv6 as well. IPv6 needs ICMPv6 for neighbor discovery. If your host uses IPv6 and loses connectivity after you load the ruleset, add meta l4proto ipv6-icmp accept before the final drop. You can also disable IPv6 if the server does not use it, as discussed in the network chapter.
Loading the ruleset without locking yourself out
A wrong rule on a remote machine can cut your SSH session. Follow this order:
- Keep one SSH session open and, if possible, a console session (hypervisor or out-of-band).
- Run
sudo nft -c -f /etc/nftables.conf. The-cflag checks syntax and loads nothing. - Load the rules in the running kernel with
sudo nft -f /etc/nftables.conf. - Open a new SSH connection from another terminal. If it works, the rules are good.
- Enable persistence with
sudo systemctl enable --now nftables.
An established session keeps working after you load the rules because of the ct state established,related rule. The real test is a new connection, so always test one.
A safety net for risky changes is a scheduled rollback. Before loading, run sudo systemd-run --on-active=5m systemctl stop nftables or use at. If you lose access, the firewall stops after five minutes. Cancel the timer after you confirm the new rules work. Note that stopping the nftables service on RHEL does not by itself remove rules, so for a true rollback use a command that flushes the ruleset, such as nft flush ruleset. Test your rollback method on a VM first.
Verifying the result
Check the active rules and the policy:
sudo nft list ruleset
sudo nft list chain inet filter input
sudo ss -tlnpnft list ruleset shows what the kernel is enforcing, which may differ from the file if someone changed rules at runtime. ss -tlnp lists listening TCP sockets. Compare it with your rules: any listening port without a matching allow rule is unreachable from outside, and any allowed port without a listener is a leftover to remove.
From another machine you can confirm with nmap -p 22,80 server. Port 22 should be open and port 80 should show as filtered, because dropped packets get no reply.
Common errors
- Using two firewall tools. If firewalld is active, its rules and your nftables file both apply. Disable one with
systemctl disable --now firewalldand mask it, or use firewalld only and skip this file. Scanners flag a host with more than one tool. - Default-drop before the SSH rule. Setting
policy dropand then loading rules in a script that fails midway leaves only the policy. Always runnft -cfirst. - Rules that do not survive a reboot.
nft -fchanges only the running kernel. Persistence comes from the service reading/etc/nftables.confat boot. - Forgetting other services. A server that also runs HTTPS needs
tcp dport { 22, 443 } accept. Add only ports that appear in your service inventory. - Restricting SSH sources. For stricter control, replace the SSH rule with
ip saddr 192.0.2.0/24 tcp dport 22 accept, using your own management network.
How OpenSCAP checks this
The CIS profile checks that a single firewall utility is installed and enabled, that the default input and forward policies are drop, that loopback rules exist, and that open ports have rules. Rescan after your changes:
sudo oscap xccdf eval \
--profile xccdf_org.ssgproject.content_profile_cis_server_l1 \
--results results.xml \
--report report.html \
/usr/share/xml/scap/ssg/content/ssg-rhel9-ds.xmlSome checks may still fail if your ruleset differs from the layout the scanner expects, such as different table or chain names. Read the failing rule in the report, compare it with nft list ruleset, and either adjust your file or record an exception, as described in the final chapter.
Exercise
You load a new nftables ruleset with a default-drop input policy over SSH. Your current session keeps working, but a colleague cannot open a new SSH connection. What is the most likely cause, and how do you confirm it?
Show answer
The ruleset has no working rule allowing new inbound connections to port 22 (for example a typo in the port, a restricted source network that excludes the colleague, or a missing accept rule). The existing session survives only because of the ct state established,related accept rule. Confirm with sudo nft list chain inet filter input to inspect the rules, and test a new connection from another terminal. Fix the SSH rule, check it with nft -c -f, and reload.
Exercise
Why does the ruleset drop packets from 127.0.0.0/8 that arrive on a non-loopback interface?
Show answer
Legitimate loopback traffic only ever uses the lo interface. A packet with a loopback source address on a real interface is spoofed, so it is dropped.