On this page

  1. Ghosting The Machine

Ghosting The Machine

This is a short but potentially extremely handy guide to ghosting one Linux box to another (or simply making a full backup of a desktop/server). Credit goes to 'topdog' for this.

You might have a small office where you customise one desktop just how you like it and need to roll this out to N other PC's or simply want a backup of a server or desktop to another machine or even to an image file.

The main tool here is netcat which is extremely powerful and has a multitude of other great uses that won't be covered here.

Target Machine:

** Boot to linux rescue mode with networking (CentOS works fine)

Initiate netcat to listen on port 30 - # nc -l -p <portnumber> | dd of=/dev/sda (assuming the hard drive is sda and not hda):

# nc -l -p 30 | dd of=/dev/sda

Source Machine:

Dump the contents of the disk to the target PC - #dd if=/dev/sda | nc <ipaddresstarget> <portnumber>

# dd if=/dev/sda | nc 30

Then to check that traffic is flowing, on the source go to another terminal (ALT/F2) and dump the tcp data on the NIC (assuming it's eth0):

tcpdump -tnli eth0 port 30

If you just want a backup image you could change the above output on the taget to:

# nc -l -p 30 | dd of=mybackup.img

That's it. Naturally the target PC/disk cannot be smaller than the source:) I hope this saves someone a lot of time.

Share this page:

Suggested articles

23 Comment(s)

Add comment


By: Anonymous

pv is a program that graphically displays the progress/status of data going through a pipe. install it and try "pv cat /dev/random > /dev/null"

By: Paul Zarucki

Many thanks for this. So neat and simple!


Even better - thanks;)

By: Mario Kerecki

I prefer Clonezilla for server/desktop backups. It can backup to usb drive, ssh server, nfs server or local disk. One of the best features is it can restore an image to a different size hardrive, bigger or smaller as long as the data fits. It also only copies "used space" whereas the dd command will copy the entire drive including empty space. Clonezilla is very easy to use, similar to the windoze Ghost program but open source and free :) To learn more checkout http://clonezilla.org cheers -m

By: Paul D

If you zero out the unused space (aka  dd if=/dev/zero of=/loc/of/mounted/drive/to/be/cloned/zero; rm zero;) THEN compress the image through gzip, you will essentially be making a clone of just the used space. I've done this to drives that were 500gb with only 30gb being used and the resulting image was 24ish gbs. Works like a charm...



Yes the target is booted from CD. Unless you just want the image file then no CD required of course. The target just needs nc which comes with most distros.


By: Juergen

You could always throw in a pv, as in

dd if=/dev/sda | pv | nc

By: igiron

What does the | pv | stand for? Thanks

By: Anonymous

If one ghost a Debian linux from a server to a harddisk, will this work if the harddisk be simply plug into another server, regardless of configuration and make, since the mother board, CPU, RAM and other components may be diffrent?




Should work. I have ghosted to different hardware fine. Only sometimes there might be xorg issues which can be fixed.

Debian has awesome hardware dtection so should be fine:)



By: Anonymous

There are no guarantees, but I have noticed Linux does a good job of booting on a different machine configurations. What I have seen is that I can usually move a hard drive from one machine to another and Linux boots fine.

With Windows the installation ends up being pretty specific to the machine configuration it is installed on, though I have multiple times done "repair installations" that have allowed me to move a drive to a new PC and keep the old installation (it seems to mostly just update the drivers.)

 So to answer your question, with Linux (Debian) you probably would have good luck with this but I can't say for sure.

By: Anonymous

Why not just use mkcdrec?  Get a backup onto CD/DVD/network, etc. and allows you to do a bare-metal recovery as well?  Don't have to worry about size of disk, or machine.  As long as the machine is in the same "family", works fine.  Much better for roll-outs, IMHO.






By: Temporary Saint

This is pretty slick.  Simple and easy.  Question though - is the target PC's OS booted off the hard disk or a CD?

By: giochi gratis

Ability to transfer files between different filesystem types (as well as different disk/partition sizes, depending purely on available space).

By: Lawrence D'Oliveiro

My tool of choice for bulk backup/restore and transfer of files is rsync. It does file-level backups, not image backups. But Linux is such that you can do a file-level transfer of an OS installation and still have it work. Only people who spend too much time with Windows think that an image backup is necessary for this!

Advantages of file versus image backups:

* Automatic support for all filesystem types that your kernel will support.

* Ability to transfer files between different filesystem types (as well as different disk/partition sizes, depending purely on available space).

* Ability to confirm the copy was successful just by running the command again; in this case, rsync will do an incremental transfer, and if it reports nothing to do, then you know you got everything.

* Fast incremental transfers over a network .

By: hypatia

Your right, but...

You'll have to install grub into the boot sector of the new box, I usually do this with a command like:

dd if=/dev/hda bs=512 count=1 | {netcat}

 Or, you could chroot the the mount point on the new box and run grub-install.

By: Anonymous

Assuming you have an ssh server running, which is more secure than opening a port to anyone:

If the target machine or network is the limiting factor:

dd if=/sourcepath/sourcefilename | gzip | ssh [email protected] "dd of=/targetpath/backup.img.gz"

If the source machine is the limiting factor:

dd if=/sourcepath/sourcefilename | ssh [email protected] "gzip > /targetpath/backup.img.gz"

If you are wanting to use netcat, just break up the commands above and use the netcat commands from the original post.


dd if=/sourcepath/sourcefilename | gzip | nc 30 if the target or network are the limiting factors


nc -l -p 30 | gzip | dd of=mybackup.img.gz

only compress it at one end as compressing something twice is generally little gain over compressing once.

By: Anonymous

How can you compress it on the fly ?

By: Anonymous

What about using clonezilla and drbl?  just do the initial image creation, not the push from the server to the clients.

 or pxe boot into clonezilla live, plug in a usb drive and go from there.


Can nc be used to ghost/backup/image a live server?



Can I use this to make a copy of all OS from one server to another without shutting down/umount the source? Is this safe to do dd if=/dev/sda on a running source server ? I have some critical servers that i need to replicate without downtime.

This is a very very very handy How to!  Thanks :)

By: Anonymous

Can i send the ghost image to another system through the internet?? What if the back up server is not on my LAN??

By: Marc

If the target system has a disk of different size, with bigger partitions for example, you can use: