dd
Command
dd is a command-line utility for Unix and Unix-like operating systems, the primary purpose of which is to convert and copy files.
On Unix, device drivers for hardware (such as hard disk drives) and special device files (such as /dev/zero and /dev/random) appear in the file system just like normal files; dd can also read and/or write from/to these files, provided that function is implemented in their respective driver.
As a result, dd can be used for tasks such as backing up the boot sector of a hard drive, and obtaining a fixed amount of random data.
Parameters
By default, dd reads from stdin and writes to stdout, but these can be changed by using the if (input file) and of (output file) options.
bs=BYTES
: read and write up to BYTES bytes at a time (default: 512); overrides ibs and obscount=N
: copy only N input blocksif=FILE
: read from FILE instead of stdinof=FILE
:write to FILE instead of stdout
Demo
To backup the entire harddisk
To backup an entire copy of a hard disk to another hard disk connected to the same system, execute the dd command as shown. In this dd command example, the UNIX device name of the source hard disk is /dev/hda
, and device name of the target hard disk is /dev/hdb
.
$ dd if=/dev/sda of=/dev/sdb
- “if” represents inputfile, and “of” represents output file. So the exact copy of /dev/sda will be available in /dev/sdb.
- If there are any errors, the above command will fail. If you give the parameter “conv=noerror” then it will continue to copy if there are read errors.
- Input file and output file should be mentioned very carefully. Just in case, you mention source device in the target and vice versa, you might loss all your data.
To backup a Partition
You can use the device name of a partition in the input file, and in the output either you can specify your target path or image file as shown in the dd command.
$ dd if=/dev/hda1 of=~/partition.img
To create an image of a Hard Disk
Instead of taking a backup of the hard disk, you can create an image file of the hard disk and save it in other storage devices. There are many advantages of backing up your data to a disk image, one being the ease of use. This method is typically faster than other types of backups, enabling you to quickly restore data following an unexpected catastrophe.It creates the image of a hard disk /dev/hda.
$ dd if=/dev/hda of=~/hdadisk.img
To restore using the Hard Disk Image
To restore a hard disk with the image file of an another hard disk, the following dd command can be used
$ dd if=hdadisk.img of=/dev/hdb
The image file hdadisk.img file, is the image of a /dev/hda, so the above command will restore the image of /dev/hda to /dev/hdb.
Backup MBR
One of the most typical use cases of dd
for the utility is the backup ofthe master boot record: the first sector on a legacy MBR
partitioned system. The length of this sector is usually 512
bytes: it contains the stage 1 of the grub bootloader
and the disk partition table. Suppose we want to backup the MBR
of /dev/sda disk, all we have to do is to invoke dd with the following syntax:
$ sudo dd if=/dev/sda bs=512 count=1 of=mbr.img
1+0 records in
1+0 records out
512 bytes copied, 0.000657177 s, 779 kB/s
Let’s analyze the command above. First of all we prefixed the actual dd invocation with sudo command, in order to run the command with administrative privileges. This is needed to access the /dev/sda
block device. We then invoked dd specifying the input source with the if
option and the output file with of
. We also used the bs
and count
options to specify respectively the amount of data that should be read at a time, or block size, and the total amount of blocks to read. In this case we could have omitted the bs
option, since 512
bytes is the default size used by dd.
The output above shows us the amount of records read and written, the amount of data copied, the amount of time in which the task was completed and the transfer speed. We now should have a clone of the MBR
sector, stored in the mbr.img
file. Obviously the file suffix has no real meaning on Linux, so the use of the “.img” one is completely arbitrary: you may want to use “.dd” to let the filename reflect the command that was used to create the file.
In the example above we use the bs
option to define both the amount of bytes that should be read and write at a time. To define separately values for the two operations, we can use the ibs
and obs
options instead, which set, respectively, the amount of bytes read and written at a time.
Benchmark the write performance of a disk
$ dd if=/dev/zero of=file_1GB bs=1024 count=1000000
Clone a drive to another drive with 4MB block and ignore error
$ dd if=/dev/source_drive of=/dev/dest_drive bs=4m conv=noerror
Make a bootable usb drive from an isohybrid file (such like archlinux-xxx.iso)
$ dd if=file.iso of=/dev/usb_drive
Reference
- https://en.wikipedia.org/wiki/Dd_(Unix)
- https://man7.org/linux/man-pages/man1/dd.1.html
- https://www.geeksforgeeks.org/dd-command-linux/
- https://linuxconfig.org/how-dd-command-works-in-linux-with-examples