Getting Started with nc (netcat) - The Versatile Tool for Port Testing and Debugging

Getting Started with nc (netcat) - The Versatile Tool for Port Testing and Debugging

What You'll Learn

  • How to verify port connectivity with a single nc -zv command
  • How to spin up a temporary listener for connection testing
  • File transfer, banner grabbing, and UDP mode patterns
  • The difference between nc.openbsd and ncat, and how to fix missing -z support

Quick Summary

  • Port checknc -zv host port
  • Simple servernc -l 8080 (receiver side)
  • File transfer → receiver: nc -l 9999 > out.txt, sender: nc host 9999 < in.txt
  • Set a timeout with -w <seconds> to avoid indefinite hangs

What Is nc (netcat)?

nc (netcat) is a general-purpose command-line tool that reads and writes directly to TCP/UDP sockets — earning it the nickname "the Swiss Army knife of networking." Unlike curl (HTTP only) or ping (ICMP only), nc works with any port and any protocol. Since nc can act as both client and server, you can test connectivity before a service is running — making it invaluable for pre-deployment checks, CI environment validation, and live debugging.

How Do You Test Whether a Port Is Open?

Use -z (zero-I/O mode — connects without sending data) combined with -v (verbose output).

$ nc -zv example.com 80
Connection to example.com 80 port [tcp/http] succeeded!

$ nc -zv example.com 22
Connection to example.com 22 port [tcp/ssh] succeeded!

When a port is closed or unreachable:

$ nc -zv example.com 25
nc: connectx to example.com port 25 (tcp) failed: Connection refused

Scan a port range at once:

$ nc -zv example.com 20-25

Add -w <seconds> to set a connection timeout and prevent hanging on unresponsive hosts.

$ nc -zv -w 3 192.168.1.10 443

How Do You Start a Simple Server?

The -l flag puts nc in listen mode, waiting on the specified port. When a connection arrives, nc bridges it to stdin/stdout.

# Receiver side (start first)
$ nc -l 8080

# Sender side (in a separate terminal)
$ nc localhost 8080
Hello from client!

Text typed on the sender appears on the receiver in real time. The connection is bidirectional, so both sides can type.

nc transmits data in plaintext with no authentication or encryption. It is not designed as a persistent production server.

Can You Transfer Files With nc?

Yes — use shell redirection. This is useful for quick data hand-offs in environments where scp/rsync is unavailable.

# Receiver side (start first)
$ nc -l 9999 > received.tar.gz

# Sender side
$ nc receiver-host 9999 < archive.tar.gz

The sender's nc exits when it reaches EOF. If the receiver does not exit automatically, press Ctrl+C.

On OpenBSD-flavor nc, add -q 0 on the receiver side to auto-exit on EOF.

$ nc -l 9999 -q 0 > received.tar.gz

Connect with nc and manually send a protocol-level request to inspect the service response.

HTTP Header Check

$ printf "HEAD / HTTP/1.0\r\n\r\n" | nc example.com 80
HTTP/1.1 200 OK
Server: nginx/1.24.0
...

SMTP Banner

$ nc mail.example.com 25
220 mail.example.com ESMTP Postfix (Ubuntu)

Banner information (software name, version, OS hints) is useful for deployment verification and security assessments.

How Do You Use UDP Mode?

Add the -u flag to switch from TCP to UDP. Use this to test UDP services like DNS (53), NTP (123), and syslog (514).

# UDP port connectivity check
$ nc -u -zv 8.8.8.8 53
Connection to 8.8.8.8 53 port [udp/domain] succeeded!

# Simple UDP listener
$ nc -u -l 5005

UDP is connectionless, so -z checks can be misleading: if the remote host receives the packet but sends no reply, nc still reports failure. For accurate UDP port scanning, consider nmap -sU.

What Are the Different nc Variants?

Linux ships multiple nc implementations with different option sets.

Command Package Notes
nc (OpenBSD) netcat-openbsd Default on Ubuntu/Debian. Supports -z, -w, -q
nc (Traditional) netcat-traditional Older build. No -z; supports -e for shell exec
ncat ncat (bundled with nmap) SSL/TLS support, broker mode

Check which variant is installed:

$ nc -h 2>&1 | head -1
OpenBSD netcat (Debian patchlevel 1.226-1)

Switch to the OpenBSD version on Ubuntu:

$ sudo apt install netcat-openbsd
$ sudo update-alternatives --config nc

Common Pitfalls

1. nc: invalid option -- 'z'

The netcat-traditional package is installed. Install netcat-openbsd and switch to it (see Variants above).

2. "Connection refused" Even Though the Port Should Be Open

The application is not listening, or a firewall is blocking it.

# Check LISTEN state
$ ss -tlnp | grep ':8080'

# Check UFW rules
$ sudo ufw status

3. File Transfer Hangs and Never Completes

The receiver's nc is waiting for more data. On the sender, confirm EOF is sent when the file ends (it is, with redirection). On the receiver, use -q 0 (OpenBSD) or press Ctrl+C.

4. "Address already in use" When Reusing a Port

The port is in TIME_WAIT state. Check with:

$ ss -tlnp | grep ':8080'

Use a different port or wait a few seconds before retrying.

Next Reading