Firewall rules with ufw
Read a server’s firewall rules with ufw status, open a port to one address only, and test it from another machine with nc, on a pretend network.
A program listening on a port is only half the story. A firewall decides which connections reach it at all. Servers usually block everything coming in, then allow only what they need: ssh for the admins, 80 and 443 for a website, the database port for the app that uses it. On Ubuntu the firewall is managed with ufw (the "uncomplicated firewall"). The machines here (your laptop, web, db and the router) are pretend ones inside your browser, so nothing you type reaches a real network.
Your laptop is getting a reporting tool that reads the shop's database on db, port 5432. Try it now:
nc -zv db 5432nc -z just checks whether a port is open. It times out: something drops the connection without an answer, which is what a firewall does.
Reading the rules
The firewall is part of db, so log in there. Its rules need root to read:
sudo ufw status numbered
Each rule says which port (To), what happens (ALLOW, DENY, REJECT), and from where (From). Anything without a rule falls to the default, shown by sudo ufw status verbose: deny (incoming).
Changing them
| Command | Does |
|---|---|
sudo ufw allow 22/tcp | lets anyone reach port 22 |
sudo ufw allow http | the same, by service name (80) |
sudo ufw allow from 10.0.0.5 to any port 5432 | lets only 10.0.0.5 reach port 5432 |
sudo ufw deny 80/tcp | drops connections, so they time out |
sudo ufw reject 80/tcp | refuses them straight away |
sudo ufw delete 3 | removes rule 3 from the numbered list |
sudo ufw enable | turns the firewall on |
Open a port to as few addresses as you can. A database open to every machine is a database waiting to be found.
Careful:Before sudo ufw enable on a server you reach over ssh, allow port 22 first. Otherwise the firewall locks you out the moment it starts.
Your turn
Let your laptop (10.0.0.5) reach the database on db, port 5432, without opening it to anyone else: (1) on db, look at the firewall rules; (2) allow port 5432 from 10.0.0.5 only; (3) back on your laptop, check with nc -zv db 5432 that the port is open now.
Your task
Let your laptop (10.0.0.5) reach the database on db, port 5432, without opening it to anyone else: (1) on db, look at the firewall rules; (2) allow port 5432 from 10.0.0.5 only; (3) back on your laptop, check with nc -zv db 5432 that the port is open now.
- Look at db’s firewall rules (not done yet)
- Port 5432 is open to 10.0.0.5, and only to it (not done yet)
- From your laptop, nc reaches db on 5432 (not done yet)
The checklist ticks itself off as you work in the terminal: any way that gets the result counts.
Hints come one at a time, then one way to do it. Try each before the next.
What this lesson uses, in one place.
ufw- The firewall: allow or block ports More on ufw
ufw allow from- Allow one address to reach a port: sudo ufw allow from IP to any port N More on ufw
nc- Check whether a port is open
Every everyday command on one page: the Linux commands cheat sheet.