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.
- A Linux server on the Atlantic.Net Cloud Platform
- A root password configured on your server
For the purposes of this tutorial, we will use the following setup:
- Sender Host IP: 126.96.36.199
- Receiver Host IP: 188.8.131.52
Step 1 – Create Atlantic.Net Cloud Server
First, log in to your Atlantic.Net Cloud Server. Create a new server, choosing Ubuntu, CentOS, or Rocky Linux as the operating system with at least 2GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.
Once you are logged in to your server, run the following command to update your base system with the latest available packages.
apt-get update -y
dnf update -y
Step 2 – 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 3 – 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 184.108.40.206 8585 < /root/file.txt
Step 4 – 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 220.127.116.11 8585 | pv | tar -zxf -
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.