Sub topic 11
Configuring a Default-Deny Host Firewall
Assume you can use sudo, edit configuration files, and identify the ports required by a service. This chapter uses nftables, the Linux packet-filtering framework. A host firewall controls traffic entering and leaving one server.
A default-deny policy drops traffic unless a rule allows it. This reduces exposure when a new service starts accidentally. It does not replace application authentication, network firewalls, or security updates.
Plan the Policy Before Applying It
List the traffic the server must receive:
- Allow loopback traffic so local programs can communicate.
- Allow replies to connections the server already accepted.
- Allow SSH from approved networks or on the approved port.
- Allow other application ports only when the workload requires them.
- Drop unsolicited inbound traffic.
- Usually allow outbound traffic, unless your design requires egress filtering.
The example below uses an inet table, so the rules apply to both IPv4 and IPv6. It also drops spoofed IPv4 loopback traffic arriving on a non-loopback interface.
# /etc/nftables.conf
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;
}
}The input chain processes packets addressed to this host. ct state established,related permits response packets for allowed connections. The forward chain blocks routing through the server. The output chain permits the server to make outbound connections.
Validate and Load the Rules
Check the rules before loading them. Keep an existing administrative session open while changing a remote firewall. Open a second session and confirm that SSH still works before closing the first one.
sudo nft -c -f /etc/nftables.conf
sudo nft -f /etc/nftables.conf
sudo nft list ruleset
sudo ss -lntupThe first command checks syntax without changing the active ruleset. The second loads the file. nft list ruleset shows the active policy, while ss helps you compare listening services with the ports you intended to expose.
On systems that provide the nftables systemd unit, enable it for boot persistence:
sudo systemctl enable --now nftables
sudo systemctl status nftablesTest from another host. Confirm that SSH succeeds, required application ports respond, and an unapproved port does not. If the server uses IPv6, test IPv6 separately.
Exercise
Why must the established,related rule appear before the default drop takes effect?
Show answer
It gives reply packets for permitted connections an explicit accept decision; otherwise the default input policy would drop those replies.