fbpx dd Command in Linux: Clone Disk and MBR Backup | Certificação Linux

Mastering the dd Command in Linux: MBR Backup & Examples

📅 03/17/2022 ⏱ 5 min ✍️ Uira Ribeiro
Linuxhow-to
Mastering the dd Command in Linux: MBR Backup & Examples
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

Have you ever imagined having your life made easier, with a specific command line capable of copying, converting and creating new data? 

That's exactly what the dd command (data duplicator) does for you on the Linux system! This tool promises to make your work routine easier and everything even more intuitive. 

But for this to happen, you need to follow some steps, such as knowing the dd command syntax, understanding how to back up the MBR and a few other points. 

With that in mind, the Certificação Linux team has developed this complete content, with everything you need to know about the dd command. Check it out! 

What is the dd command?

dd command what is it
Photo: Pexels | What is the dd command?

The dd command or "data duplicator" is a tool that helps you convert, copy and create new files in image format, both on hard drives and on other files. 

Because it performs so many functions in a single command, it is classified as a utility in Linux, within the GNU standard and coreutils, a package that accompanies it. 

In simple terms, it is a great ally for the root user. The secret lies in knowing when to use and how to use this item, so that there are no problems. 

dd command syntax

The dd command copies a file, disks and partitions to a file, disk and block devices.

Its basic syntax is:

# dd [options]

To copy a floppy disk to an image file:

# dd if=/dev/fd0 of=/root/disco1.bin

To write an image to a floppy disk:

# dd if=/root/disco1.bin of=/dev/fd0

This command can also be used to make a clone from one HD to another.

Let's suppose two HDs, one installed as primary master (/dev/hda) and the other as primary slave (/dev/hdb):

# dd if=/dev/hda of=/dev/hdb

Since the copy is done bit by bit, it doesn't matter what operating system or file system is used on the source HD. The copy is complete, including the HD partition table and the boot sector.

What is Master Boot Record (MBR)?

Making a backup of the MBR is essential in cases where disk recovery is necessary. 

The MBR is the first sector of the disk, on track 0. It occupies one sector on cylinder 0, side 0 and sector 1. Its size is 512 bytes. In the last 64 bytes of the MBR is where the Partition Table is stored.

These 64 bytes are divided into four groups of 16 bytes. Each group corresponds to a partition.

In each 16-byte group, information about the disk partitions is stored, indicating whether it is bootable, if they are primary or extended and what file system they use (EXT2, EXT3, Reiser, FAT, NTFS, etc).

Besides the linux partitions table, the MBR starts the boot sector of the first active primary partition on the disk.

In the boot sector of this partition, the information needed to read the operating system boot files is stored.

In this way, the MBR contains very important information for the system to function. Having a copy of this information is very important, but almost nobody remembers this.

Examples of using the dd command in Linux

dd command examples
Photo: Pexels | Using the dd command in Linux. 

To understand exactly how to use the dd command in Linux, it is essential that you analyze some examples and see how they are used to make the routine of those who choose it easier. 

With that in mind, we selected 3 practical examples involving the dd command, which are:

  • MBR backup, preparing it to use the command;
  • disk image creation;
  • and SWAP file creation.

We advise you to save this information somewhere, whether in your browser bookmarks, or by sharing this article with someone close to you. 

After all, it will serve as your "desk guide", always there to consult whenever a doubt or indecision arises. 

1. Make a backup of the MBR

To make a backup of the MBR, just copy the first 512 bytes of the disk to a file:

# dd if=/dev/sda of=mbr.backup bs=512 count=1

To restore:

# dd if=mbr-backup of=/dev/hda bs=512 count=1

Since the partition table occupies the last 66 bytes of the MBR, we can preserve this information by varying the dd command syntax:

# dd if=mbr-backup of=/dev/sda bs=446 count=1

2. Create disk image

For creating a disk image, you need to use a specific code, following the structure of the if command:

# dd if=/dev/sda of=/tmp/sda disk.img

If you want to understand better, you should know that:

  • the part 'if=/dev/sda' corresponds to the source location, which will be copied;
  • while the block 'of=/tmp/sda disk.img' corresponds to the image created, allocated in the /tmp folder. It will receive the disk copy.

This way, your disk image will be created correctly, without any difficulty or extra need. 

3. Create SWAP files

SWAP, presented here, is nothing more than a way to make better use of RAM memory, that is, it corresponds to a swap area. 

So, the best way to create SWAP files in Linux is by using the dd command, like this:

# dd if=/dev/zero of=/swap2 bs=1024 count=524288

This way, you can create the SWAP file and later choose what the file size and memory to be used will be. 

Did you see how useful the dd command is in Linux? Everything can be customized and activated according to disk availability and your need as a root user. 

If you like content about the Linux universe, discovering commands and preparing for certifications in the field, you need to check out the other content from Certificação Linux! 

Here, we only share the most useful and practical information, that will really help you in your IT career using the Linux system and its particularities. It's worth checking out!

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp