Linux

Transfer files using netcat

There are multiple use cases of netcat, transfering files between servers is one of those

Before going on to the steps we need to understand that transfering files via netcat is not safe, sending critical data or documents is not recommended as these files are not encrypted on the go and the person sending the files via netcat can become a victim of Man-in-the-Middle attack, use this only where security is not the concern. Instead use scp or rsync.

So let’s start with the steps

Before starting please make sure netcat is installed on both the system

Also Read: How to install netcat

First let’s understand the syntax of netcat on both the Sending and Receiving end

On the receiving end, run the below command to start netcat in listening mode and save the data to the mentioned filename

nc -l <port> > <filename>

On the sending end, run the following command to connect to the listening netcat instance on the receiver and transfer the contents of the file to it.

nc <host> <port> < <filename>

<port> | This is the port number on which netcat will listen

<filename> | This is the name of the file to be transferred

<host> | is the hostname/IP address of the receiving node

For example, to transfer a file named “xyz.txt” from the sending host to the receiving host with IP address 192.168.2.6 on port 77999, you can run the following commands:

On the receiving end:

nc -l 77999 > xyz.txt

On the sending end:

nc 192.168.2.6 77999 < xyz.txt

This will transfer the file “abc.txt” from the sending host to the receiving host over the network using netcat.

Leave a Reply

Your email address will not be published. Required fields are marked *