Firewalls, ports and network troubleshooting

Intermediate · 9 min

It works on the server but not from your browser. Listening addresses, refused versus timed out, the two firewalls in the path, and an order to check them in.

Before thisSSH keys, bastions and remote accessDNS records and TTLs in practiceReverse proxies and load balancers with nginx

StackMonsters · 9 min

Firewalls, ports and network troubleshooting

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by StackMonstersReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A port is only reachable from outside if a process listens on it on an outside-facing address. A service bound to 127.0.0.1 answers only the machine itself.
  • Connection refused means the packet reached the host and nothing accepted it. A timeout means the packet or its reply was dropped on the way, usually by a firewall.
  • There are often two firewalls: the host's own (nftables, ufw or firewalld) and the cloud's security group in front of it. Both must allow the port.
  • A sensible host firewall denies inbound by default and opens only what is needed, such as SSH from the bastion and 80 and 443 from anywhere.
  • Troubleshoot in order: name resolution, then whether anything is listening, then local access, then the firewalls, then the app itself.

“It works on the server”

You deploy the API, SSH in, run curl localhost:8080, and get a response. From your laptop the browser spins and gives up. This is the most common network ticket there is, and it has a short list of causes. The skill is checking them in order instead of guessing.

The ingredients are three ideas. A port is a number that picks which program on a host gets a connection. A program listens on a port on a particular address. A firewall decides which packets get through at all. The OSI and TCP/IP lesson has the layers if you want them.

Listening, and on which address

A program does not just listen on “port 8000”. It listens on an address and a port. 127.0.0.1 is the loopback address, which only the machine itself can reach. 0.0.0.0 means every address the machine has. I started two Python web servers on this Mac, one on each, and asked lsof what was listening:

$ python3 -m http.server 8000 --bind 127.0.0.1 &
$ python3 -m http.server 8001 --bind 0.0.0.0 &
$ lsof -nP -iTCP:8000-8001 -sTCP:LISTEN     # some columns trimmed
COMMAND ... TYPE NODE NAME
Python  ... IPv4 TCP  127.0.0.1:8000 (LISTEN)
Python  ... IPv4 TCP  *:8001 (LISTEN)

Then I connected to both through the Mac’s network address (I have replaced it with 192.168.1.20 here):

$ curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8000/
200
$ curl -sS -o /dev/null http://192.168.1.20:8000/
curl: (7) Failed to connect to 192.168.1.20 port 8000 after 1 ms: Couldn't connect to server
$ curl -s -o /dev/null -w '%{http_code}\n' http://192.168.1.20:8001/
200

Same machine, same kind of server. The one bound to loopback works for localhost and is invisible on the network. Many frameworks bind to 127.0.0.1 by default in development, which is exactly the “works on the server” trap. On Linux, ss -tlnp shows the same thing as my lsof line, with the owning process.

Refused versus timed out

How a connection fails tells you where to look. I tried three targets with nc -z, which only tests whether a TCP connection opens:

$ nc -vz -w 3 github.com 443
Connection to github.com port 443 [tcp/https] succeeded!
$ nc -vz -w 3 127.0.0.1 8000          # after stopping the server
nc: connectx to 127.0.0.1 port 8000 (tcp) failed: Connection refused
$ nc -vz -G 3 github.com 81
nc: connectx to github.com port 81 (tcp) failed: Operation timed out

Refused came back instantly. The host was reached, and its kernel answered “nothing here” with a reset. Timed out means nothing came back at all: something between me and GitHub dropped my packet for port 81 without a word. That silence is what a firewall set to drop looks like, and it is also what a wrong route or a dead host looks like.

One gotcha from doing this on macOS. My first attempt at port 81 used nc -w 3, like the other two, expecting a 3-second limit. It took 75 seconds to time out. The macOS man page describes -w as an idle timeout on a connection that is already open, so it never limited the connect, which waited for the system’s own limit. -G 3, documented there as the TCP connection timeout, stopped it at 3 seconds. curl --connect-timeout 3 worked as expected and reported Timeout was reached after 3006 ms.

The two firewalls

On a cloud server, a packet passes two independent filters. First the provider’s security group or network firewall, which you configure in the cloud console or with Terraform and which comes up again in virtual networks and security groups. Then the host’s own firewall. On modern Linux that is nftables in the kernel. Most people drive it through a front end: ufw on Ubuntu, firewalld on the Red Hat family, or the iptables command, which on current distributions translates its rules into nftables.

A sensible host policy denies inbound traffic by default and opens what you need. With ufw, from its documentation, and not run here:

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow from 10.0.1.10 to any port 22 proto tcp   # SSH from the bastion only
sudo ufw allow 80,443/tcp                                 # web from anywhere
sudo ufw enable
sudo ufw status verbose

Allow SSH before you run enable, and keep a session open, or you lock yourself out. A deny rule drops silently and produces a timeout. A reject rule sends back a refusal, which is friendlier to legitimate clients and slightly more informative to scanners.

An order to check things in

When something is unreachable, walk outward from the app:

  1. Name. Does dig +short notes.example.com return the address you expect?
  2. Listening. On the server, does ss -tlnp show the port, on 0.0.0.0 or the right interface, not 127.0.0.1?
  3. Local. Does curl -v localhost:PORT work on the server itself?
  4. Host firewall. Does ufw status or nft list ruleset allow the port?
  5. Cloud firewall. Does the security group allow it from your source address?
  6. Path. Refused or timed out from outside? tcpdump -n port 443 on the server shows whether your packets arrive at all.

Each step either finds the fault or rules out a layer. Skipping to step 6 first is how an hour disappears.

About the video

“Linux Server Networking For Complete Beginners (In 9 Minutes)” is from StackMonsters, published October 2026, 9 minutes 6 seconds. Its chapters follow almost exactly this lesson’s problem: an app that answers on the server but not in the browser, ip a and loopback, ports, ss -tulpn, 127.0.0.1 versus 0.0.0.0, refused versus timed out with curl -v, tcpdump and the TCP handshake, an AWS security group, ufw, nginx in front, and DNS. It is a small, new channel. English was judged from the title, description, channel and its English auto-caption track. Learn Linux TV’s ufw guide in deeper goes deeper on the host firewall.

Where this fits

That completes the server and network group. The next group changes the unit you ship, from a program installed on a server to an image that carries its own filesystem: what a container really is.

Check yourself

  1. 01curl localhost:8080 works on the server, but from your laptop the connection is refused. What is the most likely cause?

    The app is bound to 127.0.0.1, so it only accepts connections from the server itself. Bind it to 0.0.0.0, or put a reverse proxy on a public address in front of it.

  2. 02From your laptop the connection hangs and times out instead of being refused. What does that point to?

    Packets are being dropped, typically by a firewall or security group that silently drops instead of rejecting, or because there is no route to the host.

  3. 03Which command on a Linux server shows what is listening on which address and port, with the owning process?

    ss -tlnp, which lists TCP listening sockets with numeric addresses and the process name and PID.

  4. 04You opened port 443 in ufw but the site is still unreachable from the internet on a cloud VM. What else do you check?

    The cloud security group or network firewall in front of the VM. It is a separate firewall and also has to allow inbound 443.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark