What it is¶
dd copies bytes from an input to an output, with optional conversion. Because devices are files on Linux, it is used for disk imaging, cloning, writing ISOs to USB sticks, and creating test files. References: Wikipedia, some dd examples.
of=overwrites its target without asking. Swappingifandof, or naming the wrong device, destroys data instantly. Double-check withlsblkand unmount the target first.
Create test files¶
dd if=/dev/zero of=test.img bs=1M count=100 # 100 MB of zeroes, really written
dd if=/dev/zero of=sparse.img bs=1 count=0 seek=10G # 10 GB sparse file (allocated lazily)
truncate -s 10G sparse.img # same thing, simpler
fallocate -l 1G prealloc.img # instantly reserve 1 GB (util-linux)
Images and clones¶
# Whole-disk image to a file (run from a live environment for a consistent image)
sudo dd if=/dev/sda of=/mnt/external/disk-$(date +%Y%m%d-%H%M).img bs=32M conv=noerror,sync status=progress
# Clone a disk (destination must be at least as large)
sudo dd if=/dev/sda of=/dev/sdb bs=4M conv=noerror,sync status=progress
# Compress the image on the way
sudo dd if=/dev/sda bs=4M status=progress | gzip -c > disk.img.gz
gunzip -c disk.img.gz | sudo dd of=/dev/sda bs=4M status=progress
bs= sets the block size. Larger values (1M to 32M) are far faster than the 512-byte default. conv=noerror,sync continues past read errors and pads the unreadable blocks, which is what you want for a failing disk (for serious recovery use ddrescue instead, which retries intelligently and keeps a map). conv=notrunc matters when writing into a regular file: it stops dd from truncating the output.
A running system's disk is changing while you image it. For a trustworthy backup, boot from a live medium, unlock and mount any encrypted volumes read-only, and image from there.
Writing an ISO to a USB stick¶
sudo dd if=distro.iso of=/dev/sdX bs=4M conv=fsync status=progress
Triple-check /dev/sdX is the stick, not your system disk.
Copy a partition (example: SD card)¶
sudo umount /dev/mmcblk0p1
sudo dd if=/dev/mmcblk0p1 of=sd_card.img bs=4M status=progress
# repartition / reformat the card, then restore
sudo dd if=sd_card.img of=/dev/mmcblk0p1 bs=4M status=progress
Progress¶
dd if=/dev/zero of=disk.img bs=1M count=1024 status=progress # modern dd (coreutils 8.24+)
sudo kill -USR1 $(pidof dd) # older dd: print stats once
watch -n 1 'sudo kill -USR1 $(pidof dd)' # repeatedly
Over SSH¶
Pipe with pv for a progress bar:
# This host -> remote file
sudo dd if=/dev/sdc bs=4M | pv -tpreb | ssh [email protected] "dd of=/data/images/sdc.img bs=4M"
# Remote disk -> this host
ssh [email protected] "sudo dd if=/dev/sdc bs=4M" | pv -tpreb | dd of=/data/images/sdc.img bs=4M
See also: rsync for file-level copies, scrub/shred and blkdiscard for erasing (zeroing with dd is not a reliable secure erase on SSDs), wipefs.