UNIX / Linux: Copy Master Boot Record (MBR)

by on September 20, 2007 · 19 comments· LAST UPDATED December 31, 2010

in , ,

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

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 19 comments… read them below or add one }

1 caiken December 21, 2007 at 10:02 pm

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.

Reply

2 graysky October 31, 2009 at 2:11 pm

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

Reply

3 graysky October 31, 2009 at 2:14 pm

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

Reply

4 Sicarius November 11, 2009 at 9:15 pm

Thanks a lot, this was just the kind of short note I was looking for ,)

Reply

5 Brandon November 27, 2009 at 12:33 am

MBR is 512 or 446 bytes?

Reply

6 Bolo December 21, 2009 at 8:57 pm

MBR is divided into 3 sections
1. Bootstrap . 446 bytes
2. Partition table. 64 bytes
3. Signature. 2 bytes

Reply

7 Pavel April 19, 2010 at 12:31 pm

Very useful comments about 446 bytes. Thanks!

Reply

8 Ashrock November 15, 2010 at 6:52 pm

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

Reply

9 Fionn November 21, 2010 at 12:50 am

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’.

Reply

10 Niko December 2, 2010 at 3:26 pm

Since this is the first Google result for “linux copy mbr”, please edit original post adding a BIG FAT WARNING about the 512/446 size values.
Thanks

Reply

11 clfapujc December 31, 2010 at 10:31 am

# sfdisk -d /dev/sda > /tmp/sda.bak
# sfdisk /dev/sda < /tmp/sda.out
it should be bak also

Reply

12 nixCraft December 31, 2010 at 3:13 pm

Thanks for the heads up!

Reply

13 Nick March 17, 2011 at 3:58 pm

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…..

Reply

14 nixCraft March 17, 2011 at 5:31 pm

It can be done on both mounted and unmouted file system.

Reply

15 Mike May 18, 2011 at 3:16 pm

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 …)

Reply

16 Ndoum February 8, 2012 at 10:31 pm

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…

Reply

17 Rashid August 26, 2012 at 7:00 am

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,

Reply

18 srrikanth September 13, 2012 at 3:14 pm

How to decrease mbr size, if it is posible ?
How to increase mbr size, if it is posible ?

Reply

19 venturi November 2, 2012 at 3:11 am

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 ?

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: