Linux Commands

Use Netcat to Transfer Files

This tutorial offers an easy explanation of how to use Netcat to transfer files between devices.

Netcat is a command-line network tool used to establish TCP/UDP connections and network analysis. Netcat features include:

  • Outgoing and incoming connections, TCP or UDP, to or from any ports
  • It can be used to open local ports
  • Supports file transference between devices
  • Netcat can be used to scan ports.
  • Netcat can be used for banner grabbing
  • Full DNS forward/reverse checking, with appropriate warnings
  • Ability to use any locally configured network source address
  • Built-in port-scanning capabilities, with randomization
  • Built-in loose source-routing capability
  • Slow-send mode, one line every N seconds
  • Hex dump of transmitted and received data
  • Optional ability to let another program service establish connections
  • Optional telnet-options responder

Installing Netcat:

Before starting, I want to clarify that although I use the command “netcat” in this tutorial, you can also use the command “nc.”

To start, install Netcat by running the command shown below in Debian-based Linux distributions.

sudo apt install netcat

To install Netcat on Red Hat or Centos run:

yum install -y nc

You need to repeat the process in all devices you want to transfer data between. For this tutorial, I created a virtual machine with IP 192.168.1.102.

For this tutorial, I will also use the command pv that defaults in Linux distributions. This command is used to show the progress file transfer progress.

To install it, run:

sudo apt install pv

Sending a file using Netcat:

In this example, the device 192.168.1.102 will receive the file; another device will send it. From the receiving device, run the following command replacing linuxhint.deb with the name of the actual file you want to transfer. The -l (Listening for inbound connections) option instructs Netcat to listen for incoming connections on port 9899.

netcat -l 9899 > [FileName]

As you can see, Netcat stays listening on port 9899, waiting for the file. Now, from the sender device, run the command below, replacing the IP address with the IP of your receiver device and linuxhint.deb with the file name. The option -w is used to define the timeout in seconds.

netcat -w 2 192.168.1.102 9899 < [FileName]

As you can see below, the file linuxhint.deb was transferred to the current directory of the receiving side.

If you don’t have the file to send in the current directory, or the receiver doesn’t want to store it in the current directory, it is possible to define a path.

In the example below, the receiver will store the file linuxhint.deb into the directory linuxhint.

In the example below, the sender has the file he wants to send in the subdirectory linuxhint2:

As you can see, the file was successfully stored in the receiver’s linuxhint directory.

Showing progress in file transfers:

You also can implement the command pv to show the progress in file transfers. On the receiving side, add a pipe followed by the command pv used to monitor the progress of data through a pipe and the inbound file specification.

netcat -l 9899 | pv > LinuxHint

Then on the sender device, run the command explained in previous examples as shown below.

nc -w 2 192.168.1.102 9899 < users.txt

Pv output can be edited to change the file units; check the man page of this command to show the progress in other units than bytes.

Compress and send a directory using Netcat:

Using the commands below, you can compress and send a directory.

On the receiver device, type the command below, replacing linuxhint2 for the name of the compressed directory you want to receive from this device.

netcat -l 9899 > linuxhint2

On the sender device, run the command below, replacing linuxhint2 with the name of the directory you want to compress and send. Also, replace the IP 192.168.1.102 with your receiver’s IP address.

tar cfvz - linuxhint2 | netcat -w 2 192.168.1.102 9899 linuxhint2/

As you can see, the file was received properly and extracted using the command below:

tar xvzf <FileName>

The directory linuxhint2 was extracted with its content.

Transferring a whole disk or partition using Netcat:

You can also transfer a whole disk or partition using Netcat with the commands shown below. In the example below, I will transfer an external disk partition to a receiving side partition.

On the receiving side, type the following command, replacing the port with the one you are using and the destination disk or partition with yours.

netcat 9899 -l | bzip2 -d | dd of=/dev/sdb

On the sending side, run the following command replacing the disk or partition (sdb1), your receiver IP address and port.

bzip2 -c /dev/sdb1 | netcat 192.168.1.102 9899

In my case, my drive device was full, but we can see the procedure finished.

If you mount the device where you stored the backup, you must see the data in the mount point.

mount /dev/sdb /media

Conclusion:

Files transference is one of the best Netcat features.

In the previous tutorial on Netcat for port scan, the conclusion wasn’t favorable for this program before alternatives like Nmap. Among Netcat’s general limitations, we see it doesn’t support scanning multiple ports. File transfers are not encrypted, and an attacker may launch a Man in the MIddle attack to intercept the data in a Netcat file transfer.

It is important to clarify transferring files over Netcat isn’t a safe choice if encryption measures aren’t implemented. Netcat doesn’t include encryption features, but it can be combined with PGP or alternatives approaching this issue like Cryptcat, which is very similar to Netcat with few differences: Cryptcat doesn’t support options -t for Telnet negotiation and does not support stdin timeout (-q). On the other side, Cryptcat adds new functionalities like encryption. Other secure alternatives include file transfers over the ssh protocol (scp).

I hope this tutorial was useful. Keep following Linux Hint for more Linux tips and tutorials.

About the author

David Adams

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.