nc
a raw network connectionnc — netcat — connects to a port and joins it to your keyboard and screen. With nothing else in the way, it shows whether a port is reachable and what the service says first. It can also listen on a port and accept a connection.
nc -zv host port answers the most common network question: is that port open from here?
At the terminal
Read the session first, then the notes: each numbered line in the output is answered by the note with the same number.
What the numbered lines are telling you
A TCP connection was made and closed. -z sends no data; -v reports the result.
The host answered, but nothing is listening on that port. A firewall silently dropping the packets would instead hang until it timed out.
Connecting without -z shows what the server sends first — here the SSH version string.
nc waits on port 9000. Whatever another machine sends to that port appears here; it is a quick way to test that a firewall rule lets traffic through.
Switches
A switch changes how the command behaves. These are the ones used day to day; man nc lists the rest. Single-letter switches can usually be joined, so -l -a may be written -la.
-z- Zero I/O: just test whether the port is open.
-v- Verbose: report the result.
-l- Listen for an incoming connection.
-u- Use UDP instead of TCP.
-w SECS- Time out after this many seconds.
-n- Numeric only: do not look up names.
-k- Keep listening after a client disconnects.
Variations worth knowing
The same tool, pointed at a narrower question. These four cover most of what it is reached for day to day.
nc -zv 192.0.2.10 20-25Tests a range of ports.
nc -zvw 3 192.0.2.10 443Gives up after three seconds instead of waiting.
printf "GET / HTTP/1.0\r\n\r\n" | nc example.com 80Sends an HTTP request by hand and prints the raw response.
nc -zvu 192.0.2.53 53Tests a UDP port.
In cybersecurity
netcat is equally a diagnostic tool and an attacker’s tool.
Many services announce their software and version on connect. That is what an attacker matches against lists of known vulnerabilities.
netcat can attach a shell to a network connection. An nc process on a server that nobody started, especially with -e or connected to an outside address, is an incident.
Probing ports on systems you do not own or have permission to test is unauthorised access in many jurisdictions.