UNIX / Linux: Copy Master Boot Record (MBR)

Posted on in Categories , , , , , , last updated December 31, 2010

How do I copy MBR from one hard disk to another hard disk under Debian Linux?

To copy MBR simply use the dd command. dd command works under all Linux distros and other UNIX like operating systems too. A master boot record (MBR) is the 512-byte boot sector that is the first sector of a partitioned data storage device of a hard disk.

MBR Total Size

446 + 64 + 2 = 512

Where,

  • 446 bytes – Bootstrap.
  • 64 bytes – Partition table.
  • 2 bytes – Signature.

512 vs 446 Bytes

  • Use 446 bytes to overwrite or restore your /dev/XYZ MBR boot code only with the contents of $mbr.backup.file.
  • Use 512 bytes to overwrite or restore your /dev/XYZ the full MBR (which contains both boot code and the drive’s partition table) with the contents of $mbr.backup.file.

dd command to copy MBR (identically sized partitions only)

Type dd command as follows:
dd if=/dev/sda of=/dev/sdb bs=512 count=1
Above command will copy 512 bytes (MBR) from sda to sdb disk. This will only work if both discs have identically sized partitions.

dd command for two discs with different size partitions

# dd if=/dev/sda of=/tmp/mbrsda.bak bs=512 count=1
Now to restore the image to any sdb:
# dd if=/tmp/mbrsda.bak of=/dev/sdb bs=446 count=1
The above commands will preserve the partitioning schema.

Linux sfdisk Command Example

Linux sfdisk command can make a backup of the primary and extended partition table as follows. It creates a file that can be read in a text editor, or this file can be used by sfdisk to restore the primary/extended partition table. To back up the partition table /dev/sda, enter:
# sfdisk -d /dev/sda > /tmp/sda.bak
To restore, enter:
# sfdisk /dev/sda < /tmp/sda.bak
The above command will restore extended partitions.

Task: Backup MBR and Extended Partitions Schema

Backup /dev/sda MBR, enter:
# dd if=/dev/sda of=/tmp/backup-sda.mbr bs=512 count=1
Next, backup entries of the extended partitions:
# sfdisk -d /dev/sda > /tmp/backup-sda.sfdisk
Copy /tmp/backup-sda.sfdisk and /tmp/backup-sda.mbr to USB pen or somewhere else safe over the network based nas server.

Task: Restore MBR and Extended Partitions Schema

To restore the MBR and the extended partitions copy backup files from backup media and enter:
# dd if=backup-sda.mbr of=/dev/sda
# sfdisk /dev/sda < backup-sda.sfdisk

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

20 comment

  1. This is correct for EXACTLY the same partition sizes. In the case you want the MBR only on a drive that has a root partition of a different size then issue the same command with a smaller block size.

    dd if=/dev/sda of=/dev/sdb bs=446 count=1

    Thus preserving the partitioning schema.

  2. Right… be VERY careful with doing this w/ two discs with different size partitions! In fact, the safest way to backup/restore JUST THE MBR:

    dd if=/dev/sda of=mbrbackup bs=512 count=1

    To restore the MBR (on to the same drive or any drive):

    dd if=mbrbackup of=/dev/sdb bs=446 count=1

  3. Caiken is right! BEWARE of using the above dd line on two discs that don’t have IDENTICALLY sized partitions! You will lose data!

    Here’s what I do to copy just the MBR from a hdd:

    dd if=/dev/sda of=mbrbackup bs=512 count=1

    Now to restore the image to any hdd:

    dd if=mbrbackup of=/dev/sdb bs=446 count=1

  4. I am not sure what MBR is until i found this post.
    But this is not exactly what i want to do.
    I want to copy all the files and directories of a root hard disk with a disk size of 70GB into a external hard drive with a disk capacity of 250GB mounted on /mnt/sda2 .
    I can use only cp command as my Debian OS crashed and i want to take immediate backup of all the files.
    Is this command useful in any way to do that.
    Thanks,

    Ash

    1. Ash;

      If you can use ‘cp’, then you can use ‘dd’; they’re both required for Posix-compliant systems (such as is Debian).

      Also, you cannot make a ‘backup’ after the system has crashed. Backups are made before crashes happen so that Recovery may be performed. What you are attempting to do is called ‘salvage’.

  5. Hi .. there are numerous info about this nowadays on all sites…
    but one thing i never sure of is ….
    SHOULD THIS BE DONE ON MOUNTED OR UNMOUNTED FILESYSTEM ????
    No sites explain this really…..

  6. IHi.

    I tried this twice to virtualize a physical machine to ESX but each time, on boot, I only have
    GRUB _
    displayed and no more. I used the 446 bytes size. The partitionning is right and all data have been copied from old to new disk.
    I finally startup from a rescue disk, chrooted and used grub-install.

    But could grub store data somewhere else ?
    (I tried on grub 1 and grub 2 …)

  7. I found myself with the MBR on one drive (sda) (which seems to have packed up), Ubuntu 11.04 install (sdb) on another drive and all I have access to is the the 8.04 live disc. I was unable to therefore use things like grub-install since I couldn’t mount my drives (kep getting unrecoginzed file extensions or some such….)

    So this seemed the most practical way of doing things since all I wanted to do was create a new MBR where my 11.04 install is with the info from the packed up drive. But a quick check using Boot Info Script:

    http://sourceforge.net/projects/bootinfoscript/

    shows that core.img is missing from after using dd to replace the MBR on sdb. Mind you I did use 446 since sda is 80Gb and sdb is 160Gb. I assumed I’m dealing with two different sized partitions.

    I’d appreciate any insigths…

  8. this is a very good article.

    I take the backup of mbr as above mentioned procedure
    dd if=/dev/sda of=/tmp/mbrsda.bak bs=512 count=1

    How can I restore the mbr(I take the backup of mbrsda.bak file on usb/cd) I case my harddisk craches.

    Regards,

  9. I am interested in using dd command to transfer a fully working ( and licensed with OEM key ) XP Home OS on a bootable 10GB Western Digital hard drive to a 16GB flash USB thumb drive. The Western Digital is at /dev/sda and the USB thumb drive is at /dev/sdc ( Ubuntu 10.10 is running on a USB thumb drive at /dev/sdb ). I use the command sudo dd if=/dev/sda of=/dev/sdc bs=64k. I partitioned and formatted the thumb first to full 16GB for NTFS ( I don’t thingk that matters using dd bit for bit image ). All files transferred ok BUT when I try to boot off the new thumb XP Home goes to the safe mode warning menu saying “Windows shut down in an unexpected manner. Do you want to boot normal, safe, command prompt, etc”. Selecting ANY of the boot modes simply shuts down the computer. Any suggestions ?

Leave a Comment