UNIX / Linux: Copy Master Boot Record (MBR)

by Vivek Gite on September 20, 2007 · 16 comments

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

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 16 comments… read them below or add one }

1 caiken December 21, 2007

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

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

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

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

Reply

5 Brandon November 27, 2009

MBR is 512 or 446 bytes?

Reply

6 Bolo December 21, 2009

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

Very useful comments about 446 bytes. Thanks!

Reply

8 Ashrock November 15, 2010

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

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

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

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

Reply

12 Vivek Gite December 31, 2010

Thanks for the heads up!

Reply

13 Nick March 17, 2011

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 Vivek Gite March 17, 2011

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

Reply

15 Mike May 18, 2011

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

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

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 2 + 3 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: