Checking ports
Distinguish the responses: succeeded/open — the port is listening; Connection refused — reached the host, but nothing is listening; timed out — never reached it (firewall/routing).
Checking whether ports are reachable, simple data transfer over TCP/UDP.
nc -zvw3 host 22
is the port open? (refused vs timed out)
nc -zvu host 53
UDP
nc -zv host 20-25
a range
nc -l 9000
listen (a test server)
echo data | nc host 9000
send
nc -l 9000 > out.bin
receive a file (on the other side — nc host 9000 < file.bin)
nc (netcat) is the TCP/UDP "Swiss Army knife": check whether a port is
open, send raw data, spin up a temporary listener, move a file between
machines.
On Ubuntu this is usually
netcat-openbsd(options as below). There's also ancatvariant (from nmap) with similar syntax.
| Option | Meaning |
|---|---|
-z |
scan mode: only check, don't send data |
-v |
verbose output (refused / open / timed out) |
-u |
UDP instead of TCP |
-l |
listen (server mode) |
-p N |
the local port |
-w N |
connection timeout, in seconds |
-n |
skip DNS resolution (faster) |
-k |
(with -l) don't close after one client |
| Option | What it does | Example |
|---|---|---|
-z |
Scan mode — only check reachability, do not send data. | nc -zv host 22 |
-v |
Verbose output — refused, open, timed out. | nc -zv host 80 |
-l |
Listen for an incoming connection (server mode). | nc -l 9000 |
-w N |
Connection timeout in seconds — keeps the command from hanging forever. | nc -zvw3 host 22 |
-u |
Use UDP instead of TCP. | nc -zvu host 53 |
-n |
Do not resolve DNS names — faster and more reliable for checks. | nc -zvn 10.0.0.5 22 |
-p N |
Use a specific local port. | nc -p 5000 host 9000 |
-k |
With -l — keep listening after one client disconnects. | nc -lk 9000 |
Nothing found. Try a shorter query.
Distinguish the responses: succeeded/open — the port is listening; Connection refused — reached the host, but nothing is listening; timed out — never reached it (firewall/routing).
nc -zvw3 host port. refused is not the same as timed out: the
first means the service isn't running/it's the wrong port (the network is
fine); the second means it never got there (firewall/routing). This is the
key distinction.
nc -l 8080 on one side, nc -zv host 8080 on the other — does TCP/UDP get
through on that exact port (useful when debugging a firewall).
Without -w, a connection can wait forever. Add a timeout (-w3), and
-z for a clean check with no data exchange.
Found an inaccuracy? Report an error in the article