How to use Netcat – a Netcat tutorial

Posted by

Netcat is an open source utility which allows you to easily pipe data across a network. You can use it to connect and send data to any TCP service, such as web servers, IRC servers, etc. Netcat can run on both Linux and Windows – in this article, I will demonstrate its use in a Linux environment.

The Netcat binary is called nc and can often be found at /bin/nc on a Linux system.

The syntax of the nc command is:

nc <host/ip> <port>

You can pipe data from a file (using cat) or from the echo command into nc. Here is an example of connecting to IRC using Netcat:

echo -e "USER ident 0 * :Gecos\nNICK nicknamehere" | nc irc.geekshed.net 6667

You can kill the connection with ctrl + c.

Netcat can also be used to connect and send data to another instance of Netcat. You can run the nc command on one computer, in daemon mode, and connect to it using nc on another. This method is ideal to send data from one computer to another. Here is an example of copying a file between two computers with Netcat:

# Run this command on the computer you're sending to
nc -l -p 54321 > /path/to/output/file.txt

# Then run this command on the computer you're sending from
cat /path/to/input/file.txt | nc -q 1 recipient.ip.address.here 54321

Obviously there’s better/faster ways to  send a file over a network, but hopefully this gives you an idea of the potential of Netcat. A common use is to use dd with Netcat to image a failing hard drive over a network to a healthy computer:

# Run this command on the computer you're sending to
nc -l -p 54321 | dd of=disk.img

# Then run this command on the computer you're sending from. Change /dev/sda to the disk you want to image
dd if=/dev/sda | nc -q 1 recipient.ip.address.here 54321

You could even change the of of the recipient computer’s dd command to write directly to a hard drive.

Leave a Reply

Your email address will not be published. Required fields are marked *