Netcat - how to transfer files without scp or ftp

One of my recent customers provided me hardware with custom Linux system. Distribution used on this hardware was very limited there was no developers tools, file transfer applications (like scp, ftp or even tftp) or communication clients like ssh. I had to deploy some firmware files to the system without modifying it. This was i386 machine. Of course I could compile something and add this software using usb stick or other stoarge, but what if I would not have direct access to hardware ? Also for development and testing purposes it would be much easier to use network transfer, then running with usb stick.

When looking for answer I found this. I heard before about netcat, but more in context of debugging then using it as file transfer application. Luckily nc as very small tool is in almost all distributions and it was also available in my small custom distro.

File transfer with netcat

nc by man page is described as TCP/IP swiss army knife , but can be used to transfer files.

What have to be done is setting receiving side ie.:

nc -l -p 2020 > my_file.bin

What tell nc to listen on inbound connection (-l) on port 2020 (-p 2020) and redirect content of incoming packages to my_file.bin.

On sender side we pipe my_file.bin to nc like that:

cat my_file.bin | nc <dest_ip_addr> 2020

Which cause nc to create TCP connection to <dest_ip_addr> on port 2020 and send everything it gets on standard input.

Known flaws

From what I saw sometimes nc doesn’t end at EOF and just hang waiting for next data, which never come. In that case I just break with Ctrl-C on both ends. Then check if all stuff was transferred correctly by verifying MD5 sum on sender and receiver side. In most cases files pass this integrity test.

Piotr Król
Founder of 3mdeb, a passionate advocate for open-source firmware solutions, driven by a belief in transparency, innovation, and trustworthiness. Every day is a new opportunity to embody the company's vision, emphasizing user liberty, simplicity, and privacy. Beyond business, a casual chess and bridge player, finding peace in nature and nourishment in theology, philosophy, and psychology. A person striving to foster a healthy community, grounded in collaboration and shared growth, while nurturing a lifelong curiosity and a desire to deeply understand the world.