A host firewall is your second line of defence. If a service ever starts listening that you did not intend, a default-deny firewall means it is not reachable until you decide it should be. This lesson sets one up, tightens the kernel's network behaviour, and proves it works.
Order matters. Add the SSH allow rule before you enable the firewall. Keep your console handy and your first session open.
Option A: ufw (simplest, recommended for most servers)
ufw is Ubuntu's friendly front end to the packet filter.
sudo ufw default deny incoming
sudo ufw default allow outgoing
# SSH only from your admin range (use your real range; 203.0.113.0/24 is a documentation example)
sudo ufw allow from 203.0.113.0/24 to any port 22 proto tcp
# Public services you actually serve
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw logging medium
sudo ufw enable
sudo ufw status verbose numberedUseful extras:
sudo ufw limit 22/tcp # rate-limit SSH if it must be open to many addresses
sudo ufw delete 3 # remove rule number 3 (use the numbered status)
sudo ufw reloadDocker bypasses ufw. Docker inserts its own firewall rules, so
-p 8080:8080can expose a port even when ufw says it is blocked. Publish container ports on loopback (-p 127.0.0.1:8080:8080) behind a reverse proxy, or manage theDOCKER-USERchain. Always test from outside.
Check IPv6 too: /etc/default/ufw should say IPV6=yes so the same rules apply to IPv6 traffic.
Option B: nftables (explicit, scriptable)
Prefer one tool. Do not run ufw and a hand-written nftables ruleset together.
sudo tee /etc/nftables.conf >/dev/null <<'EOF'
#!/usr/sbin/nft -f
flush ruleset
table inet filter {
chain input {
type filter hook input priority 0; policy drop;
ct state established,related accept
ct state invalid drop
iif "lo" accept
meta l4proto icmp icmp type echo-request limit rate 5/second accept
meta l4proto ipv6-icmp accept
tcp dport 22 ip saddr 203.0.113.0/24 accept
tcp dport { 80, 443 } accept
}
chain forward { type filter hook forward priority 0; policy drop; }
chain output { type filter hook output priority 0; policy accept; }
}
EOF
sudo nft -c -f /etc/nftables.conf # check the syntax, applies nothing
sudo systemctl enable --now nftables # load now and at boot
sudo nft list rulesetWhy each line: established/related keeps replies flowing, invalid drops malformed packets, loopback lets local services talk, ICMPv6 must be allowed because IPv6 needs it to work, and the policy is drop for everything not listed.
Back-out: from the console, sudo ufw disable or sudo nft flush ruleset.
Kernel network hardening with sysctl
sudo tee /etc/sysctl.d/99-net-hardening.conf >/dev/null <<'EOF'
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.all.log_martians = 1
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.tcp_syncookies = 1
EOF
sudo sysctl --system| Setting | What it stops |
|---|---|
rp_filter |
Spoofed source addresses |
accept_redirects, send_redirects |
ICMP-redirect route poisoning and acting as an accidental router |
accept_source_route |
Sender-chosen routing paths |
log_martians |
Logs impossible packets, useful for detection |
tcp_syncookies |
SYN-flood resource exhaustion |
Do not blindly set
net.ipv4.ip_forward = 0. Docker, Kubernetes, VPN and routing hosts need forwarding. Leave it alone on those, and rely on the firewall'sforwardpolicy.
Remove listeners nobody needs
sudo ss -tulpn # what is listening, and which process owns it
systemctl list-unit-files --state=enabled | head -50
sudo systemctl disable --now cups avahi-daemon # examples: remove only what you do not use
sudo apt purge -y telnet rsh-client # old clear-text clientsA service that listens on 0.0.0.0 but only serves local clients should be reconfigured to bind 127.0.0.1. Check the service's own config (databases, caches and admin panels are frequent offenders).
Prove it from outside
From a different machine (against your own server only):
nmap -Pn -sS -p- SERVER_IP # every TCP port: only the ones you opened should answer
nmap -Pn -sU --top-ports 50 SERVER_IP # common UDP ports (slow; run when you can wait)If a port you did not open responds, find the process with ss -tulpn and decide: close it, bind it locally, or allow it deliberately.
🧪 Lab
- Enable ufw with default deny, scoped SSH and one public port, then confirm SSH still works from a second session.
- Run the outside scan and confirm only the expected ports answer.
- Apply the sysctl file and verify with
sysctl net.ipv4.conf.all.rp_filter. - If you use Docker, publish a test container and see whether ufw really blocks it. Fix it.
Checkpoint
- Why add the SSH rule before enabling the firewall?
- Why must ICMPv6 be allowed in an IPv6-capable ruleset?
- Why should you not run ufw and a custom nftables ruleset at the same time?
- Which sysctl setting should you leave alone on a Docker host, and why?
- How do you prove the firewall works, rather than assume it does?