In our previous article, we introduced the netcat utility along with a few examples of its use as a port scanner.
In this article, we’ll focus on how we can use netcat to send messages between computers and transfer files.
Example 4: Sending messages between systems.
For this, we will use netcat as in server mode on one computer and in client mode on the other computer.
On our Ubuntu system(hostname: server and IP address is 192.168.87.146), we will tell netcat to listen for incoming traffic on a particular port.
Netcat will operate in server mode on this system.
In the below sample I’ve turned on netcat to listen for incoming traffic on port 7777 on my ubuntu system.
[root@server ~]# nc -l 7777
From our centos system(hostname: client), we will initiate a connection to the centos system on port 7777.
Netcat will operate in client mode here.
[root@client ~]# nc 192.168.87.146 7777
So, now all you have to do is type something on either computer and it will appear on the other computer just as is.
On ubuntu: root@server:~# nc -l -p 7777 hello there how are you
On centos: [root@client ~]# nc 192.168.87.146 7777 hello there how are you
Press ctrl+c on the Server system to instruct netcat to close the connection.
A word of caution: Unfortunately the netcat version that comes available with centos will not allow you to listen to incoming traffic on a source port.
If you’ve already tried it you will receive the following error message:
[root@server ~]# nc -l -p 7777 usage: nc [-46DdhklnrStUuvzC] [-i interval] [-p source_port] [-s source_ip_address] [-T ToS] [-w timeout] [-X proxy_version] [-x proxy_address[:port]] [hostname] [port[s]] [root@server ~]#
The manpage for netcat states that using the -l and -p options together in the same command will result in an error.
Here is the snippet of the -l option from the nc manpage:
-l Used to specify that nc should listen for an incoming connection rather than initiate a link to a remote host. It is an error to use this option in conjunction with the -p, -s, or -z options. Additionally, any timeouts specified with the -w option are ignored.
Example 5: Send files between systems using IO redirection.
In this example, we’ll send a file from our Server system to our client system using IO redirection.
On the Server system use the nc command to listen to a port and redirect the command’s output to a file.
[root@server ~]# nc -l 1234 > rec.txt
The file rec.txt will contain the content of the file sent from the other computer.
Now we’ll create a file on our client system to send via netcat.
root@client:~# echo "I'm sending a file through netcat." >> sent.txt root@client:~# cat sent.txt I'm sending a file through netcat. [root@client ~]# nc 192.168.87.146 1234 < sent.txt
I’ve used input redirection to transfer the file as if we had typed the file content interactively on the keyboard.
Our nc listener on the other end of our Server system will have received the transmitted content and will write it to the output file we specified.
root@server:~# nc -l 1234 > rec.txt root@server:~# root@server:~# cat rec.txt I'm sending a file through netcat.
Example 6: Transfer files using piping instead of IO redirection.
In this example, we’ll use UNIX pipes instead of IO re-direction to transfer files through netcat.
The syntax for receiving the file will remain the same though.
So, on our Ubuntu system, we begin listening to port 4321 and redirect this output to a file.
root@server:~# nc -l 4321 > rpipe.txt
On our centos system we create a file a pipe it out to the netcat command to initiate a connection to the Ubuntu server on port 4321 thereby transmitting or transferring the file content.
[root@client ~]# echo "I'll use this as a pipe.HaHa" >> spipe.txt [root@client ~]# [root@client ~]# cat spipe.txt | nc 192.168.87.146 4321 [root@client ~]#
On our ubuntu server, the file rpipe.txt will now have the content of the file spipe.txt.
root@server:~# cat rpipe.txt I'll use this as a pipe.HaHa
Again, the nc -l part does not work on the netcat version installed with Centos.
Note: Netcat does not recognize >> symbol for redirection while you are transferring the content of a file. This implies that it’s not possible to append to a file while moving it via netcat.
Example 7: Copying an entire directory tree via netcat.
Using tar in conjunction with netcat we’ll be copying all the files in a directory from one computer to another.
On the Ubuntu system, we will run the netcat command in listen mode on a specific port and pipe the output to the tar command.
root@server:~# nc -l -p 2345 | tar -xvf -
The dahs(-) at the end of the tar command tells tar to receive its input from stdin being piped from the netcat command.
On the centos system go the directory whose contents are to be transferred and then execute the following:
[root@client sahil]# tar -cvf - * | nc 192.168.87.146 2345 sahil_file2 sahil_file5 sahil_file7
The dash(-) in this tar command implies that tar will create an archive consisting of all the files in the current directory specified by * and write it to standard out.
We pipe the output of the tar command, i.e., the archive generated by tar to the netcat command which transmits that archive to the Ubuntu server via the connection is established on port 2345.
If we go back to our ubuntu system we will see the following output from the netcat command that we started earlier:
root@server:~# nc -l -p 2345 | tar -xvf - sahil_file2 sahil_file5 sahil_file7 root@server:~#
The archive has been extracted, and files would be available in the directory from where we ran the netcat command.
root@server:~# ls rec.txt rpipe.txt sahil_file2 sahil_file5 sahil_file7 sent.txt root@server:~# pwd /root
In the next article, we’ll see how we can use netcat to grab some information about the destination system.
Latest posts by Sahil Suri (see all)
- Setting up chrooted ssh jails in Linux - October 8, 2019
- How To exclude copying of specific directories in Linux using cp/scp/rsync - October 7, 2019
- Docker container ports explained - September 27, 2019
- Docker Volumes explained - September 25, 2019
- Docker networking commands explained - September 24, 2019