The Linux
dd command is a versatile utility for creating exact byte-for-byte copies of disks, partitions, and files. It serves as the backbone for disk cloning, full system imaging, and data recovery tasks in Unix-like operating systems.
Understanding how to use
dd correctly is critical because it operates at a low level, bypassing file system checks. This power allows for raw data manipulation but also means that incorrect usage can lead to irreversible data loss if the target device is specified incorrectly.
Basic Syntax and Structure
The fundamental structure of the command involves specifying an input source and an output destination. The syntax follows the pattern
dd if=[source] of=[destination] [options]. Here,
if stands for input file, which typically refers to a block device like
/dev/sda or
/dev/nvme0n1. Conversely,
of denotes the output file, where the cloned data will be written.
It is vital to distinguish between these two parameters. Swapping them results in overwriting the source with the contents of the destination, effectively destroying the original data. Always verify the device names before execution using tools like
lsblk or
dmesg to ensure you are targeting the correct hardware.
Cloning a Disk to Another Disk
One of the most common use cases is cloning one physical drive to another. This process creates an identical copy of the source disk, including the partition table, boot sectors, and all file systems. To perform this operation, you must identify both the source and target devices.
For example, to clone
/dev/sda to
/dev/sdb, you would run:
sudo dd if=/dev/sda of=/dev/sdb bs=4M status=progress conv=noerror,sync
The
bs=4M option sets the block size to 4 megabytes, which significantly speeds up the transfer rate compared to the default smaller blocks. The
status=progress flag provides real-time feedback on the amount of data copied, which is essential for long-running operations. Additionally,
conv=noerror,sync ensures that the command continues even if read errors occur, filling bad sectors with null bytes to maintain synchronization.
Before proceeding, ensure that the target disk is at least as large as the source disk. If the target is smaller, the cloning process will fail or result in a corrupted image. For detailed troubleshooting regarding SSD connectivity issues during such transfers, refer to our guide on
SSD USB Thumb Drive Unexpectedly Disconnects on Raspberry Pi 4.
Creating Disk Images for Backup
Instead of writing directly to another physical drive, you can create an image file stored on your hard drive. This method is ideal for backups, virtual machine snapshots, or archiving entire systems. The resulting file acts as a complete snapshot of the disk state at the time of creation.
To create an image file named
disk_backup.img from
/dev/sda, use the following command:
sudo dd if=/dev/sda of=/path/to/disk_backup.img bs=4M status=progress
Storing images on external storage or network shares conserves local space. You can later restore this image by reversing the
if and
of parameters. When dealing with NVMe drives, ensure that the partitions are recognized correctly, as discussed in our article on
Linux Not Displaying NVME SSD Partitions: Possible Solutions.
Restoring from an Image
Restoration is the inverse of the cloning process. You take an existing image file and write its contents back to a physical disk. This is particularly useful after a system failure or when deploying standardized configurations across multiple machines.
To restore the previously created image to
/dev/sdb, execute:
sudo dd if=/path/to/disk_backup.img of=/dev/sdb bs=4M status=progress
This operation wipes all existing data on the target disk. Ensure that no other processes are accessing the target device, such as mounted file systems or swap spaces. Unmounting any active partitions on the target drive prevents data corruption during the write process.
Advanced Options and Best Practices
Several advanced options enhance the reliability and efficiency of
dd. Using
conv=fdatasync forces a physical sync of I/O operations before finishing, ensuring that all data is flushed to the disk before the command exits. This is crucial for preventing data loss in case of sudden power failures.
Compression can also be integrated into the pipeline to save storage space. By piping the output through
gzip, you can create compressed images without needing extra steps. For instance:
sudo dd if=/dev/sda bs=4M | gzip > /path/to/compressed_backup.img.gz
When restoring compressed images, use
gunzip in reverse. Remember that compression requires more CPU resources, which may slow down the overall process depending on your hardware capabilities.
Frequently Asked Questions
Can dd clone a disk to a smaller target?
No,
dd performs a byte-for-byte copy and cannot resize partitions automatically. The target device must be equal to or larger than the source device to accommodate all data sectors successfully.
How do I monitor the progress of a long dd operation?
Use the
status=progress argument within the command line. This displays the amount of data transferred, the transfer speed, and the elapsed time directly in the terminal output.
Is it safe to use dd on a mounted filesystem?
It is generally unsafe to clone a mounted filesystem because open files may change during the copy process, leading to inconsistencies. For consistent backups, unmount the filesystem or boot from a live environment before running the command.
What happens if there are bad sectors on the source disk?
By default,
dd stops at the first read error. Adding
conv=noerror,sync allows the process to continue, replacing unreadable sectors with zeros to keep the output stream synchronized.
How can I compress a dd backup to save space?
Pipe the output of
dd into a compression tool like
gzip. This reduces the file size significantly but increases the CPU load during both the backup and restoration phases.