Netcat, often referred to as the “Swiss Army Knife” of networking, is a versatile tool for creating network connections and transferring data between systems. In this article, we’ll delve into the world of Netcat and explore how to leverage its capabilities for seamless file transfer between servers. We’ll cover the installation of Netcat, opening firewall ports for communication, and demonstrate how to copy files directly and in compressed mode using Netcat.
Installing Netcat
Netcat is available in most Linux distributions’ default repositories. You can install Netcat using your package manager.
# Debian based systems.
# apt install netcat-traditional
# Rhel based systems.
# dnf install nc
Opening Firewall Ports
If firewalls are enabled, ensure that the necessary ports are open for Netcat communication. Open TCP port 8888 (or any desired port) on the receiving server.
# firewall-cmd --add-port=8888/tcp --permanent
# firewall-cmd --reload
Copying Files Without Compression
On the receiving server, start Netcat in listening mode.
# Exaplanation:
# -v verbose [use twice to be more verbose]
# -l listen mode, for inbound connects
# -w secs timeout for connects and final net reads
# -p port local port number (port numbers can be individual or ranges: lo-hi
[inclusive])
# Recive Command.
# nc -v -l -w 5 -p 8888 > received_file.txt
listening on [any] 8888 ...
connect to [10.1.1.101] from <remote_server_hostname> [remote_server_ip]
On the sending server, transfer the file to the receiving server.
# nc -v -w 5 <receiver_ip> 8888 < file_to_send.txt
Connection to <receiver_ip> 8888 port succeeded!
Copying Files in Compressed Mode
On the sending server, compress the file before sending.
# gzip -c file_to_send.txt | nc -v -w 5 <receiver_ip> 8888
On the receiving server, decompress the received file.
# nc -v -w 5 -l -p 8888 | gzip -d > received_file.txt
Verifying File Integrity
Calculate and compare checksums of the original and received files to ensure integrity.
# md5sum file_to_send.txt
# md5sum received_file.txt
Conclusion
Netcat is a powerful tool for facilitating file transfer between servers with minimal overhead and complexity. By following this guide, you can quickly set up Netcat, open firewall ports, and seamlessly copy files directly or in compressed mode between servers. Whether you’re transferring small files or large datasets, Netcat provides a reliable and efficient solution for your network file transfer needs. Experiment with different options and configurations to optimize your file transfer process and unlock the full potential of Netcat in your network environment.