How to use the dd command on Linux? MBR backup and more examples
Can you imagine having your life easier, with a specific command line, capable of copying, converting and creating new data?
That’s exactly what the dd (data duplicator) command does for you on the Linux system! This tool promises to facilitate your work routine and make everything even more intuitive.
But for this to happen, you need to follow some steps, such as knowing the syntax of the dd command, understanding how to back up the MBR, and a few more points.
With that in mind, the Linux Certification team developed this complete content, with everything you need to know about the dd command. Check it out!
What is the dd command?
Photo: Pexels | What is the dd command? The dd or “data duplicator” command is a tool that helps you convert, copy and create new files in image format, both on hard drives and other files.
Because it performs so many functions in the same command, it is classified as a utility on Linux, within the GNU standard and coreutils, a package that accompanies it.
Simply put, 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 is no problem.
Ddd 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 archive image:
# dd if=/dev/fd0 or=/root/disco1.bin
To write an image to a floppy disk:
# dd if=/root/disco1.bin or=/dev/fd0
This command can also be used to clone one HD to another.
Suppose two hard drives, one installed as a primary master (/dev/hda) and the other as a primary slave (/dev/hdb):
# dd if=/dev/hda or=/dev/hdb
Since the copy is done bit by bit, it doesn’t matter what the operating system is, nor the file system used on the source hard drive. The copy is complete, including the HD partition table and the boot sector.
**What is Master Boot Record (MBR)?
Backing up the MBR is essential in cases where a disk recovery is necessary.
The MBR is the first disk sector, on track 0. It occupies a sector on cylinder 0, side 0 and sector 1. Its size is 512 bytes. The last 64 bytes of the MBR are where the Partition Table is stored.
Those 64 bytes are divided into four groups of 16 bytes. Each group corresponds to a partition.
In each group of 16 bytes, information about the disk partitions is stored, stating whether it is bootable, whether they are primary or extended, and which file system they use (EXT2, EXT3, Reiser, FAT, NTFS, etc.).
In addition to the Linux partition table, the MBR starts the boot sector of the first active primary partition on the disk.
The boot sector of this partition stores the information necessary to read the operating system boot files.
Thus, the MBR contains information that is very important for the functioning of the system. Having a copy of this information is very important, but hardly anyone remembers it.
Examples of using the dd command on Linux
Photo: Pexels | Use of the dd command on Linux. To understand exactly how to use the dd command on Linux, it is essential that you analyze some examples and check how they are used to facilitate the routine of those who choose them.
With this in mind, we selected 3 practical examples involving the dd command, namely:
- MBR backup, preparing it to use the command;
- creating disk images;
- and creating SWAP files.
We recommend that you save this information somewhere, either 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 when any doubt or indecision arises.
1. Back up the MBR
To back up the MBR, simply copy the first 512 bytes of the disk to a file:
# dd if=/dev/ada 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 syntax of the dd command:
# dd if=mbr-backup of=/dev/ada bs=446 count=1
**2. Create disk image
To create the disk image, it is necessary to use a specific code, following the structure of the if command:
# dd if=/dev/ada of=/tmp/sda disk.img
If you want to better understand, know that:
- The ‘if=/dev/sda’ part corresponds to the place of origin, which will be copied;
- whereas the block “‘of=/tmp/sda disk.img’ corresponds to the created image, located in the /tmp folder. She will receive a copy of the disc.
In 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 an exchange area.
Thus, the best way to create SWAP files on Linux is to use the dd command, like this:
# dd if=/dev/zero of=/swap2 bs=1024 count=524288
In this way, you can create the SWAP file and then choose the size of the file and memory to be used.
Did you notice how the dd command is useful on 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 area, you need to know the other contents of the Linux Certification!
Here, we share only the most useful and practical information that will really help you in your IT career using the Linux system and its peculiarities. It’s worth checking it out!