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.

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

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:

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

On Arch Linux enter the following command:

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:

And on Fedora:

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

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

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:

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:

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.

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

On the receiving end enter:

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

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:

netcat-receiving-tar-gzipped-directory

On the sending device, use:

netcat-sending-tar-gzipped-directory

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.

2 comments

  1. I find sneaker-net much more efficacious. It may not be faster or geekier than netcat but it is much safer. Very little chance of the data packets being intercepted unless, of course, you get held up for your USB stick. :-)

Leave a Comment

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.

Sponsored Stories