netcat utility (nc command) considered as TCP/IP swiss army knife. It reads and writes data across network connections, using TCP or UDP protocol. It is designed to be a reliable “back-end” tool that can be used directly or easily driven by other programs and scripts. At the same time, it is a feature-rich network debugging and exploration tool, since it can create almost any kind of connection you would need and has several interesting built-in capabilities.


I also install the netcat package for administering a network and you’d like to use its debugging and network exploration capabilities.

One my favorite usage is to migrating data between two server hard drives using netcat over a network. It is very easy to copy complete drive image from one server to another.

You can also use ssh for the same purpose, but encryption adds its own overheads. This is tried and trusted method (hat tip to karl) .

Make sure you have backup of all important data.

Install netcat

It is possible that nc may not be installed by default under Redhat / CentOS / Debian Linux.

Insall nc under Redhat / CentOS / Fedora Linux

Use yum command as follows:
# yum install nc

Loading "installonlyn" plugin
Loading "rhnplugin" plugin
Setting up Install Process
Setting up repositories
rhel-x86_64-server-vt-5   100% |=========================| 1.2 kB    00:00
rhel-x86_64-server-5      100% |=========================| 1.2 kB    00:00
Reading repository metadata in from local files
Parsing package install arguments
Resolving Dependencies
--> Populating transaction set with selected packages. Please wait.
---> Downloading header for nc to pack into transaction set.
nc-1.84-10.fc6.x86_64.rpm 100% |=========================| 6.9 kB    00:00
---> Package nc.x86_64 0:1.84-10.fc6 set to be updated
--> Running transaction check

Dependencies Resolved

 Package                 Arch       Version          Repository        Size
 nc                      x86_64     1.84-10.fc6      rhel-x86_64-server-5   56 k

Transaction Summary
Install      1 Package(s)
Update       0 Package(s)
Remove       0 Package(s)

Total download size: 56 k
Is this ok [y/N]: y
Downloading Packages:
(1/1): nc-1.84-10.fc6.x86 100% |=========================|  56 kB    00:00
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing: nc                           ######################### [1/1]

Installed: nc.x86_64 0:1.84-10.fc6

Debian / Ubuntu Linux netcat installation

Simply use apt-get command:
$ sudo apt-get install netcat

WARNING! These examples may result into data loss, ensure there are good backups before doing this, as using command wrong way can be dangerous.

How do I use netcat to copy hard disk image?

Our sample setup

HostA //
HostB //

Your task is copy HostA /dev/sda to HostB’s /dev/sdb using netcat command. First login as root user

Command to type on hostB (receiving end ~ write image mode)

You need to open port on hostB using netcat, enter :
# netcat -p 2222 -l |bzip2 -d | dd of=/dev/sdb

  • -p 2222 : Specifies the source port nc should use, subject to privilege restrictions and availability. Make sure port 2222 is not used by another process.
  • -l : Used to specify that nc should listen for an incoming connection rather than initiate a connection to a remote host.
  • bzip2 -d : Compresses image using the Burrows-Wheeler block sorting text compression algorithm, and Huffman coding. This will speed up network transfer ( -d : force decompression mode)
  • dd of=/dev/sda : /dev/sda is your hard disk. You can also specify partition such as /dev/sda1

Command to type on hostA (send data over a network ~ read image mode)

Now all you have to do is start copying image. Again login as root and enter:
# bzip2 -c /dev/sda | netcat hostA 2222
OR use IP address:
# bzip2 -c /dev/sda | netcat 2222

This process takes its own time.

A note about latest netcat version 1.84-10 and above

If you are using latest nc / netcat version above syntax will generate an error. It is an error to use -l option in conjunction with the -p, -s, or -z options. Additionally, any timeouts specified with the -w option are ignored. So use nc command as follows.

On hostA, enter:
# nc -l 2222 > /dev/sdb
On hostB, enter:
# nc hostA 2222< /dev/sda
# nc 2222< /dev/sda

Using a second machine (hostB), connect to the listening nc process at 2222 (hostA), feeding it the file (/dev/sda)which is to be transferred. You can use bzip2 as follows.
On hostA, enter:
# nc -l 2222 | bzip2 -d > /dev/sdb
On hostB, enter:
# bzip2 -c /dev/sda | nc 2222

Further readings

How do I improve performance?

As suggested by anonymous user:

You should definitely use bs=16M or something like that. Otherwise, the copy will take forever. Copying a 300 GB hard drive over a 1 Gbps cross-over cable took about 1 1/2 hours or so using bs=16M Without this option, the same thing would have taken about 7 hours.

In short use command as follows:
# netcat -p 2222 -l |bzip2 -d | dd of=/dev/sdb bs=16M

Updated for accuracy.

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

34 comments… add one
  • Me May 25, 2009 @ 17:32

    Just skipping the bzip2, is MUCH faster on slower systems..

  • Paul Johnson Jul 14, 2009 @ 20:33

    I have new version of netcat. I wonder if there is a way to use something like the bs option with this new style of command in order to speed it up. Adapting your instruction to my case, here is what I do:

    On the TARGET machine, 129.237.61.XX, make sure port 12345 is unfirewalled. Then set netcat to grab the incoming on 12345 and divert it to disk /dev/sdb. Can also replace with particular partition /dev/sdb2:

    nc -l 12345 | bzip2 -d > /dev/sdb

    After that, go to the SOURCE machine and compress the source disk /dev/sda and pipe it to through nc over to the other system:

    bzip2 -c /dev/sda | nc 129.237.61.XX 12345

    That does work, but not speedy.

    Also, in your instructions, I wonder if you ought to add some information on whether this should be done on a “live” (mounted) file system. From what I can tell, one ought to do this only after booting from a rescue disk or external drive or some sort.

  • Marco Jul 16, 2009 @ 17:02

    This does not work! I tried it using the exact directions given, and it just produces two unbootable drives! This was a useless post, I know this can be done because I know of people who have done it, but this dislexic post is obiously not the correct way to do it.

  • richard Jan 24, 2010 @ 8:45

    An excellent HOWTO. Thank you. Take heed of ‘me’ advice. bzip2 will cause a cpu/memory bottleneck on old machines and under utilise the network. I also found using pv to add a progress meter was useful:
    Target host (HOST B):
    nc -p 2222 -l -vv | pv -r | dd of=/dev/sda bs=16M

    Source host (HOST A):
    dd if=/dev/sda bs=16M | nc 2222

  • Hari Jan 28, 2010 @ 2:32

    Thanks for putting this together. This is a good alternative to have in case ssh option doesn’t workout for some reason. This should also be better than ssh when the hostB is a very lowend device (like iphone).

  • Hari Jan 28, 2010 @ 2:48

    I meant to say “hostA” (ie., the source device is a lowend device like iphone)

  • Ksh Feb 27, 2010 @ 21:41

    Why not gzip? In my experience bzip2 take 3-4 times more cpu cycles than “gzip -3” ;on the other side compression factor could be only 10-20% better.

  • Salah May 15, 2010 @ 13:25

    I see that everyone is recommending dd/nc but I wonder if there’s another solution that provide workarounds for network problems (unstable connections => disconnect/resume, bandwidth limitation => schedule,.. etc). While I’m not if it’s possible to use dd/nc that way.

    I don’t mind spending days copying a harddisk from a remote location to another if that will go automatically and error free. Anyone?

  • David Bain May 23, 2010 @ 10:52

    Hmmmm.. when I follow these instructions everything works except it appears to “hang” when no more data is left to pass from the source machine. I added pv to the command “chain”, as noted above, so I pretty much know when no more data is being passed. Isn’t it supposed to close the connection once all the bytes have been moved?

  • Scott Jul 2, 2011 @ 13:32

    Great site and follow through by everybody. I wonder about the output destination partition/file. Is the output partition created before the
    dd of=/dev/sdb
    if so how is this done?
    Can you instead just send this to a file on the receiving end?
    dd of=/tftp/HOSTA.img

    So far the discussion is about device or hard drive NOT a partition.
    If one wants to make a bootable image of the source partition wouldn’t you copy the?
    if=/dev/sda1 (if that is it).

    Thanks everybody.

    • Lennart Jun 4, 2014 @ 1:48

      Scott, forgot to answer the HOW

      On the receiving end:
      nc -l 2222 > /home/my_account/myfile.img

      On the sending end:
      nc my_receiving_hostname 2222 < /dev/sda

  • Roger Casta Apr 5, 2013 @ 21:00

    Thanks for this nice guide. I just want to mention a little problem I faced.

    Have tried to clone a 60 GB partition over a 100 Mbps network and only acheived a throughput of 5 Mbps on average. I realized the problem was using compression with bzip2. Without compression i’m getting 70 Mbps on average.

    Maybe compression is only usefull on very slow networks.

    Note: I’m clonning partitions betwen two HP Mini laptops attached directly with an ethernet cable (no switch in between them).

  • angel Mar 27, 2014 @ 22:28

    stupid question, after clone my driver I can start my operative system in the image normally or I need do an extra step??…thank you!

    • Lennart Jun 4, 2014 @ 1:39

      You can, however depending on your O/S you might want to change some things to avoid conflicts. On Ubuntu/Debian Linux for example, you might want to mount your root and delete /etc/udev/rules.d/70-persistent-net.rules

      This file contains the autodetected MAC addresses for the network adapters, and after cloning they will be wrong. When you delete it and restart it will be regenerated automatically by udev with the correct MAC addresses.

      You might also want to set a different hostname. To generate a random one you could do this:

      cat /dev/urandom | tr -dc ‘a-zA-Z0-9’ | fold -w 8 | head -n 1 > /etc/hostname

      Other than this everything should be dandy.

Leave a Reply

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

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.