System administrators often need to clone partition or hard disk in Linux, to backup their data. There are many third-party tools available to help you do this. In this article, we will look at how to clone a partition or hard disk using dd command which is pre-installed in most Linux distributions. It is used to convert or copy files but can also be used for purpose described above.
How to Clone Partition or Hard Disk in Linux
Clone Linux Partition
dd command allows you to copy partitions as well as hard disks. Let us say you have two partitions /dev/sdb1 and /dev/sdb2.
Here is the command to list these partitions using fdisk command.
$ sudo fdisk -l /dev/sdb1/ /dev/sdb2
The above command will display details about the two partitions such as their size, no. of sectors, sector size, i/o size. You can clone one partition to another only if both exist.
Here is the command to clone partition /dev/sdb1 to /dev/sdb2
# dd if=/dev/sdb1 of=/dev/sdb2
In the above command, we tell dd command to use /dev/sdb1 as input file and /dev/sdb2 as output file. It is important that destination partition should be equal in size or larger than source partition.
Clone Linux Hard Disk
Cloning hard disk is similar to cloning partition. Instead of specifying partitions as input file and output file, we specify hard disks. Here also it is important that the destination disk is equal in size or larger than source disk. Let us say you want to clone hard disk /dev/sdb to /dev/sdc then here is the command to do it.
# dd if=/dev/sdb of=/dev/sdc
Once the cloning is complete, you can verify the change by listing the 2 disks using fdisk command.
# fdisk -l /dev/sdb /dev/sdc
Backup MBR
You can also use dd command to backup Master Boot Record (MBR) located in first sector of device, even before 1st partition.
Here is the command to backup MBR with dd command.
# dd if=/dev/sda of=/backup/mbr.img bs=512 count=1.
In the above command we tell dd command to copy 1 block of 512 bytes to /backup/mbr.img file. This is because we know that the first sector contains MBR and so we instruct dd command to copy only that sector.
That’s it. In this article, we have learnt different ways to clone our data using dd command – clone partition or hard disk. We have also seen how to clone master boot record of hard disk.
Also read:
How to Assign Multiple IP Addresses to Network Interface
How to Find Public IP Address of Server
How to Find Folder Name in Linux
How to Check Open Ports in UFW
How to Debug Shell Script
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.