Netcat Basics

Netcat is a utility which allows us to perform a myriad of network related tasks such as data transfer and remote command execution over any TCP or UDP port. This post covers basic usage of the tool.

Why use Netcat?

Netcat is easily used via command line and is particularly handy for use in any scripting task that requires transferring data or listening for requests.  This powerful little utility is often referred to as the "Swiss Army Knife" of networking tools and id arguably the single most useful tool for interacting with systems across a network.

Some people may ask "why bother using netcat when you can use ssh and scp ?".  The answer is that netcat (or nc, which the program is also called) is installed on a lot of linux boxes by default, sshd is not.

Hence it can come in handy if you don't have access or permissions to install sshd, or indeed if you just don't want to install sshd at all.

Getting Started

The netcat uility is called nc, check if you have it installed:

which nc
If you need to install it use:
sudo apt-get install netcat

Basic Chat Server

Lets try it out using a simple hello world test.
On one of your machines e.g. run the following command:

nc -l 2222
This will simply listen on port 2222 for any incoming data.
On another machine run:
nc 2222
Next, type anything at all such e.g. "hello world!" and you'll see it echo'd on the listener's shell.
Any text entered into either of the shells ends up being displayed on the other machine also.

File transfer - from the server side (listener)

Now onto something more useful, we'll transfer a file from one box (the server) to another box (the client). So as soon as the server receives a connection, the file gets transferred.

On the machine where the file exists run the following command:

nc -l 2222 < filename
On the box where you'd like to receive the file, run:
nc 2222 > any_file_name
Note that if you don't point the data to any_filename, the data will just be displayed in the shell at the receiving end. Also, obviously the receiving file any_file_name can be any file name (but is normally the same as the original).

If you wanted to append the contents of filename to an already existing any_filename, you could use this instead:

nc 2222 >> any_file_name
Note the '>>' rather than just a single '>' (the '>>' appends while the > replaces).

File transfer - from the client side

To transfer a file in the opposite direction use:

nc -l 2222 > file_copy
On the client side (sender in this case) use:
cat file_to_send | nc 2222

To keep the listening open for further data, use the the -k option:

nc -lk 2222 >> file
Some people actually use this as a very basic honeypot to catch people snooping on their network.

One thing to bear in mind when using netcat is that no encrytion is used, so sensitive data should not be transferred using netcat.  That's why there is an encrypted version of the utility called cryptcat.
Also, for those of you who were used to the old style netcat there is a version available called nc.traditional (which supports the -e and -c options to execute commands!).

Thanks to Hobbit and Eric Jackson for their work with netcat.