netcat (nc) — The TCP/IP Swiss Army Knife

Practical guide to netcat (nc) — read and write raw TCP and UDP connections, scan ports, transfer files, and debug network services from the command line.

netcat (nc) is the TCP/IP Swiss Army knife: a single command lets you read and write raw data across TCP and UDP connections, scan ports, transfer files between machines, and debug network services straight from the shell. Available on virtually every Unix system without extra dependencies, nc is the first tool administrators reach for when something on the network needs a quick check. This guide covers the most useful options for legitimate admin and diagnostic work – from a fast port scan to a compressed directory transfer.

TCP Connections

nc <host> <port> — Connect to a TCP port.

nc example.com 80

nc -v <host> <port> — Connect with verbose output.

nc -v example.com 443

nc -z <host> <port> — Port scan: check if a port is open (no data sent).

nc -zv example.com 80

nc -z <host> <start>-<end> — Scan a range of ports.

nc -zv localhost 3000-3100

nc -w <seconds> <host> <port> — Set a connection timeout.

nc -w 5 -zv example.com 443

Listen & Server

nc -l <port> — Listen on a TCP port (simple server).

nc -l 8080

nc -lk <port> — Listen and keep accepting connections (persistent).

nc -lk 8080

nc -l <port> > <file> — Listen and save received data to a file.

nc -l 9999 > received.tar.gz

nc <host> <port> < <file> — Send a file to a listening netcat.

nc 192.168.1.10 9999 < backup.tar.gz

UDP

nc -u <host> <port> — Connect via UDP instead of TCP.

nc -u localhost 5000

nc -lu <port> — Listen for UDP packets.

nc -lu 5000

echo '<data>' | nc -u <host> <port> — Send a UDP message.

echo 'test message' | nc -u localhost 5000

HTTP & Testing

echo -e 'GET / HTTP/1.1\r\nHost: <host>\r\n\r\n' | nc <host> 80 — Send a raw HTTP GET request.

echo -e 'GET / HTTP/1.1\r\nHost: example.com\r\n\r\n' | nc example.com 80

nc -l 8080 <<< 'HTTP/1.1 200 OK\r\n\r\nHello' — Create a minimal one-shot HTTP server.

while true; do echo -e 'HTTP/1.1 200 OK\r\n\r\nHello' | nc -l 8080; done

echo 'QUIT' | nc <host> 25 — Test if an SMTP server is responding.

echo 'QUIT' | nc -w 3 mail.example.com 25

File Transfer

nc -l <port> > <file> (receiver) — Receive a file (run on destination machine).

nc -l 9999 > backup.tar.gz

nc <host> <port> < <file> (sender) — Send a file (run on source machine).

nc 192.168.1.10 9999 < backup.tar.gz

tar czf - <dir> | nc <host> <port> — Send a directory as a compressed stream.

tar czf - project/ | nc 192.168.1.10 9999

nc -l <port> | tar xzf - — Receive and extract a compressed stream.

nc -l 9999 | tar xzf -

Common Patterns

nc -zv <host> <port> 2>&1 | grep succeeded — Quick connectivity check (script-friendly).

nc -zv db.example.com 5432 2>&1 | grep succeeded

nc -l <port> -e /bin/bash — Bind a shell to a port (for authorized testing only; netcat-traditional or ncat only; not in OpenBSD nc).

nc -l 4444 -e /bin/bash

nc -l <local-port> | nc <remote-host> <remote-port> — Simple TCP proxy / relay.

nc -l 8080 | nc backend.local 3000

echo -n | nc -w 1 <host> <port> && echo 'open' || echo 'closed' — Quick port check with open/closed output.

echo -n | nc -w 1 localhost 3306 && echo 'MySQL open' || echo 'MySQL closed'

Conclusion

netcat remains indispensable because it needs neither extra dependencies nor elevated privileges: a single nc command handles port checks, quick LAN file transfers, and debugging new network services. When you need TLS support, multiple concurrent connections, or proxy features, reach for socat as the more capable alternative.

Further Reading

  • socat – more powerful socket relay with TLS support
  • nmap – advanced port and service scanner
  • ss – inspect active socket connections and listening ports