How to make disk image with dd on Linux
Tags: linux
Published: 2022-07-12 19:23:00

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:

```shell
dd if=/dev/ORIGIN_DISK of=/dev/PLACE-TO-WRITE bs=64K conv=noerror,sync
```

 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
```

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
```



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:

```shell
sudo fdisk -l
```

You should see similar to this:
![fdisk](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lxswn95ib96cwj5joixc.png)


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

To create disk image run this:

```shell
dd if=/dev/sda conv=sync,noerror bs=128K | gzip -c > SSD_image.gz
```

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:

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

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:

```shell
gunzip -c SSD_image.gz | dd of=/dev/sda
```

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:

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

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

[![Chris Texe Twitter](https://madlon.eu/wp-content/uploads/sites/21/TwitterTexeButton.webp)](https://twitter.com/TexeChris)

[![Chris Texe LinkedIn](https://madlon.eu/wp-content/uploads/sites/21/LinkedInTexeButton.webp)](https://www.linkedin.com/in/texe/)

Also you can visit my websites:

- [Linux blog](https://varlock.net)
- [Web Agency website](https://madlon.eu)