DEV Community

Cover image for How to make disk image with dd on Linux
Chris Texe
Chris Texe

Posted on • Updated on

How to make disk image with dd on Linux

Recently I had to send my notebook to service (by the way - it's fifth time when it's in service). When I sent my notebook first time, I made a full backup because service after repair usually delete all data on disk and install clean Windows to check if everything is OK. But when I sent my notebook third time I said ENOUGH! Every few months I had to install Linux Mint, configure it, install my apps and so on, and so on... Then I started to create images. So how to make disk image with dd on Linux and what is the disk image?

What is disk image?

Disk image is a one big file which contains every byte of some disk. Notice that it's not a copy if files. DIsk image is created by special applications from low level disk reading. It means these applications read disk sectors byte by byte and write them to another disk creating one big file. It's very useful when you want to change disk to another for instance HDD to SSD.

How to create whole disk image?

This command creates image of whole disk:

dd if=/dev/ORIGIN_DISK of=/dev/PLACE-TO-WRITE bs=64K conv=noerror,sync
Enter fullscreen mode Exit fullscreen mode

To clone /dev/sda (250GB) to /dev/sdb (250GB) in Linux, type this:

dd if=/dev/sda of=/dev/sdb bs=64K conv=noerror,sync
Enter fullscreen mode Exit fullscreen mode

After running above command you will get a big 250GB file.

How to create a partition image?

This command creates image of whole disk:

dd if=/dev/sda1 of=/dev/sdb1 bs=64K conv=noerror,sync
Enter fullscreen mode Exit fullscreen mode

Let's stop here for a moment and see what we have here:

if - input device/file

of - output device/file

bs=64K - The block size. Usually peope use 64K or 128K, but you can use other values if know what you do

conv=noerror - dd will continue operation and ignore all read errors

sync - add input blocks with zeros if there were any read errors (it means data offsets stay in sync)

How to create disk image using live CD or USB flash drive?

Just boot your CD/DVD or USB flash drive and run terminal. Then list your disks by using fdisk:

sudo fdisk -l
Enter fullscreen mode Exit fullscreen mode

You should see similar to this:
fdisk

Sometimes you can see different names of disk, eg. nvme for SSD M.2.

To create disk image run this:

dd if=/dev/sda conv=sync,noerror bs=128K | gzip -c > SSD_image.gz
Enter fullscreen mode Exit fullscreen mode

DD will create compressed image of whole sda disk. The image will be in current directory. If you want to save it in another directory, tell dd where to save the image:

dd if=/dev/sda conv=sync,noerror bs=128K | gzip -c > /media/disk2/images/SSD_image.gz
Enter fullscreen mode Exit fullscreen mode

Above commands clone the entire hard disk, including the MBR, bootloader, all partitions, UUIDs, and data.

How to restore disk image?

To restore compressed image to a new disk:

gunzip -c SSD_image.gz | dd of=/dev/sda
Enter fullscreen mode Exit fullscreen mode

We assume SSD_image.gz is in current directory and we will lost all data on /dev/sda.

Bonus tip

If you want to see the progress just add status=progress:

dd if=/dev/sda conv=sync,noerror bs=128K status=progress | gzip -c > /media/disk2/images/SSD_image.gz
Enter fullscreen mode Exit fullscreen mode

It would be great if you will comment or follow me on social media:

Chris Texe Twitter

Chris Texe LinkedIn

Also you can visit my websites:

Oldest comments (0)