Copy hard disk or partition image to another system using a network and netcat (nc)

by Vivek Gite · 23 comments

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
Output:

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
=============================================================================
Installing:
 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
Complete!

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 // 192.168.1.1
------------------------
           sda
        NETWORK
           sdb
------------------------
HostB // 192.168.1.2
-------------------------

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
Where,

  • -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 192.168.1.1 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
OR
# nc 192.168.1.1 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 192.168.1.1 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.

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 23 comments… read them below or add one }

1 miljan 08.12.07 at 11:44 am

Netcat man page says that -l can not be used with -p switch.

-p source_port
Specifies the source port nc should use, subject to privilege restrictions and availability. It is an error to
use this option in conjunction with the -l option.

2 vivek 08.12.07 at 12:41 pm

miljan,

I don’t have any problem using -p 2222 with -l option. My netcat version is 1.10-32 and I don’t see any such message in man page. Can you specify your netcat version? The post has been updated for latest version. Thanks for quick feedback.

3 miljan 08.12.07 at 1:25 pm

This is strange because it seems to be the same version as from your post.

[root@r2d2 ~]# nc -p 2222 -l
usage: nc [-46DdhklnrStUuvzC] [-i interval] [-p source_port]
[-s source_ip_address] [-T ToS] [-w timeout] [-X proxy_version]
[-x proxy_address[:port]] [hostname] [port[s]]

[root@r2d2 ~]# yum list installed nc
Installed Packages
nc.i386 1.84-10.fc6 installed

But anyway, in case someone else has the same problem, -l is enough for netcat to start listening on a desired port.

4 lefty.crupps 08.13.07 at 12:51 pm

try putting the port at the end, like this:

nc -l -v -p 2222

5 Anonymous 08.13.07 at 1:52 pm

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

6 Karl O. Pinc 08.13.07 at 2:52 pm

You forgot to mention _why_ someone would want to use netcat to do these things, instead of just plain old ssh which is easier because it can be done in one command from one system. You’d want it when you don’t want the overhead of ssh, and don’t need encryption. (It’s also good to have articles like this because netcat is so useful and if you’ve another problem it’s nice to start with something that you know works.)

7 foobar 08.13.07 at 3:41 pm

this is basically copying a compressed partition or disk over the network, unencrypted. you may consider using ssh for safety reasons, e.g:

foo:# cat /dev/sda | bzip | ssh remotehost “cat > /backup/foo.sda.bz2″

8 Anuarag 08.31.07 at 4:08 am

Thanks for this guide.

I am confused!! on the host A (i.e. source disk host) command to be typed is:
# bzip2 -c /dev/sda | netcat hostA 2222

Should it be hostA or hostB.

Similarly I am confused about the commands given for the new version of nc: It seems to me commands for host A and B exchanged?

Please correct me.

Thanks
Anurag

9 g4b 01.28.08 at 3:01 pm

-l also takes a host as option, so if you really want to be sure, no one else just streams in your data you can add like
nc -l HostSource -p 2222…

also maybe update the whole thing to use “HostSource” and “HostDest” because HostA and HostB is a bit confusing maybe.

thanx for the tutorial, g4b

10 The Cog 02.05.08 at 8:46 am

Useful hint, thanks.
There is some confusion betwen host A and B though. All the examples are copying from B to A, but the headings: “Command to type on host…” indicate you are copying from A to B.
Nit-picking, I know.

11 Ben 03.28.08 at 2:08 pm

I don’t see any good about using ssh in local network and it would produce extra traffic. Here’s my contrib to backup and restore a linux parition:

# BACKUP – to backupserver (192.168.1.1)
nc -p 2222 -l | bzip2 -c > Image.bz2
# BACKUP – from clienthost (192.168.1.2)
dd if=/dev/sda bs=16M | nc 192.168.1.1 2222

# RESTORE – to clienthost (192.168.1.2)
nc -p 2222 -l > /dev/sda
# RESTORE – from backupserver (192.168.1.1)
dd if=Image.bz2 bs=16M | bunzip2 -c | nc 192.168.1.2 2222

12 Brian Trauger 06.25.08 at 4:51 am

This looks very helpful. Can I also specify just 1 partition instead of the whole drive? I have a dual-boot with Xp and want to just backup the Windows partition.. Or should I just use partimage?

13 chains 08.13.08 at 4:10 pm

I tried what Ben suggested:

# BACKUP – to backupserver (192.168.1.1)
nc -p 2222 -l | bzip2 -c > Image.bz2
# BACKUP – from clienthost (192.168.1.2)
dd if=/dev/sda bs=16M | nc 192.168.1.1 2222

# RESTORE – to clienthost (192.168.1.2)
nc -p 2222 -l > /dev/sda
# RESTORE – from backupserver (192.168.1.1)
dd if=Image.bz2 bs=16M | bunzip2 -c | nc 192.168.1.2 2222

but when restoring i’m getting the error:
“bunzip2: (stdin) is not a bzip2 file.”

Any suggestions?

14 S 08.23.08 at 2:10 am

in response to #11(ben);

why would you first netcat the data uncompressed and only then compress it into an image?

15 Hannes 09.04.08 at 8:30 pm

Thanks a lot for this article.
But like Anuarag said, “hostA” should be “hostB” here:

(on hostA, type) # bzip2 -c /dev/sda | netcat hostA 2222

We connect not to ourself, yes? :)

16 Florian 01.06.09 at 1:42 am

The command works fine, but if you use netcat to make a diskdump of two directly connected machines, you should avoid the compression, because it will slow down everything.

Works fine for the following scenario:
Host A Host B (with twisted pair)

17 rickx 03.19.09 at 7:37 am

A bit messy tackling errors in the instructions, but doing
sudo su
nc -l -p 2222 | dd of=/dev/sda bs=16M
on the receiver (192.168.0.2) and
sudo su
dd if=/dev/sda bs=16M | nc 192.168.0.2 2222
on the source (192.168.0.1) works.
Use
ifconfig eth0 192.168.0.1[or 2] netmask 255.255.255.0
to configure the nics from the shell on the fly.
Problem is grub then: the entries with GUIDs will not work, so edit the menu.list before rebooting or you’ll get error 2.
Thanks anyway.

18 casparoo 05.01.09 at 5:38 am

How can I copy a hard disk image of a Windows machine to a Linux machine through network ( using netcat and dd). Could you please provide the details. Thanks in advance…

19 Me 05.25.09 at 5:32 pm

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

20 Paul Johnson 07.14.09 at 8:33 pm

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.

21 richard 01.24.10 at 8:45 am

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

22 Hari 01.28.10 at 2:32 am

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).

23 Hari 01.28.10 at 2:48 am

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

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post:

Next post: