I love working with the netcat utility. In this article I will share some simple practical examples of netcat for the linux user.
What is Netcat?
nc, also known as the TCP/IP swiss army knife is a feature rich network utility which can be used to read and write data to network connections using TCP or UDP.
You can learn how to install netcat in your Ubuntu linux machine in one of our previous articles about the netcat utility.
Or open a new terminal (CTRL+ALT+T) and run the following command.
sudo apt-get install netcat
After the netcat installation is finished is time to learn some knowledge that you can easily put in practise.
Use netcat As A Client And Server
The netcat tool can operate in two modes, as a client and as a server. The -l option puts netcat in the listening mode (server).
The following command will create a listener in your local machine. The -p option is used to specify the port where your simple small server will listen on.
nc -l -p 123
The above server is listening for incoming connections on port 123. You can use any port that is free at the moment you are running the listener.
Can we connect to the server? Yes, we can. Open another tab in your terminal and run the following command.
nc localhost 123
The above command is used to put netcat in client mode. We connect to the listener that is running in our local machine on port 123.
Congratulations my friend! You have created your first chat with netcat. Try to type some plain text in one of the tabs, hit Enter and see what is going to happen.
I have created a detailed article on creating simple chat with netcat for you guys. Feel free to read and ask me any question about it.
Scan Ports With netcat
Ok guys, do you like to use netcat as a port scanner? I do!
Port scanning is the act of systematically scanning a computer’s ports.
Open a new terminal (CTRL+ALT+T) and run the following commands. The following command will perform a TCP scan.
Note: We are performing port scan in our local machine.
nc -v -z 127.0.0.1 25
The following output is the result of the above command.
root@baby:/home/oltjano# nc -v -z 127.0.0.1 25
localhost [127.0.0.1] 25 (smtp): Connection refused
The -v option is used to run netcat in verbose mode and the -z option tells netcat to not make a full connection since we are only interested to know the state of the port.
Is the port you are scanning open or closed? Maybe it is filtered.
If you like you can read a very detailed article on port scanning with netcat that I wrote for linoxide reader.
Transfer Files With Netcat
You can also use netcat to transfer your files. Suppose we have a file named test.txt on machine A and we wan to transfer its content on machine B. The machine B will listen for a connection. Run the following command on machine B in order to wait and get the test.txt file.
nc -l -p 123 > test.txt
Then run the following command on machine A.
cat test.txt | nc localhost 123
Note: I have only one machine in my network so A and B are the same. What matters is that this technique works.
Stream A Video With Netcat
This is very useful when you want to watch a long video or movie from your server (another machine in your network) but you don't have time to copy it. Make sure you have mplayer installed in the machine that you want to watch the video. Ubuntu geeks and users can install mplayer with the following command.
sudo apt-get install mplayer
In the server side (the machine where your video is stored) run the following command. It is very similar to the file transfer with netcat, but here we do not copy the file in our local machine, we play it with mplayer.
cat video_name.avi | nc -l 13
Run the following command in the client side and everything should be ok. You will easily understand if this technique worked because a video will start to play.
nc server_ip_address 13 | mplayer -vo x11 -cache 3000
Grab Banners With Netcat
To perform a banner grab with netcat run the following command.
nc -v 127.0.0.1 22
Note: Specify the port based on the service you are looking for. The port 22 is used by ssh.
The following is the output of the above command.
localhost [127.0.0.1] 22 (ssh) open
It works like a charm. This is one of the reasons that I love netcat so much.
Does netcat Support IPV6 Connectivity
Yes it does. You can use the -6 to force netcat to use IPV6.
nc -6 -l 13
nc -6 localhost 13
One article is not enough to cover netcat utility and its usage, but this article is the guide for every new user to netcat. We covered chats, video streaming, file transferring, banner grabbing, port scanning etc. Feel free to suggest your netcat tips in the comments.