When the time comes to wipe, backup, or restore data on Linux, there are a number of apps you can pick. GParted is one of the most proven options out there. Disks is a more modern alternative that looks great in the GNOME desktop environment. But no matter which Linux operating system you run, there’s one option that always works — dd.
You can use Linux without ever needing to dip into the terminal. But once you add certain commands to your repertoire, not only will you save time, but you will learn skills that you can use across any version of Linux.
Why Use dd?
Whether you’re on a desktop or a server, dd simply works. dd began as a Unix command, so in addition to Linux, it supports other Unix-like operating systems such as FreeBSD and macOS.
dd is also fast. I don’t mean it will make speedy work of whatever drive you’re trying to eradicate. Rather, it takes mere seconds to initiate a wipe. Once you know what to type, you can open a terminal and erase a drive in the time it takes you to type in a search for instructions online.
With great power comes with great responsibility. A certain dd entry makes our list of Linux commands you should never run.
Are you nervous? Good. Even once you’re a dd expert, you always want to type out commands carefully. An absent mind can, at worst, ruin hardware and, more likely, result in data loss. Now, let’s get started.
Cloning a Drive
Cloning a drive is a surefire way to make a backup of your computer. You can back up data to a portable hard drive or Dropbox, but this only saves your files. To save your apps, you can have to learn how to back up each individual one. For some, that means exporting a specific file. For others, that means copying a hidden folder. Then there are those apps that you can’t backup at all.
dd gets around this by creating an exact copy of your entire hard drive or partition. When you restore this copy, you bring your computer back to exactly how it was. You will get back your files, your apps, your browsing history, and even your system settings. Everything.
To clone your drive, you will need a second drive that has more space than the one you’re copying. Likely, this will be a portable hard drive or a large flash drive.
Start by opening the terminal. You will need administrator rights to execute any dd commands. You can type su to sign in as an administrator, or you can type sudo at the beginning of the dd command.
When you’re ready to copy, type the command below. Note, it will erase any pre-existing data on the second drive, so make sure to backup any data beforehand.
dd if=/dev/sdX of=/dev/sdY
Now, let’s make sense of what’s going on. dd is the command. if is the input, as in the location you want to copy. of is the output, or the location you’re replacing with your copy.
sdX and sdY refer to the drives you are interacting with. Drives are often given a name such as /dev/sda, /dev/sdb, or /dev/sdc. You can find out the names using a partition editor. Or, since you’re already in the terminal, you can use the lsblk command.
Creating an Image File
Another way to clone a drive is to create a disk image that you can move around and restore as you would a bootable USB.
Creating image files allows you to save multiple backups to a single destination, such as a large portable hard drive. Again, this process only requires one command:
dd if=/dev/sdX of=path/to/your-backup.img
To save space, you can have dd compress your backup.
dd if=/dev/sdX | gzip -c > path/to/your-backup.img.gz
This command shrinks your backup into an IMG.GZ file, one of the many compression formats Linux can handle.
Restoring a Drive
What good are those backups if you can’t use them? When you’re ready to restore a clone, you have two options. If you used the first approach, simply swap the two destinations.
dd if=/dev/sdY of=/dev/sdX
When restoring from an image file, the same concept applies:
dd if=path/to/your-backup.img of=/dev/sdX
If your image file is compressed, then things get a little different. Use this command instead:
gunzip -c /path/to/your-backup.img.gz | dd of=/dev/sdX
To be clear, gunzip is g unzip, as in the opposite of g zip This command decrompresses your backup. Then dd replaces the existing drive with this image.
Parameters to Consider
You can alter your command by sticking a parameter at the end. By default, dd can take a while to transfer data. You can speed up the process by increasing the block size. Do so by adding bs= at the end.
dd if=/dev/sdX of=/dev/sdY bs=64
This example increases the default block size from 512 bytes to 64 kilobytes.
conv=noerror tells dd to continue despite any errors that occur. The default behavior is to stop, resulting in an incomplete file. Keep in mind that ignoring errors isn’t always safe. The resulting file may be corrupted.
conv=sync adds input blocks with zeroes whenever there are any read errors. This way data offsets remain in sync.
You can combine these last two as conv=noerror,sync if you so desire. There is no space after the comma.
Getting to Know dd
In case you’re interested, dd’s name refers to a statement in IBM’s Job Control Language. If you don’t understand what’s going on there, no sweat. I don’t either. That doesn’t make the command any harder to use.
Need more information that can help you improve your relationship with dd? The wiki page is pretty thorough. There’s also a great write-up on the Arch Linux wiki. Again, it doesn’t matter if you’re using Arch or not. dd works the same way regardless of your Linux operating system.
If it turns out dd isn’t for you, you’re not out of luck. There are other ways to clone a hard drive.
What’s your preferred method for backing up your computer? Are you a fan of dd? Is there an app you would recommend instead? Don’t hesitate to share your experience with fellow readers!
Image Credit: wavebreakmedia, Jane Kelly via Shutterstock.com