Linux dd Command Tutorial

The dd command is a very useful command used to copy data from one source into a destination. The dd command created in the Unix system in order to copy and convert files and also used in Linux too. The most powerful feature of the dd command is the ability to copy data as raw.

dd Command Syntax

The dd command has the following syntax where the if and of very important.

dd OPTION if=DATA of=DATA
  • if=DATA is a file or device which is used for input. The DATA specifies the device or file path.
  • of=DATA is a file or device which is used for output. The DATA specifies the device or file path.
  • OPTION is used to set diferent options like bytesize, count etc for the copy operation.

Create Disk/Block Device Image

Let’s start with a simple and most common use case for the dd command. The dd command can be used to create a disk, partition, or block device image as a file. This can be useful for backup. For example, the /dev/sda disk can be copied into a file and later restored without any problem. In the following example, we will copy the /dev/sda disk into a file named backup.img.

dd if=/dev/sda of=/mn/backup.img

Copy or Backup MBR (Master Boot Record)

MBR (Master Boot Record) contains critical information about the system boot. The MBR is located first 512 bytes of the boot disk which is generally the first disk in the system. If the MBR is corrupted the system won’t boot properly and the MBR should be fixed. A solution for the MBR corruption is to backup the MBR with the dd command like below and then restore which is explained below. We set the bs which is byte size as 512 and count as 1 which means we will only copy one 512 bytes into the MBR.img file.

dd bs=512 count=1 /dev/sda /mnt/MBR.img

Restore Disk/Block Device Image

The data in an image file can be easily restored into the disk, partition, or block device with the dd command. Specify the source image file with the if option and destination disk, partition, or block device with the of option.

dd if=/mn/backup.img of=/dev/sda

In some cases we may want to restore some part of the source image file. We can use the bs and count options. In the following example we will copy 5k 512byte blocks.

dd bs=512 count=5k if=/mn/backup.img of=/dev/sda

Read Zero Data

Linux and Unix provide different data sources for different cases. The /dev/zero data source or device is used to create infinite 0’s which can be useful to overwrite existing data that can be located on a disk, partition, block device of file. We can read /dev/zero and write this data into a disk-like below which will nullify the existing data.

dd if=/dev/zero of=/dev/sda

Alternatively we can nullify and overwrite into a file like below. All data in the disk.img deleted.

dd if=/dev/zero of=/mnt/disk.img

Read Random Data

Another useful data source for the dd command is the /dev/random device. The random device can be used to create random data and put it inside the disk, disk partition, block device, or file. This also overwrites existing data too. But generated data is random which is not always 0.

dd if=/dev/random of=/dev/sda

Alternatively random data can be put into a file which delete existing data in a secure manner.

dd if=/dev/random of=/mnt/disk.img

Wipe Disk/Block Device

Actually we have already learned wiping a disk, disk partition, block device, or file data in previous examples. But here we will provide more detailed examples. Wiping data removes the existing data completely. To make things more secure wiping multiple times is a better and more secure way. The /dev/zero and /dev/random can be read and write into a disk, disk partition, block device, or file to wipe.

#Wipe a disk device
dd if=/dev/random of=/dev/sda

#Wipe a disk partition
dd if=/dev/random of=/dev/sda1

#Wipe a file
dd if=/dev/random of=/mnt/file.img

Leave a Comment