Pin Me

Linux Command Line: dd

written by: jlwallen•edited by: J. F. Amprimoz•updated: 7/4/2011

The dd command has many uses, but one of its most important is for disk cloning. With this command you can make an exact duplicate of your hard drive onto another hard drive (>= original disk size ). In this entry to the Linux Command Line series you will learn the basics of the dd command.

  • slide 1 of 7

    What is dd?

    Disk Definition (dd) came from IBM's Job Control Language and became a staple of the low-level UNIX and Linux commands. What dd does is copy bytes from an input source to an output source. But dd can also copy raw data as well, which makes it perfect for copying boot sectors.

    The dd command can handle many tasks including:

    disk cloning (Note: Target disk must be of equal size or greater than the original disk)

    disk imaging

    partition cloning

    restoring from image file

    The dd command is an incredibly powerful tool, so much so it can, if used improperly, do serious damage to data. To that end dd is often jokingly referred to as Disk Destroyer. So, when using dd, you want to use it with caution. In fact, it's always best to practice using dd on non-production machines.

  • slide 2 of 7

    Basic command structure

    The dd command uses a somewhat outdated command structure, relying on if and of statements. Think of if as input file and of as output file. And remember, Linux devices are seen in the form of /dev/sd* (Where * is the letter associated with a particular device and can often be followed by a single number). So the structure of the command (which must be run as the root user) looks like:

    dd if=/dev/sda of=/dev/sdb

    What the above command would do is clone the data located on /dev/sda to /dev/sdb. The dd command doesn't really care what the contents of the drives are, just the locations.

    Naturally there are arguments aplenty for dd. We'll discuss those, as needed, in the examples below.

  • slide 3 of 7

    Cloning a drive

    To clone a drive you would use the command:

    dd if=/dev/sda of=/dev/sdb

    Where /dev/sda is the location of the device to clone and /dev/sdb is the location of the target device. This command doesn't care how your drive is partitioned, nor the type of data that is on the drive. What this will create is an exact copy of one drive onto another.

  • slide 4 of 7

    Creating an image file of a hard drive

    An image file is a great way to save a compressed image of your hard drive. To do this issue the command:

    dd if=/dev/sda /path/to/disk.img

    Where /dev/sda is the device to clone and /path/to/disk.img is the location where you want the image to be saved. Once the image is complete you can compress the image (to save space) and copy it onto CD for safe keeping.

  • slide 5 of 7

    Cloning a partion of a drive

    What if you only want to clone a single partition of a drive and save it as an image? To do this issue the command:

    dd if=/dev/sda1 /path/to/disk_partition.img

    Where /dev/sda1 is the partition to clone and /path/to/disk_partition.img is the location to save the image file.

  • slide 6 of 7

    Restoring from image

    What if you need to restore a partition (or drive) from a saved image? Simply reverse the if and of statements like so:

    dd if=/disk.img /dev/hda <-- to restore an entire drive (Where /disk.img is the image to restore and /dev/hda is where the image is to be restored to.)

    dd if=disk_partition.img /dev/hda1 <-- to restore a single partition on a drive (Where disk_partition.img is the partition image to restore and /dev/hda1 is the location of the partition to be restored.)

  • slide 7 of 7

    Final Thoughts

    The dd command is as powerful as it is useful. What you have read is only a cursory (as well as most useful) introduction to what dd can do. If you have need to image or clone Linux drives, dd is the tool you need.