dd – How to copy a drive in Linux from the command line
What Could Go Wrong
As always, a lot could go wrong, but the biggest thing is not having the correct input or output file specified. Nothing stops you from using dd to overwrite your current working harddrive. Make sure that you have the right devices specified.
What is dd
dd is a useful command line utility for ghosting or copying devices, including partitions, entire hard drives, or making backup copies of a cd or dvd. When you use dd, you basically create an identical image, you aren’t copying individual files. Therefore, if you original drive is 1TB, your copy will be 1TB, even if only 30GB used. The nice thing is that dd doesn’t care what the source file is, how it is formatted, or what information it contains.
Some uses for dd:
- Creating a “ghost” image of a hard drive partition – i.e. ghosting the c:\ partition to make a whole backup of your Windows drive
- Making a backup copy of your favorite Grateful Dead cd
- Copying an entire hard drive before replacing it
Note: dd will output a file that contains all of the data contained in your original file. Below is way I normally use it.
dd if=/dev/sda of=/tmp/sda_image.img conv=noerror,sync
So, what does all of this mean:
dd: This is the name of the command, always start here
if: This is the name of your input file. Typically this is the name of a device. In this case, I am creating an image of the entire primary sata harddrive (available on the system as /dev/sda). Other alternatives:
- /dev/sda1 (The first partition on sda)
- /dev/sr0 (Typically refers to the first CD / DVD sata drive)
of: This is the name of the output file. This can be any file with any extension, although I normally stick to .img for disk drives and .iso for cds and dvds
conv=noerror,sync: These are additional command line options. Refer to the man page for full details, but generally this allows the image to remain synced along the proper boundaries and continues even if errors are encountered
Other things to consider
You might also consider adding
which will copy 1KB at a time, as opposed to bit by bit. In many cases this can speed up the duplication significantly.
How to restore from the image
To restore from the previously created image, just do the following:
dd of=/dev/sda if=/tmp/sda_image.img conv=noerror,sync
Note: the only difference between these commands are the flipping of the “if” and “of”