dd Command: Disk Imaging and Safe Usage

dd Command: Disk Imaging and Safe Usage

What You'll Learn

  • How to clone a disk or partition into an image file with dd
  • How to write a downloaded ISO to a USB stick to make boot media
  • How to avoid wiping the wrong device with a mistaken of=

Quick Summary (the safe workflow)

  1. Identify the target device with lsblk (match by size and mount state)
  2. sudo dd if=<input> of=<output> bs=4M status=progress conv=fsync
  3. Run sync afterward to flush write buffers

Assumptions (environment)

  • OS: Ubuntu / any common Linux distribution
  • dd ships with GNU coreutils. No extra install needed
  • Device operations require sudo (root)

What Is the Basic Syntax of dd?

Conclusion: The base form is dd if=input of=output bs=blocksize. Arguments use key=value form, not hyphenated flags like most commands.

dd simply reads from an input (if) and writes to an output (of). Every argument is written as key=value.

$ dd if=input.img of=/dev/sdb bs=4M status=progress

Key operands:

  • if= (input file): source. Defaults to standard input
  • of= (output file): destination. Defaults to standard output
  • bs= (block size): bytes read/written at once (e.g. 4M = 4 MiB)
  • count=: number of blocks to copy (to clone only the start)
  • status=progress: live transfer amount and speed
  • conv=fsync: guarantee data is written to the physical media on finish

bs directly affects speed. The default 512 bytes is slow. For disks or USB media, bs=4M is a safe choice.

How Do You Identify the Right Drive? (Most Important)

Conclusion: Use lsblk to match capacity and mount point, then confirm the target device name. Always note the difference between /dev/sda (the whole disk) and /dev/sda1 (a partition).

Most dd disasters come from picking the wrong device name. Always check first.

$ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda      8:0    0 238.5G  0 disk
├─sda1   8:1    0   512M  0 part /boot/efi
└─sda2   8:2    0   238G  0 part /
sdb      8:16   1  28.7G  0 disk
└─sdb1   8:17   1  28.7G  0 part /media/user/USB

How to read it:

  • sda: 238.5G, mounted at / -> system disk. Never write to it
  • sdb: 28.7G with RM (removable) set to 1 -> the USB stick you inserted
  • sdb with no trailing number is the whole disk; sdb1 is a partition inside it

Run lsblk once before and once after inserting the USB, then target the newly appeared device. Cross-check the SIZE column too.

How Do You Write an ISO Image to a USB?

Conclusion: Unmount the target USB, then run sudo dd if=xxx.iso of=/dev/sdX bs=4M status=progress. Point of= at the whole disk with no partition number.

The standard way to make a bootable USB for Ubuntu and similar.

1. Unmount it

A mounted target during writing causes failures or corruption.

$ sudo umount /dev/sdb1

2. Write with dd

$ sudo dd if=ubuntu-24.04.iso of=/dev/sdb bs=4M status=progress conv=fsync
1538000000 bytes (1.5 GB, 1.4 GiB) copied, 142 s, 10.8 MB/s
367+1 records in
367+1 records out

3. Sync before removing

The kernel may still hold buffered data after dd returns. Flush it with sync.

$ sync

conv=fsync and a trailing sync overlap in purpose, but using both prevents the "command finished but the write is still in flight" failure.

How Do You Clone a Whole Disk to an Image File?

Conclusion: Put the device in if= and an image file in of= to back up the whole thing. Pipe through gzip to shrink mostly-empty disks.

Back up a disk or partition into a single file.

$ sudo dd if=/dev/sdb of=usb-backup.img bs=4M status=progress

Compress while saving (useful when much of the disk is empty):

$ sudo dd if=/dev/sdb bs=4M status=progress | gzip > usb-backup.img.gz

An image file consumes the same capacity as the source device (a 28 GB USB -> a 28 GB file). Check the destination free space with df -h first.

How Do You Restore an Image Back to a Disk?

Conclusion: Just swap if= and of=. Pipe compressed images through gunzip while restoring. Double-check the destination once more.

# Uncompressed image
$ sudo dd if=usb-backup.img of=/dev/sdb bs=4M status=progress conv=fsync

# Compressed image
$ gunzip -c usb-backup.img.gz | sudo dd of=/dev/sdb bs=4M status=progress conv=fsync

How Do You Speed It Up or Watch Progress?

Conclusion: A larger bs (4M to 64M) is faster. Watch progress with status=progress; on older systems that lack it, send kill -USR1 from another terminal.

Tuning the block size

$ sudo dd if=/dev/sdb of=backup.img bs=64M status=progress

A small bs means more system calls and slower transfers. Too large just wastes memory, so 4M to 64M is the practical range.

Checking progress without status=progress

Send a signal to a running dd from another terminal to print the current transfer amount.

$ sudo kill -USR1 $(pgrep -x dd)

GNU coreutils 8.24 and later support status=progress. Prefer it, and treat kill -USR1 as the fallback for older environments.

What Are the Common dd Disasters?

Conclusion: Nearly every accident reduces to a wrong of=, swapped if/of, or writing to a mounted device. A pre-run check prevents all three.

Disaster Cause Prevention
Destroyed the system disk Wrong of= device name Match size and mount with lsblk first
Backup comes out empty Swapped if= and of= Say out loud "read = if, write = of"
Wrote it but it won't boot Wrote to a partition (sdb1) Write ISO to the whole disk (sdb)
Content corrupt after finishing Pulled USB before buffers flushed Always use conv=fsync + sync

Copy-paste safe template

# 1. Check devices (always first)
lsblk

# 2. Write ISO to USB (assumes it is unmounted)
sudo dd if=image.iso of=/dev/sdX bs=4M status=progress conv=fsync

# 3. Sync after finishing
sync

Replace /dev/sdX with the actual device name you confirmed in lsblk.

Next Reading