Netcat, transfer files quickly between computers

about netcat

In the next article we are going to take a look at Netcat. This is one network tool that allows through the terminal, using a simple syntax, to open TCP / UDP ports in a HOST, associate a shell to a specific port and force UDP / TCP connections.

Some call this tool the TCP / IP Swiss Army Knife. Can be used as a ad hoc solution to transfer files over local networks or from the internet, the latter with caution. It is also useful for transferring data between virtual machines or containers, etc.

This tool it may only be advisable to use it within a local area network. If you send data with this tool to a server over the Internet, the packets could be intercepted along the route. Files will be sent without added security. But if the transferred data does not contain sensitive data, it really would not be a serious problem.

Install Netcat on Ubuntu

Most Gnu / Linux based operating systems come with this pre-installed tool. To check if you have Netcat installed on your computer, open a terminal (Ctrl + Alt + T) and type:

Netcat installed on Ubuntu

netcat

If the command is not found, you can install this tool using command:

sudo apt install netcat

It will be necessary install netcat both on the computer that receives the files and the one that sends them.

Use netcat to transfer files between computers

On the computer that will receive the files, you must look up the IP address used. In this example it will be the local reed. For this you can use:

destination ip for netcat

ip route get 8.8.8.8

Or you can also use:

ip a

In the previous screenshot you can see that the IP of the receiver in this case will be 192.168.0.103. There to clarify that Before writing the command of the sender, the relevant command will have to be written in the receiver.

On the computer where the file will be received, type this command:

file received with netcat

nc -vl 44444 > nombre_del_archivo_recibido

In the above command two parameters are used: -v and -l. The first makes the output detailed so that you can see what is going to happen. As for -l, it makes the tool "I heard"On port 44444. The command basically what it does is open a communication channel on the receiving device. If you have a firewall configured, you have to make sure that its rules are not blocking the connection.

Normally, netcat will display everything it receives in the terminal. After> create a redirect. Instead of printing it to the screen, it sends all the output to the file specified after>. This will create a file with the given name.

On the computer that will send the file, you will have to write, replacing 192.168.0.103 with the IP of your computer what will you receive what was sent, the following:

file sent with netcat

nc -N 192.168.0.103 44444 < /ruta/al/archivo/para/enviar/

In this command, -N causes netcat to close when the transfer is complete. Directory and file paths can be absolute or relative.

chat with netcat

In case of use the commands shown without redirects, it will create a 'chat' somewhat basic between the two devices. If you type something on one terminal and press Enter, it will appear on the other computer. This is an easy way to copy and paste text from one device to another.

This connection can be closed by pressing Ctrl + C in either of the two teams involved.

Send compressed files on the go

If you would like to send large files, this tool allows you to compress them on the fly to speed up the transfer. In the receiver you will have to write:

Netcat receiver compressed file

nc -vl 44444 | gunzip > nombre_del_archivo_recibido

On the issuer part, replacing 192.168.0.103 with the IP address of your receiving computer, you will have to write the following:

gzip -c /ruta/del/archivo/a/eviar | nc -N 192.168.0.103 44444

Send and receive directories

Something that you may also need to do at some point is send all files from the same directory at the same time. The following option will also compress what is sent over the network.

At the receiving end, we will use the following command:

directory received with netcat

nc -vl 44444 | tar zxv

For this case, on the sending device, we will have to use the following command:

directory sent with netcat

tar czp ruta/al/directorio/para/enviar | nc -N 192.168.0.103 44444

Help

If you need learn more about netcat, you can use the help:

Netcat help

nc -h

Today, Ubuntu users can find many software solutions that can be helpful when transferring files between computers. This is a good option when it comes to one-off transfers between teams from our local network.


Leave a Comment

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

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.