Refused or timed out? Find the firewall rule
Tell a refused connection from a dropped one with curl and nc, find the ufw rule that drops it, and open just that port without switching the firewall off.
A connection can fail in two very different ways, and knowing which one you've got cuts the search in half.
Refused or timed out
- Refused comes back at once. The machine is there and answered "nothing listens on that port". Look for a service that isn't running.
- Timed out takes seconds and ends in silence. Nothing answered at all: the packets were dropped on the way, usually by a firewall, or the machine is off.
curl waits a long time before giving up, so give it a limit with -m (seconds). nc -zv checks one port and -w sets its timeout:
curl -m 5 http://web/nc -zv -w 3 web 80nc -zv -w 3 web 22If 22 answers and 80 times out, the machine is up and something stands in front of port 80. (web is one of the pretend machines inside this page.)
ufw: the firewall, in sentences
Ubuntu's firewall front end is ufw (uncomplicated firewall). It needs root, and it lives on the machine it protects, so log in to web first.
| Command | Does |
|---|---|
sudo ufw status | is it on, and the rules |
sudo ufw status numbered | the rules with numbers, to delete one |
sudo ufw status verbose | also the default policy for anything no rule mentions |
sudo ufw allow 80/tcp | let port 80 in |
sudo ufw delete 3 | remove rule 3 |
Rules are checked top to bottom and the first one that matches wins. DENY drops packets silently (a timeout for the client); REJECT answers with a refusal, which is kinder to your own users and tells attackers more.
Tip:sudo ufw disable also "fixes" it, by turning off the firewall for every port. Don't: open the one port you need.
Underneath, ufw writes rules for the kernel's own firewall (nftables or iptables), so these rules are real ones, written in a friendlier way.
Your turn
Requests to http://web/ hang and then fail. Show that port 80 times out while ssh still answers, find the rule on web that blocks it, and open port 80 with the firewall still on.
Your task
Requests to http://web/ hang and then fail. Show that port 80 times out while ssh still answers, find the rule on web that blocks it, and open port 80 with the firewall still on.
- Port 80 times out (not done yet)
- Port 22 still answers (not done yet)
- Find the rule that blocks it (not done yet)
- Port 80 answers, firewall still on (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.
nc -z- Just check, send nothing
curl -m- Fetch a URL, or test a web server More on curl
ufw status- On or off, and the rules (numbered: with numbers for delete) More on ufw
ufw allow- Accept connections, for example 80/tcp, http or OpenSSH More on ufw
ufw delete- Remove a rule, written as it was added, or by number More on ufw
Every everyday command on one page: the Linux commands cheat sheet.