nc (netcat)
nc (netcat) reads and writes raw data over TCP, UDP and Unix sockets. It is the Swiss army knife for port checks, banner grabs, quick listeners, file transfers and testing through a proxy.
42.1 What it does, where it runs
| Platform | nc flavour |
|---|---|
| Linux | OpenBSD netcat (netcat-openbsd) |
| macOS | Built-in /usr/bin/nc + Apple-only options |
| Windows | Not built in: use ncat from Nmap |
42.2 How a port check works (nc -zv)
- Step 1
1.Client 192.168.62.10 sends SYN to port 80 of server 192.168.62.20
- Step 2
2.Server replies SYN-ACK: port open, nc prints succeeded!
- Step 3
3.SYN to port 23 (no service): server replies RST = Connection refused
- Step 4
4.SYN to port 8080: firewall DROPs it, no reply comes back
- Step 5
5.After -w 3 seconds nc prints timed out = filtered
-z = only scan (handshake, no data), -v = print the result, -w 3 = timeout
nc -zv -w 3 192.168.62.20 80nc -zv -w 3 192.168.62.20 23nc -zv -w 3 192.168.62.20 8080nc -zv -w 1 192.168.62.20 80 20-23$ nc -zv -w 3 192.168.62.20 80Connection to 192.168.62.20 80 port [tcp/http] succeeded!$ nc -zv -w 3 192.168.62.20 23nc: connect to 192.168.62.20 port 23 (tcp) failed: Connection refused$ nc -zv -w 3 192.168.62.20 8080nc: connect to 192.168.62.20 port 8080 (tcp) timed out: Operation in progress$ nc -zv -w 1 192.168.62.20 80 20-23Connection to 192.168.62.20 80 port [tcp/http] succeeded!nc: connect to 192.168.62.20 port 20 (tcp) failed: Connection refusednc: connect to 192.168.62.20 port 21 (tcp) failed: Connection refusednc: connect to 192.168.62.20 port 22 (tcp) failed: Connection refusednc: connect to 192.168.62.20 port 23 (tcp) failed: Connection refused| nc result | Packet received | Port state |
|---|---|---|
| succeeded! | SYN-ACK | Open |
| Connection refused | RST | Closed |
| timed out | Nothing | Filtered (firewall drop) |
42.3 Source address and port
- -s IP: send from this local IP; -p port: fixed source port.
- A verbose listener (-lnv) on the server shows exactly where the connection came from.
# servernc -lnv 1234# clientecho hello from client | \ nc -N -s 192.168.62.10 -p 31337 \ 192.168.62.20 1234server$ nc -lnv 1234Listening on 0.0.0.0 1234Connection received on 192.168.62.10 31337hello from client42.4 Listener and file transfer
- Step 1
1.Server: nc -l 1234 > /tmp/file.out (listening on 0.0.0.0 1234)
- Step 2
2.Client: nc -N 192.168.62.20 1234 < /tmp/file.in
- Step 3
3.2 MB of data crosses the switch to the server
- Step 4
4.Server writes file.out (2000000 bytes)
- Step 5
5.End of file: -N shuts the socket down (FIN), connection closes
- Step 6
6.With -k the listener stays up for the next client
# client: a 2 MB random filehead -c 2000000 /dev/urandom > /tmp/file.insha256sum /tmp/file.in# servernc -l 1234 > /tmp/file.out# clientnc -N 192.168.62.20 1234 < /tmp/file.in# serversha256sum /tmp/file.out# -k: keep the listener runningnc -lk 1234 > /tmp/k.txtecho client one | nc -N 192.168.62.20 1234echo client two | nc -N 192.168.62.20 1234cat /tmp/k.txtclient$ sha256sum /tmp/file.in5486fe4a701c429405cd81eaba82d25a30bab8ddd000edc55fc49e5328aca537 /tmp/file.inserver$ ls -l /tmp/file.out-rw-r--r-- 1 root root 2000000 Nov 8 17:50 /tmp/file.outserver$ sha256sum /tmp/file.out5486fe4a701c429405cd81eaba82d25a30bab8ddd000edc55fc49e5328aca537 /tmp/file.outserver$ cat /tmp/k.txtclient oneclient twoSame SHA-256 on both sides: the file arrived unchanged.
42.5 UDP and Unix sockets
- -u: UDP instead of TCP; nc -luk on the server is a simple syslog-style receiver that accepts data from many senders.
- -U: Unix domain socket instead of a port, for processes on the same machine.
- -4 / -6 fix the IP version, -n disables DNS, -Z uses DCCP, -b -u allows UDP broadcast.
- Scanning extras: -r ports in random order, -i N waits N sec per port/line.
# server: UDP syslog-style receivernc -luk 5140 > /tmp/syslog.txt# client 192.168.62.10echo '<14>app1: disk 91% full' | nc -u -w 1 192.168.62.20 5140# proxy 192.168.62.5echo '<11>app2: login failed' | nc -u -w 1 192.168.62.20 5140# servercat /tmp/syslog.txtss -ulnp | grep 5140# Unix-domain socketnc -lU /tmp/n042.sock > /tmp/u.txt &echo via unix socket | nc -NU /tmp/n042.sock$ cat /tmp/syslog.txt<14>app1: disk 91% full<11>app2: login failed$ ss -ulnp | grep 5140UNCONN 0 0 0.0.0.0:5140 0.0.0.0:* users:(("nc",pid=81,fd=3))$ cat /tmp/u.txtvia unix socket42.6 I/O and session options
| Option | What it does |
|---|---|
| -C | Send LF as CRLF (needed for HTTP, SMTP) |
| -d | Do not read stdin |
| -N | Shut down the socket on EOF |
| -q N | Wait N sec after EOF, then quit |
| -w N | Connect / idle timeout |
| -W N | Exit after N packets received |
| -t | Answer telnet DO/WILL with DON'T/WON'T |
| -v / -h | Verbose (stderr) / help |
printf 'GET / HTTP/1.0\n\n' | nc -C -w 3 192.168.62.20 80# server: exit after one datagramnc -u -W 1 -l 5000; echo exit=$?# clientecho first | nc -u -w 1 192.168.62.20 5000HTTP/1.1 200 OKDate: Sun, 08 Nov 2026 17:51:43 GMTConnection: closeContent-type: text/htmlAccept-Ranges: bytesContent-Length: 22 <h1>n042 lab web</h1>server$ nc -u -W 1 -l 5000; echo exit=$?firstexit=042.7 Socket tuning: TTL, TOS, minttl
- -M 5: TTL of outgoing packets = 5; -m 64 on a listener: kernel drops packets with TTL below 64.
- -T lowdelay sets TOS 0x10, -T ef sets DiffServ EF 0xb8; also af11..af43, cs0..cs7 or a number.
- -I / -O: TCP receive / send buffer; -S: TCP MD5 signature (BGP); -V: routing table; -D: SO_DEBUG.
- Verified on the server with tcpdump capturing only SYN packets.
tcpdump -n -v -c 3 -i eth0 'tcp[tcpflags] == tcp-syn' and dst port 1234 > /tmp/cap.txt &echo a | nc -N -M 5 192.168.62.20 1234echo b | nc -N -T lowdelay 192.168.62.20 1234echo c | nc -N -T ef 192.168.62.20 1234# server: minttl 64nc -l -m 64 1234echo low ttl | nc -N -M 5 -w 3 -v 192.168.62.20 1234echo normal ttl | nc -N -w 3 -v 192.168.62.20 1234server$ grep -o 'tos 0x[0-9a-f]*, ttl [0-9]*' /tmp/cap.txttos 0x0, ttl 5tos 0x10, ttl 64tos 0xb8, ttl 64client$ echo low ttl | nc -N -M 5 -w 3 -v 192.168.62.20 1234nc: connect to 192.168.62.20 port 1234 (tcp) timed out: Operation in progressclient$ echo normal ttl | nc -N -w 3 -v 192.168.62.20 1234Connection to 192.168.62.20 1234 port [tcp/*] succeeded!42.8 Through a proxy
- -x host:port: connect via a proxy; -X 4 / 5 = SOCKS version (5 is default), -X connect = HTTP CONNECT proxy; -P user = proxy username.
- The server sees the connection coming from the proxy, not from the client.
- In the lab, ssh -D on 192.168.62.5 turned port 1080 into a real SOCKS proxy.
# proxy 192.168.62.5: SOCKS on 1080ssh -f -N -D 0.0.0.0:1080 root@127.0.0.1# servernc -lknv 1235# clientecho via socks5 | nc -N -x 192.168.62.5:1080 192.168.62.20 1235echo via socks4 | nc -N -X 4 -x 192.168.62.5:1080 192.168.62.20 1235echo x | nc -N -X connect -x 192.168.62.5:1080 192.168.62.20 1235server$ nc -lknv 1235Listening on 0.0.0.0 1235Connection received on 192.168.62.5 38944Connection received on 192.168.62.5 38960via socks5via socks4client$ echo x | nc -N -X connect -x 192.168.62.5:1080 192.168.62.20 1235nc: proxy read: Broken pipe-X connect needs an HTTP CONNECT proxy, so against a SOCKS port it fails with Broken pipe.
42.9 Comparison: Linux nc vs macOS nc
| Option | Linux (OpenBSD nc) | macOS nc |
|---|---|---|
| -C | Send CRLF | Do not use cellular data |
| -c | - | Send CRLF |
| -b | Allow UDP broadcast | Bind to an interface (en0) |
| -I | TCP receive buffer | Keepalive probe interval |
| -G | - | TCP connect timeout |
| -H / -J / -L | - | Keepalive idle / probe count / probes before unreachable |
| -P | Proxy username | Not available |
| --apple-* | - | delegate-pid/uuid, ext-bk-idle, nowakefromsleep, ecn |
- On macOS -s and -p do not work together with -l.
- --apple-ecn default|enable|disable keeps or forces ECN; --apple-nowakefromsleep keeps the port out of the list drivers check on sleep.
42.10 Common mistakes
- Assuming nc options are the same everywhere: always check nc -h on that machine before scripting.
- Sending HTTP/SMTP without -C: servers expect CRLF line endings.
- Trusting a UDP -zu result: UDP has no handshake, so 'open' is a weak signal.
- Forgetting -N on the client: the connection may not close after the file ends.
42.11 Interview tips
- Port reachable without telnet: nc -zv -w 3 host port; only the TCP handshake, no data.
- succeeded = open (SYN-ACK), refused = closed (RST), timed out = filtered (a firewall drops the SYN).
- nc -l + redirect is a quick file transfer; verify with sha256sum.
- On Windows use ncat from Nmap.