nc

Reads and writes data across network connections, using TCP and UDP.

Swiss-army knife tool to debug and monitor network connections, scan for open ports, transfer data, act as a proxy, and more.

Cheatsheet

nc [options] <host> <port>

Command Line Flags

  • -u: Switch to UDP (Default is TCP.)
  • -v: Verbose logging
  • -z: Scan the port without connecting (Zero I/O)
  • -l: Listen for incoming messages on port

Common Use Cases

Scan for open ports

nc -z -v <host> 20-80

Pass messages

First machine:

nc -l 5555

Second machine:

nc <first-machine> 5555

Transfer a file

On the receiving machine, open a listener and pipe incoming output to a file.

nc -l 5555 > file_name

On the sending machine, send the file as input to connecting the receiving machine.

nc <receiving-host> 5555 < file_name

Transfer a directory

Same as above, but use tar to compress the directory.

Receiving machine is set to extract.

nc -l 5555 | tar xzvf -

Sending machine compresses and sends the directory as a single archive.

tar czvf - /path/to/dir | nc <host> 5555