Lesson 15 of 16 · Everyday hygiene
Firewalls, open ports and patching
Reduce what can be reached and keep what's left up to date. Find what is listening with ss, understand bind addresses, set up a default-deny firewall with ufw (and the Docker trap that bypasses it), use cloud security groups as a second layer, then patch: automatic security updates, restarts that actually load the fix, and how to decide what to patch first.
Part 1: shrink what can be reached
What is listening?
$ sudo ss -tulpn
Netid State Local Address:Port Process
tcp LISTEN 0.0.0.0:22 users:(("sshd",pid=812,…))
tcp LISTEN 127.0.0.1:5432 users:(("postgres",pid=990,…))
tcp LISTEN 0.0.0.0:6379 users:(("redis-server",pid=1001,…))
tcp LISTEN [::]:443 users:(("nginx",pid=1200,…))
| Bind address | Reachable from |
|---|---|
127.0.0.1 / [::1] |
This machine only |
A specific IP (e.g. 10.0.1.5) |
That network |
0.0.0.0 / [::] / * |
Every interface, possibly the internet |
In this example Redis on 0.0.0.0:6379 is a problem: open Redis and database ports are among the most common ways servers get compromised. Fix it at the source first (bind to 127.0.0.1, or turn the service off), then add the firewall as a second layer.
Every listening port is a door in your house. 127.0.0.1 is an inside door between rooms; 0.0.0.0 is a door onto the street. First, close or brick up doors you don't use. Then put a guard at the gate (the firewall) who only lets visitors through the doors you chose.
A default-deny firewall with ufw
Allow SSH before enabling, or you lock yourself out:
$ sudo ufw default deny incoming
$ sudo ufw default allow outgoing
$ sudo ufw allow OpenSSH # or: allow from YOUR.IP to any port 22 proto tcp
$ sudo ufw allow 80,443/tcp
$ sudo ufw enable
$ sudo ufw status verbose
ufw is a friendly front end for nftables/iptables. On RHEL-family systems the equivalent is firewalld (firewall-cmd).
The Docker trap
Ports published by Docker (-p 8080:80) are reachable even if ufw denies them, because Docker writes its own NAT rules that are processed before ufw's. Fixes:
- publish only on localhost and put a reverse proxy in front:
-p 127.0.0.1:8080:80; - or add filtering rules to Docker's
DOCKER-USERchain; - and always check from outside with
nmapfrom another machine you own.
Two layers in the cloud
Cloud security groups or firewall rules filter traffic before it reaches the server. Use both: the security group as the outer wall, the host firewall as the inner one, so one misconfiguration is not enough.
Part 2: keep it patched
Most real compromises use known vulnerabilities with fixes already available (lesson 2). Patching is the highest-value habit after not exposing things.
Automatic security updates
$ sudo apt install unattended-upgrades
$ sudo dpkg-reconfigure -plow unattended-upgrades
On Ubuntu this installs security updates daily. Check its log in /var/log/unattended-upgrades/.
Installing is not enough: restart
A running program keeps the old library in memory until it restarts. A new kernel only runs after a reboot.
$ cat /var/run/reboot-required # exists if a reboot is needed
*** System restart required ***
$ sudo needrestart # lists services still using old libraries
Plan regular reboot windows, or use a live-patching service for kernels on servers that can't reboot often.
What to patch first
Not every CVE is urgent for you. Prioritise:
- Vulnerabilities known to be exploited right now (for example on CISA's Known Exploited Vulnerabilities list).
- Anything exposed to the internet: SSH, web servers, VPNs, TLS libraries.
- High-severity issues in software you actually run.
- Everything else on a regular schedule.
Recap
- Find what listens with
ss -tulpn; bind internal services to 127.0.0.1; remove what you don't need. - Default-deny with ufw, allowing SSH first; beware Docker bypassing ufw; add security groups as a second layer.
- Turn on automatic security updates, then restart services and reboot so fixes take effect.
- Patch exploited and internet-facing issues first.
This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.