Ncat: Connection refused. comes back almost instantly, which makes it tempting to blame a firewall. But the message usually means the opposite: something on the path answered. A TCP stack—or a reject rule—actively refused the connection because no socket was listening on the exact IP address and port Ncat tried.
That distinction matters because Ncat gives you several ways to create the mismatch yourself: a listener bound to one address, a client using the other address family, an access filter that behaves differently than you expect, a one-shot listener that already exited, or a script that connects before the listener is ready. This guide works through those causes in order and finishes with a two-terminal loopback test that proves the fix.
Table of Contents
- What "Connection refused" means in Ncat
- Check what the listener actually bound to
- Separate IPv4 from IPv6
- Rule out Ncat's access controls
- Check the startup race
- Prove the fix with a two-terminal loopback test
- When the refusal is remote
- Conclusion
What "Connection refused" means in Ncat
At the socket level, the Linux connect(2) manual page defines ECONNREFUSED as a stream-socket connect that "found no one listening on the remote address." In the normal TCP case, the client sends a SYN, the destination finds no listening socket, and its TCP stack replies with a reset. RFC 793's reset-generation rules describe this directly: a SYN addressed to a non-existent connection is rejected with a reset.
A reset is not the only way to produce the error. Linux's REJECT target can send a TCP RST with --reject-with tcp-reset, so a firewall rule can make a reachable port look closed. The iptables REJECT target documentation confirms that this option sends a TCP RST back to the sender. In other words, "refused" tells you an active rejection arrived; it does not prove that no firewall is involved.
The timing is the first useful clue:
| What Ncat prints | What came back | What it usually means |
|---|---|---|
Ncat: Connection refused. |
TCP RST, or an ICMP reject response | No listener on that exact IP and port, or a reject rule |
Ncat: Connection timed out. |
Nothing | Packets are being dropped: a DROP rule, a routing black hole, or an overloaded listener |
Ncat: No route to host. |
ICMP unreachable or a local routing failure | Wrong route, VPN, subnet, or ARP problem |
A refusal normally returns in one round trip—often well under a millisecond on loopback. A timeout waits for the connect timeout you set with -w/--wait. Run ncat -v -w 5 <host> <port> and watch whether the error appears immediately or after five seconds; that single observation separates an active rejection from a filtered path.
Check what the listener actually bound to
Ncat's listen mode accepts an optional host. The Ncat reference guide explains that if the host is omitted, Ncat listens on all available addresses over IPv4 and IPv6; if the port is omitted, it defaults to 31337. Supplying a host narrows the bind to that one address.
That gives you very different listeners:
ncat -l 8080 # all local interfaces, IPv4 and IPv6
ncat -l 127.0.0.1 8080 # IPv4 loopback only
ncat -l ::1 8080 # IPv6 loopback only
ncat -l 192.0.2.10 8080 # one specific IPv4 address
The quickest way to see the difference is ss:
ss -ltnp 'sport = :8080'
A listener bound only to loopback looks like this:
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 1 127.0.0.1:8080 0.0.0.0:* users:(("ncat",pid=4242,fd=3))
A wildcard listener looks more like this:
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 1 0.0.0.0:8080 0.0.0.0:* users:(("ncat",pid=4242,fd=3))
LISTEN 0 1 [::]:8080 [::]:* users:(("ncat",pid=4242,fd=4))
Read the Local Address:Port column carefully. 127.0.0.1:8080 accepts only connections addressed to IPv4 loopback. 0.0.0.0:8080 accepts IPv4 connections on any local address. [::]:8080 is the IPv6 wildcard. If you connect to a LAN address while the listener is bound to 127.0.0.1, the SYN reaches the host, finds no matching socket, and gets a reset. The client sees Connection refused even though Ncat is running perfectly.
If the process column is empty, run ss with the privileges needed to see other users' sockets. For a deeper explanation of how binding and address families interact, the Nmap Project's overview of Ncat's IPv4/IPv6 and listen-mode behavior shows why the same listener can answer on one address and refuse on another.
There is a second, easy-to-miss bind problem: the listener may have already exited. Without -k/--keep-open, a plain listen-mode Ncat is one-shot—it handles a single connection and exits after that client disconnects. A test that succeeded once and then gets Connection refused on the second attempt is often just a listener that has already done its job. Use ncat -lk 127.0.0.1 8080 when you need repeated connections.
Separate IPv4 from IPv6
localhost is not a single address. On many Linux systems it resolves to both ::1 and 127.0.0.1, and the order depends on /etc/hosts, NSS, and the resolver. Ncat's -4 and -6 options force one family, which is exactly what you want for diagnosis—and exactly what can make the mismatch obvious.
GNU bug #18914 is a textbook example. A Guile REPL listened on 127.0.0.1, ncat localhost 37146 returned Ncat: Connection refused, and ncat 127.0.0.1 37146 connected immediately. The GNU bug report records the full sequence.
Do not rely on hostname resolution order. Test each path explicitly:
ncat -v -4 127.0.0.1 8080
ncat -v -6 ::1 8080
ncat -v -4 localhost 8080
ncat -v -6 localhost 8080
Whichever test connects tells you which family the listener actually serves. A refusal from the -6 test against an IPv4-only listener is expected behavior, not a new fault. To see how the name resolves on your machine, run:
getent ahosts localhost
In scripts, prefer a numeric address (127.0.0.1 or ::1) over localhost. If the service must accept both families, bind the wildcard with ncat -l 8080 and verify with ss that both an IPv4 and an IPv6 socket appear.
Rule out Ncat's access controls
Ncat's own --allow and --deny options are a common suspect, but they produce a different symptom. The Nmap access-control guide explains that --allow permits only the listed hosts and denies all others, while --deny rejects the listed hosts and accepts the rest. The lists accept addresses, hostnames, ranges, and CIDR netmasks.
The important detail is where the check happens. In Ncat's listen-mode source, Ncat calls accept() first, then runs allow_access(), and if the check fails it closes the socket. The TCP handshake has already completed. A denied client therefore sees a connection that opens and is then dropped—typically Connection reset by peer when it sends data—not an immediate Connection refused.
That means if Ncat says Connection refused, changing --allow will not fix it. If the connection establishes and then dies, run the listener with -v; Ncat logs New connection denied: not allowed for a filter rejection. The same post-accept close happens when the connection limit is reached; the default limit is 100, configurable with --max-conns.
ncat -l -v --allow 127.0.0.1 8080
ncat -l -v --deny 192.0.2.10 8080
Watch both terminals while you test. If the client connects and immediately loses the session, check access filters and the connection limit. If the client never connects at all, go back to the bind address and address-family checks.
Check the startup race
Scripts create a different race. ncat -l 8080 & returns as soon as the process is forked, not when bind() and listen() have completed. The next line can run ncat 127.0.0.1 8080 before the socket exists, and the kernel correctly refuses it.
Linux's own BPF selftests hit this. The commit “selftests/bpf: Prevent client connect before server bind in test_tc_tunnel.sh” describes the netcat server taking time to start listening and the test failing with Ncat: Connection refused; the fix added a loop that waits for the port to appear before the client connects.
Use the same pattern: poll ss until the listening socket exists, then connect.
ncat -lk 127.0.0.1 8080 >/dev/null 2>&1 &
listener_pid=$!
trap 'kill "$listener_pid" 2>/dev/null' EXIT
for attempt in $(seq 1 50); do
if ss -ltn 'sport = :8080' | grep -q LISTEN; then
break
fi
sleep 0.1
done
ncat -v 127.0.0.1 8080
-k keeps the listener alive for the rest of the script, and the trap cleans it up. Do not use a fixed sleep 1 as your only synchronization: it hides the race on fast machines and still fails on slow ones. Polling for the actual socket is deterministic.
Prove the fix with a two-terminal loopback test
Use two terminals on the same machine. This removes DNS, routing, and remote firewalls from the picture.
In terminal 1, start a listener bound only to IPv4 loopback:
ncat -lk 127.0.0.1 8080
In terminal 2, connect:
ncat -v 127.0.0.1 8080
Type a line and press Enter. Terminal 1 should print it. Press Ctrl-D in terminal 2 to close that client; because of -k, terminal 1 stays up.
Now prove the family boundary:
ncat -v -4 localhost 8080
ncat -v -6 ::1 8080
The IPv4 test connects. The IPv6 test is refused because the listener is bound to 127.0.0.1. That refusal is correct behavior, not a new problem.
Next, restart the listener on the wildcard:
ncat -lk 8080
Check the bind with ss -ltnp 'sport = :8080' and repeat both family tests. Both should connect. The comparison looks like this:
| Command | Listener on 127.0.0.1:8080
|
Listener on all addresses |
|---|---|---|
ncat -4 127.0.0.1 8080 |
Connects | Connects |
ncat -6 ::1 8080 |
Refused | Connects |
ncat 192.168.1.20 8080 |
Refused | Connects if the firewall allows it |
Once the loopback test passes, move outward one variable at a time: same host but LAN address, then another host on the LAN, then the remote network. At each step you know whether the failure is local binding, address family, or something in between.
When the refusal is remote
On a remote target, the timing of the failure is the most useful clue.
ncat -v -w 5 192.0.2.20 8080
An immediate refusal means an active rejection came back. A five-second timeout means the SYN went into a black hole. Those are different problems.
Check the server's listening socket first with ss -ltnp 'sport = :8080'. A service bound to 127.0.0.1 is unreachable from another host even though it is running perfectly.
Then inspect packet filters. iptables -S and nft list ruleset show the rules. A REJECT rule can send a TCP reset with --reject-with tcp-reset, which Ncat reports as Connection refused; a DROP rule produces a timeout instead. Cloud security groups and network ACLs commonly drop silently, so a timeout there points at the cloud layer rather than at Ncat.
Conclusion
Ncat: Connection refused is a precise signal: something actively rejected the connection, and the most common reason is that no socket is listening on the exact address and port the client used. Work through the Ncat-specific checks in order: read the listener's real bind address with ss -ltnp, test IPv4 and IPv6 separately, remember that --allow/--deny close an accepted connection rather than refusing it, confirm that a one-shot listener has not already exited, and wait for the socket before connecting from a script. The two-terminal loopback test then gives you a known-good baseline before you add DNS, routing, and firewalls back into the picture.
Top comments (0)