Using netcat and tar to quickly transfer files between machines

So you have lots of data to transfer between two machines over ethernet. A nice quick and dirty method is to use netcat and tar. This is by no means secure, but if you haven’t got the time or desire to setup NFS, FTP, Samba, or wait hours for scp to do its job then this can save you a lot of time.

Linux System using tar and netcat

On the receiving end do:
# netcat -l -p 7000 | tar x

And on the sending end do:
# tar cf - * | netcat otherhost 7000

Notes:

Depending on your hardware you might want to use compression with tar. You’ll probably find your hard disk drive is a bottleneck on a fast network. Compression is useless if you can’t saturate your network and will probably just slow things down. tar cf – * is copy everything in the current working directory. And your files will be untared at the other end from where you started the listening netcat. It won’t be obivous that the operation is finished so you’ll need to check network activity or top. This is indeed quick and dirty.

OpenBSD using tar and nc

OpenBSD usually offers a much tighter system rather than the mess you have with Linux. OpenBSD has its own verison of netcat just refered as nc. The tar command is less sloppy than what you find in Linux. So if you’re using two OpenBSD machines then one would do the following.

On the receiving end do:
# nc -l 7000 | tar -xpf -

And on the sending end do:
# tar -cf - * | nc otherhost 7000

It appears that the OpenBSD rewrite of netcat is now in Debian Sid. The package is netcat-openbsd.

Moving LVM images between machines without ssh

On the receiving end do:
# nc -l 7000 | dd of=/dev/mapper/vgfoo-lvbar bs=32768

And on the sending end do:
# dd if=/dev/mapper/vgbaz-lvquz bs=32768 | nc otherhost 7000

Source: http://toast.djw.org.uk/tarpipe.html

Bookmark the permalink.

Leave a Reply

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