How to clone a partition or hard drive in Ubuntu (Linux)

Cloning a hard drive can be useful for backing up (byte for byte) or moving data to a new hard drive. In Ubuntu this is pretty simple, though it is important to pay attention!

We’re going to use a linux command called “dd“, a low level copying program.

So, how to clone a hard drive?

Let’s open up a terminal firstly. You can do this by:

  • opening up your accessories menu and clicking on terminal
  • opening your unity dash and typing in terminal, then clicking on it!
  • press ctrl-alt-t

Create partition to copy to

Get Gparted

For this we’ll need a program called Gparted (it can be done from command line but for the sake of simplicity we’ll use a gui). Gparted can be found on the Ubuntu live media (CD or USB). Alternatively you can download it by typing this command into a terminal:

sudo apt-get install gparted

Open up Gparted (from your accessories menu, unity dash or terminal)

Create partition to backup to (target)

You should now see a screen like this. Make a note of the partition you want to clone/backup (source/input file):

In this case, lets backup the Ubuntu partition, /dev/sdd5.

To create the partition we select unallocated space and click new:

My original drive was 80gb, so I need to create a new partition of the same size and click Add. You need to click Apply after to actually apply the changes you are making here.

Note: A gb (gigabyte) is actually 1024mb (not 1000) so make sure you input the correct size!

Make a note of the newly created partition (target/output file).

Now its time for the juicy work…

Clone the partition

There are 2 things we need for this:

  1. if (input file): /dev/sdd5
  2. of (output file): /dev/sdd6

In the terminal type:

sudo dd if=/dev/sdd5 of=/dev/sdd6

This will start copying your “if” to your “of”. It may take a while so be patient, there won’t be any output so keep the terminal open until you get your command prompt back again.

Cloning a drive

In the case of cloning a whole drive, the “if” here would have been /dev/sdd. The command would have been the same format but the “of” would have to be another drive or partition of capable size.

  • S Hodge

    Very helpful. This dd command is what I was missing in cloning my data.

    • http://www.sudo-juice.com/ Gareth Parmar

      Glad it helped.

  • Pingback: How to Clone a Hard Drive or Partition using GParted and DD | Wasamara

  • jb0nez

    What if I have bad sectors on the original? (It’s failing, with SMART errors, that’s why I want to clone) will dd skip those?

    • http://www.sudo-juice.com/ Gareth Parmar

      No, dd will copy them. have you tried repairing through the disk utility?

  • Josh Perlstein

    Very helpful, thank you so much!

    • 진오 강

      Exactly what I was going to say! Thanks xD

    • John

      Another way to monitor progress is do it with pv from the start, e.g.:

      pv -ptearb /dev/sdd5 | dd of=/dev/sdd6 bs=4M

      Also choosing an appropriate block size with dd can massively impact performance (gparted’s copy partition feature will automatically determine the optimum blocksize with a quick benchmark before it starts). You can actually skip dd entirely if you don’t want to specify a block size (this blog’s usage of dd is equivalent to cp, in fact), e.g.:

      pv -ptearb /dev/sdd5 > /dev/sdd6

      Final note is pv can’t generally determine the size of a block device and so can’t give accurate ETAs and progress. For that you can use blockdev:

      pv -ptearb /dev/sdd5 -s `blockdev –getsize64 /dev/sdd5` | dd of=/dev/sdd6 bs=4M

      The pv program is available in most modern distribution’s repositories. It is a tool specifically designed to pipe data and monitor and print progress, speed, and ETA.

      Still, gparted copy partition does all of the above. Since gparted is available in just about every repository these days, unless you want to waste a lot of time ignoring tools that others have written to solve tedious problems for us, it is the right tool for this job.

  • omi

    What happens if target/ output file is of a greater size ?

  • Viktor Kunz

    WTH!?
    When you open Gparted just select the partition you want to clone, right click on the selection and choose copy. In the disk selection list choose another disk you want to clone your partition to. Select the free (unallocated space), right click and choose paste. Then just apply the operations and that’s it! Your disk is being cloning now.

    Kind Regards

    • http://www.sudo-juice.com/ Gareth Parmar

      GParted’s role here is purely for prep. If you wanted to prepare the target in true Linux fashion learning the partitioning commands would be the best option… Completely removing GParted from the equation!

  • Bartłomiej Tomala

    great, but if you use gparted to create partition, why you don’t use option ‘copy partition to the clipboard’ and then ‘paste partition from the clipboard’, it easiest way and works for me

    • http://www.sudo-juice.com/ Gareth Parmar

      Looking at the gparted man page it doesn’t look very helpful. So if you were to try use it on a server I’m guessing it might be a bit more complicated. I like the Unix philosophy of writing a program that does one thing and does it well…. so I would prefer to create a partition via terminal using a different program to the one I’m cloning with (my next post I think!)

      • John

        [g]parted is a program that does one (well a few) things and does them well. It sounds more like you like the Unix philosophy of reinventing wheels. And I’m assuming you just didn’t realize gparted could copy partitions when you wrote this.