Write Image File to SD Card Using dd Command
In this article
The command to write an image file to an SD card using dd is sudo dd if=/path/to/image.img of=/dev/sdX bs=4M status=progress conv=fsync, where /dev/sdX is the SD card's device identifier. This command copies the raw image byte-for-byte onto the card, overwriting all existing data, and is the standard method for flashing Raspberry Pi OS, Android images, and other disk images on Linux.
What the dd Command Does
dd is a Unix utility that copies data at the block level. Unlike file managers that copy individual files, dd reads the entire image file and writes it directly to the storage device, preserving partition tables, boot loaders, and file system metadata exactly as they appear in the image. This makes it ideal for creating bootable SD cards because the card becomes an exact clone of the image, not just a folder of files.
The basic syntax breaks down into four parts:
if=— input file, the image you want to write (e.g.,if=raspios.img)of=— output file, the SD card device (e.g.,of=/dev/sdb)bs=— block size, how many bytes are read/written at once (4M is a common efficient choice)status=progress— shows a live transfer speed and byte count
Adding conv=fsync forces dd to physically write all data to the card before exiting, preventing the common issue where the command finishes but the card is not fully written.
Finding the Correct SD Card Device
Writing to the wrong device destroys data on your hard drive, so identifying the SD card is the most critical step. Before plugging in the card, run lsblk to list all block devices. Then insert the SD card and run lsblk again. The new entry that appears is your SD card, typically named sdb, sdc, or similar, with a size matching the card's capacity.
For example, if lsblk shows sda as your main disk and a new sdb with size 29.8G appears after inserting a 32GB card, then /dev/sdb is the SD card. Never rely on the device name alone; always verify by size and by the fact that it newly appeared.
Some systems also show partitions on the card like sdb1 and sdb2. You must write to the whole device (/dev/sdb), not to a partition (/dev/sdb1), because the image contains its own partition table.
Step-by-Step Writing Procedure
Once you have confirmed the device name, the writing process is straightforward:
- Unmount any mounted partitions on the SD card:
sudo umount /dev/sdb* - Run the write command:
sudo dd if=image.img of=/dev/sdb bs=4M status=progress conv=fsync - Wait for completion; the progress bar shows bytes copied and speed
- When finished, run
syncto flush remaining buffers - Remove the card safely with
sudo eject /dev/sdbor by using the desktop's unmount option
Writing a 4GB image to a typical SD card takes roughly 1–3 minutes depending on card speed and USB reader. Larger images like a 16GB Android build can take 10–20 minutes. Do not interrupt the process; pulling the card mid-write corrupts the card and may require reformatting.
Common Mistakes and How to Avoid Them
The most frequent errors people encounter are:
- Writing to the wrong device — always double-check with
lsblkbefore runningdd - Using the partition instead of the whole disk —
/dev/sdb1instead of/dev/sdb - Forgetting
sudo—ddrequires root privileges to write to block devices - Not using
conv=fsync— the command may appear to finish but data is still in cache - Writing a compressed image directly — if the image ends in
.gzor.xz, you must decompress it first or pipe it throughgunzip
For compressed images, use gunzip -c image.img.gz | sudo dd of=/dev/sdb bs=4M status=progress conv=fsync. This decompresses on the fly and writes directly, saving disk space.
Verifying the Write Was Successful
After writing, you can verify the card by comparing the image to what is on the card. The cmp command compares two files byte-by-byte:
sudo cmp /dev/sdb image.img
If the command outputs nothing and returns to the prompt, the write is identical. If it reports a difference at some byte offset, the write failed or the card is faulty. Note that this only works if the image size matches the card's usable space; if the image is smaller than the card, the comparison stops at the end of the image, which is fine.
Alternatively, you can mount the card after writing and check that the expected files exist. For Raspberry Pi images, the boot partition should contain files like config.txt and kernel.img.
Alternatives to dd
While dd is universal and scriptable, several alternatives offer simpler interfaces or additional safety features:
- Balena Etcher — a graphical tool that works on Linux, macOS, and Windows; it validates the write automatically
- Raspberry Pi Imager — official tool that downloads and writes images, also offers optional verification
- GNOME Disks — built into many Linux desktops; has a "Restore Disk Image" option
- pv — a pipe viewer that can be combined with
ddfor a progress bar if yourddlacksstatus=progress
These tools essentially run dd in the background but add a user interface and post-write verification. For single writes, they are often easier; for scripting or automation, dd remains the standard.
Safety and Data Loss Considerations
Writing an image with dd is destructive. Every byte on the target card is overwritten; there is no undo. Before proceeding, confirm that:
- The SD card does not contain data you need
- The device name is correct (recheck with
lsblk) - The image file is not corrupt (check its checksum against the source)
- You have enough free disk space if using a compressed image pipeline
If you accidentally write to your hard drive, stop immediately, do not reboot, and seek data recovery help. The damage may be partial if you interrupted quickly, but any write can destroy the partition table. This is why verifying the device name twice is non-negotiable.
Frequently Asked Questions
Why does dd appear to hang or take a long time?
If you omit status=progress, dd shows no output until it finishes, which can look like a hang. Even with progress, slow SD cards or USB 2.0 readers make writes take several minutes. If the command is running longer than expected, check the progress output; if it shows a stalled byte count, the card may be failing or the reader may have disconnected.
Can I write a .zip or .rar image file with dd?
No. dd writes raw bytes, so a compressed archive would be copied as-is, not decompressed. You must extract the image first (unzip image.zip) or pipe through a decompressor like gunzip or unxz before the of= part.
What does "No space left on device" mean when writing?
This usually means the image file is larger than the SD card's actual capacity. Some cards are mislabeled or have less usable space than advertised. Check the image size with ls -lh image.img and the card size with lsblk; the card must be larger than the image.
Is it safe to remove the SD card immediately after dd finishes?
No. Even with conv=fsync, it is safer to run sync and wait for the command to return. Then eject the card through the file manager or sudo eject /dev/sdb. Removing it immediately can leave the file system in an inconsistent state.
Why does my card show less capacity after writing?
This is normal. The image contains a partition table that defines the usable space. If the image is smaller than the card, the remaining space is unallocated and not shown in file managers. You can expand the partition later using tools like fdisk or the Raspberry Pi's raspi-config.