Linux Command Reference ยท Networking
Linux nc (netcat) command
Reference for the Linux nc (netcat) command: test ports, listen for connections, send and receive data over TCP and UDP, transfer files and the main option differences.
nc, or netcat, reads and writes data over network connections. It connects standard input and output to a TCP or UDP socket, so any shell pipeline can talk to a network service, and any program output can be sent to another machine. People call it the "Swiss army knife" of networking because the same few options cover port checks, quick file transfers, tiny chat servers and manual protocol testing.
nc [OPTIONS] HOST PORT # connect nc -l [OPTIONS] PORT # listen
Try it
nc is not installed in our sandbox, so these commands are read-only:
$ nc -zv db.internal 5432 Connection to db.internal (10.0.0.12) 5432 port [tcp/postgresql] succeeded! $ printf 'GET / HTTP/1.0\r\nHost: example.org\r\n\r\n' | nc example.org 80 | head -1 HTTP/1.0 200 OK # receiver # sender $ nc -l 9000 > received.txt $ nc 10.0.0.5 9000 < report.txt
Bash can do a basic port check without nc, by using the built-in /dev/tcp redirection. Here a tiny Python server plays the remote side:
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
nc [-46bCDdhklnrStUuvz] [-I LEN] [-i SECS] [-p SRC_PORT] [-s SRC_ADDR] [-w SECS] [-X PROTO] [-x PROXY] HOST PORT nc -l [-k] [-p PORT] [OPTIONS] [PORT]
| Option | Description |
|---|---|
-l |
Listen for an incoming connection instead of connecting. |
-k |
Keep listening after a client disconnects (not in every version). |
-z |
Zero-I/O mode: just test whether the port accepts a connection (scanning). |
-v |
Verbose; report the result of the connection. |
-u |
Use UDP instead of TCP. |
-n |
Numeric only: do not resolve names. |
-w SECS |
Time out after this many seconds of inactivity or connection waiting. |
-p PORT |
Source port (or the listening port in some versions). |
-s ADDR |
Source address to bind. |
-N |
Shut down the network socket after end of input (OpenBSD version). |
-q SECS |
Wait this long after end of input, then quit (traditional version). |
-e CMD |
Run a program on connect (only in some builds, for good reasons often removed). |
Returns: 0 when the connection succeeded (or data was transferred), 1 on failure. With -z the exit status is the port test result, which makes it good for scripts.
Usage notes
- Several versions exist. The OpenBSD
nc(Debian'snetcat-openbsd), the traditionalnetcat(netcat-traditional), and Nmap'sncatdiffer in options.nc -l 9000works in the OpenBSD version; the traditional one wantsnc -l -p 9000. Checknc -h. - Port test in scripts.
nc -z -w 3 HOST 22returns quickly: 0 for open, non-zero for closed or filtered. - File transfer. The receiver listens and redirects to a file; the sender redirects the file into
nc. There is no encryption or integrity check, so use it only on trusted networks and compare checksums. - Hand-written protocols. Pipe text into
ncto talk HTTP, SMTP or Redis by hand. Use\r\nline endings where the protocol requires them. - UDP has no connection. With
-u,-zcan report "success" even when nothing is listening, because nothing is answered either way. - Security. An open
nclistener (especially with-e) is a backdoor. Do not leave one running. Ports below 1024 need root to listen on. - No
nc? Bash can open sockets with/dev/tcp, andcurloropenssl s_clientcover HTTP and TLS.
Related
/dev/tcp,telnet,nmap,ss,curl- Lessons: Network Troubleshooting, Networking Basics