Skip to content

Refused or timed out? Find the firewall rule

NinjaLesson 60 of 669 min

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 80
nc -zv -w 3 web 22

If 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.

CommandDoes
sudo ufw statusis it on, and the rules
sudo ufw status numberedthe rules with numbers, to delete one
sudo ufw status verbosealso the default policy for anything no rule mentions
sudo ufw allow 80/tcplet port 80 in
sudo ufw delete 3remove 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.

Next