Skip to content

Connection refused although the service runs: bind address 127.0.0.1

Networkingtested on Ubuntu 24.04, iproute2 6.1.0

“The database is running, I can reach it locally” – yet every other machine gets Connection refused immediately. Refused arrives within milliseconds: the host is there, but nobody listens on this address and port. Most often the service is bound to the loopback address only.

Symptom

sudo ip netns exec client nc -zv -w3 10.2.0.20 5432
nc: connect to 10.2.0.20 port 5432 (tcp) failed: Connection refused

The client cannot reach the database stand-in on port 5432. Inside the db namespace it works via 127.0.0.1 – but not even there via its own LAN address.

Diagnosis

ss shows in one line which address the service listens on.

sudo ip netns exec db ss -ltnp 'sport = :5432'
State  Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0      5          127.0.0.1:5432      0.0.0.0:*    users:(("python3",pid=50798,fd=3))

Causes

127.0.0.1:5432The service listens on the loopback address only

Many databases, admin UIs and development servers bind to 127.0.0.1 by default. The service is then reachable only from the same machine (or namespace, container) via 127.0.0.1 – not even via its own LAN address.

sudo ip netns exec db nc -zv -w3 10.2.0.20 5432
nc: connect to 10.2.0.20 port 5432 (tcp) failed: Connection refused

Container127.0.0.1 in a container is the container's loopback

If a development server in a container listens on 127.0.0.1, port publishing does not help either: the connection arrives on the container's interface, not on its loopback.

other addressThe service is deliberately bound to another address

If the service is bound to an address on the storage network on purpose, it is meant to be unreachable over the LAN – then refused is the correct answer.

Fix

Bind the service to exactly the address it should be reachable on – in the course only to the storage network.
sudo systemctl stop db-app
sudo systemd-run --unit=db-app ip netns exec db python3 -m http.server 5432 --bind 10.10.0.20
sudo ip netns exec web nc -zv -w3 10.10.0.20 5432
Connection to 10.10.0.20 5432 port [tcp/postgresql] succeeded!

Don't: Reflexively bind to 0.0.0.0. That opens every entrance – including networks nobody thought of when setting it up.

▶
Break it yourself and find it – free
Network debugging, Day 5: who is listening? Reading ss, bind addresses, Address already in use and connection states.
Go to day 5 →

FAQ

How do I tell Connection refused from a timeout?

Refused arrives within milliseconds as a TCP reset from the target host – it is there, only the port is closed. A timeout lasts as long as the client waits and means no answer comes at all.

What does 0.0.0.0 in the Local Address column mean?

The service listens on all IPv4 addresses of the host, including those it gets later.

Related error patterns

More