1. Introduction
Transferring files between systems is a common task in the world of networking and system administration. In this article, we’ll explore how to transfer files with the netcat (or nc) command in Linux. The simplicity and power of netcat offer a valuable solution for sharing files seamlessly across systems.
First, we’ll begin by setting up the transfer scenario, involving a sender and a receiver system. Then, we’ll learn the necessary commands for both sides and outline the details of how the transfer works. Lastly, we’ll ensure that the file arrives intact at the receiver end without tampering.
Without any further ado, let’s get into the nitty-gritty of it.
2. netcat
netcat is a networking utility that performs various tasks, including port scanning, banner grabbing, and file transfers. Its simplicity and flexibility make it a favorite among administrators for various networking operations. One of the key features of netcat is its ability to create client-server connections and forward data between them.
3. Setting Up the File Transfer
First, look at the netcat command’s installation status on the Linux CLI. We can use the which command to verify the installation of netcat:
receiver# which nc
receiver#
If it’s not there, we can employ the apt command for the installation of the netcat command on the system:
receiver# sudo apt install nc -y
Then, let’s use the which command again to identify the path of the installation:
receiver# which nc
/usr/bin/nc
receiver#
Here, the netcat command is installed in the /usr/bin/nc path.
4. File Transfer Process
Now that the system is ready, let’s initiate the file transfer. Obviously, the sender will utilize the netcat command to initiate the transfer, while the receiver will accept and save the incoming file.
To begin with, we’ll log in to the sender’s server and navigate to the directory containing the file that needs to be transferred.
For this, we first need to ensure the file is available on the appropriate path for transfer. In this case, we’re using the sample_file_transfer.iso from the current path:
sender# nc -w 2 172.31.200.52 9899 < sample_file_transfer.iso
#
The < operator is used to send the data from the sender’s netcat session over port 9899.
The ls command displays the list of files in the current path:
sender# ls
sample_file_transfer.iso
sender#
Now, in order to receive the file, we’ll use the netcat command with the IP address of the receiver where we want to send the desired file.
Further, let’s open the port for the file transfer on the receiver’s server using the netcat command. In addition, we’ll use the -l flag in the receiver’s command to indicate that netcat should be receptive to the incoming connections:
receiver# nc -l 9899 > sample_file_transfer.iso
...
... Intentionally left the space ...
...
Likewise, the > operator is used to receive the data from the sender’s netcat session over port 9899.
Now, we’ve successfully transferred the sample_file_transfer.iso file from the sender to the receiver’s system in the mentioned path.
5. File Data Verification
Now, the file transfer is complete. The next step is to verify whether the transfer was free of errors. We do this by checking the md5 checksums of the original file on the sender’s system and the received file.
Let’s use the md5sum command to get the hash value of the original file from the sender’s server:
sender# md5sum sample_file_transfer.iso
c6779ec2960296ed9a04f08d67f64422
Subsequently, let’s obtain the hash value of the copied file from the receiver’s server:
receiver# md5sum sample_file_transfer.iso
c6779ec2960296ed9a04f08d67f64422
As we see, the hash values from the sender and receiver match, which assures that the transfer was accurate.
6. Conclusion
In summary, we learned about the process of transferring files between Linux systems using the netcat command. Next, we saw how to set up a sender and receiver, with the sender using netcat to initiate the process and the receiver tending to the incoming data.
Lastly, we also covered the importance of verifying the success of the transfer and the method to confirm the accuracy of the received file.