How to Use Netcat to Quickly Transfer Files Between Linux Computers

There’s no shortage of software solutions that can help you transfer files between computers. However, if you do this very rarely, the typical solutions such as NFS and SFTP (through OpenSSH) might be overkill. Furthermore, these services are permanently open to receiving and handling incoming connections. Configured incorrectly, this might make your device vulnerable to certain attacks.

netcat, the so-called “TCP/IP swiss army knife,” can be used as an ad-hoc solution for transferring files through local networks or the Internet. It’s also useful for transferring data to/from your virtual machines or containers when they don’t include the feature out of the box. You can even use it as a copy-paste mechanism between two devices.

Also read: How to Securely Transfer Files in Linux Using SCP

How to Install netcat on Various Linux Distributions

Most Linux-based operating systems come with this pre-installed. Open a terminal and type:

netcat

netcat-command-not-found

If the command is not found, install the package that contains netcat, a BSD variant. There is also GNU’s version of netcat which contains fewer features. You need netcat on both the computer receiving the file and the one sending it.

On Debian-based distributions such as Ubuntu or Linux Mint, install the utility with:

sudo apt install netcat-openbsd

With openSUSE, follow the instructions on this page, specific to your exact distribution.

On Arch Linux enter the following command:

sudo pacman -S openbsd-netcat

Unfortunately, the RedHat family doesn’t include the BSD or GNU variants of netcat. For some odd reason, they decided to go with nmap-ncat. While similar, some command line options are not available, for example -N. This means you will have to replace a line such as nc -vlN 1234 > nc with nc -vl 1234 > nc so that it works on RedHat/Fedora.

To install ncat on RedHat:

sudo yum install nmap-ncat

And on Fedora:

sudo dnf install nmap-ncat

How to Use netcat to Transfer Files Between Linux Computers

On the computer that will receive the file, find the IP address used on your internal network.

ip route get 8.8.8.8

After “src” you will see the internal network IP address of the device. If, for some reason, results are irrelevant, you can also try:

ip address

netcat-find-ip-address

In the screenshot offered as an example, the IP is 10.11.12.10.

On the same computer, the one that will receive the file, enter this command:

nc -vl 44444 > pick_desired_name_for_received_file

netcat-receiving-file

And on the computer which will send the file, type this, replacing 10.11.12.10 with the IP you discovered earlier:

nc -N 10.11.12.10 44444 < /path/to/file/you/want/to/send

netcat-sending-file

Directory and file paths can be absolute or relative. An absolute path is “/home/user/Pictures/file.png.” But if you already are in “/home/user,” you can use the relative path, “Pictures/file.png,” as seen in the screenshot above.

In the first command two parameters were used: -v and -l. -v makes the output verbose, printing more details, so you can see what is going on. -l makes the utility “listen” on port 44444, essentially opening a communication channel on the receiving device. If you have firewall rules active, make sure they are not blocking the connection.

In the second command, -N makes netcat close when the transfer is done.

Normally, netcat would output in the terminal everything it receives. > creates a redirect for this output. Instead of printing it on the screen, it sends all output to the file specified after >. < works in reverse, taking input from the file specified instead of waiting for input from the keyboard.

If you use the above commands without redirections, e.g., nc -vl 44444 and nc -N 10.11.12.10 44444, you create a rudimentary “chat” between the two devices. If you write something in one terminal and press Enter, it will appear on the other computer. This is how you can copy and paste text from one device to the other. Press Ctrl + D (on the sender) or Ctrl + C (anywhere) to close the connection.

Optimize File Transfers

When you send large files, you can compress them on the fly to speed up the transfer.

On the receiving end enter:

nc -vl 44444 | gunzip > pick_desired_name_for_file

And on the sender, enter the following, replacing 10.11.12.10 with the IP address of your receiving device:

gzip -c /path/to/file/you/want/to/send | nc -N 10.11.12.10 44444

Send and Receive Directories

Obviously, sometimes you may want to send multiple files at once, for example, an entire directory. The following will also compress them before sending through the network.

On the receiving end, use this command:

nc -vl 44444 | tar zxv

netcat-receiving-tar-gzipped-directory

On the sending device, use:

tar czp /path/to/directory/to/send | nc -N 10.11.12.10 44444

netcat-sending-tar-gzipped-directory

Conclusion

Preferably, you would only use this on your local area network. The primary reason is that the network traffic is unencrypted. If you would send this to a server, through the Internet, your data packets could be intercepted along the network path. But if the files you transfer do not contain sensitive data, it’s not a real issue. However, servers usually have SSH preconfigured to accept secure FTP connections, and you can use SFTP instead for file transfers.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Alexandru Andrei

Fell in love with computers when he was four years old. 27 years later, the passion is still burning, fueling constant learning. Spends most of his time in terminal windows and SSH sessions, managing Linux desktops and servers.