Lots of software can help to transfer files between multiple computers including FTP, NFS, Samba, and more. However, configuring those services incorrectly might make your server vulnerable to attacks. If this is a concern, you can use Netcat to transfer files between multiple devices securely.
Netcat, also known as a “TCP/IP swiss army knife,” is a command-line network tool that allows you to transfer files through local networks or the Internet. It provides an easier way to transfer data without the need for additional services.
In this guide, we will show you how to use Netcat to transfer files on Linux.
Prerequisites
- A Linux server
- A root password configured on your server
For the purposes of this tutorial, we will use the following setup:
- Sender Host IP: 69.87.221.67
- Receiver Host IP: 45.58.46.205
Step 1 – Install Netcat
By default, Netcat is not available in Linux, so you will need to install it first.
For Ubuntu and Debian based distributions, install Netcat using the following command:
apt-get install netcat-openbsd pv -y
For CentOS and RHEL based distributions, install Netcat using the following command:
dnf install nmap-ncat pv -y
Step 2 – Transfer Files Between Two Linux Machine
First, create a sample file.txt on the sender host.
echo "Transferring file using Netcat" > file.txt
Next, you will need to run the nc command on the receiver host from where you want to receive the file.
nc -l -p 8585 > file.txt
This command will instruct nc to listen for incoming requests on port 8585 until it receives a request. Once the request is received it will receive the file.txt file.
Next, go to the sender host, run the following command to send the file.txt file to the receiver host:
nc -N 45.58.46.205 8585 < /root/file.txt
Step 3 – Transfer and Compress Files Between Two Linux Machine
In this section, we will show you how to transfer and compress the larger file using Netcat.
In this example, we will transfer the ubuntu.iso file from one machine to another.
On the sender machine from where you want to transfer the ubuntu.iso file, run the following command:
tar -zcf - ubuntu.iso | pv | nc -l -p 8585 -q 25
In the above command, tar is used to compress the file, and the pv command is used to monitor the progress of the file.
Next, go to the receiver machine and run the following command to receive the ubuntu.iso file:
nc 69.87.221.67 8585 | pv | tar -zxf -
Conclusion
Congratulations! You have successfully transferred files between two computers using the Netcat command. You can now use Netcat to transfer files over a secure network. Try it out on your virtual private server from Atlantic.Net.