If you’ve ever wanted to completely clone your Ubuntu install, with all of the tweaks, files you’ve downloaded and changes you’ve made to it, there’s a fairly simple way to do this. This is great if you want a complete backup, or if you’re looking to move your system to a newer (read: bigger, faster, stronger) hard drive or even just to clone your install to other machines with the same hardware.
We’ll be using the terminal (Applications-> Accessories-> Terminal) and the dd command to do this. You’ll also need to have your second disk up and running when we get going. You can either have it installed and mounted internally or use an external disk enclosure and USB or Firewire. (Note: Doing this via USB 1 will be excruciatingly slow!)
You’ll also want to either be cloning your hard drive to one of the exact same size, or if you have a larger disk, make a partition of the same size on it and clone to that. Then, use an Ubuntu liveCD to change the partition size (System-> Administration-> Partition Editor). Lastly, you’ll need a LiveCD.
On to the good stuff. Got both disks plugged in? Good! Now you’ll need to figure out which disk you are copying from and which disk you are copying too. In your terminal, type:
df -h
Look first for the partition that’s mounted at root, or ‘/’. Here’s what my root partition looks like.
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 71G 46G 22G 68% /
If you’re using a SATA drive it will appear like that. IDE should be /dev/hda1. See that slash below the Mounted on? That’s the root drive.
Now you’ve got to locate the drive you’re copying too. The same df -h command will do the trick. Look for another disk mounted on /dev/****. If you’re not sure what you’re looking for, first run the df -h command without your second disk mounted. Then plug the 2nd disk in (be sure to shut down if you’re doing this inside your machine and not via USB or FireWire) and run the df -h command again. The newest partition that appears is what you’re looking for!
So if your current root partition is /dev/sda1 and the partition you’re going to copy to is /dev/sdb1 (a USB mounted drive) here’s the command you’ll need to type in your terminal:
sudo dd if=/dev/sda1 of=/dev/sdb1
Replace with the correct paths for your drives if they differ. It’s going to take a while, so grab a book or start up a movie. Maybe go to bed.
Once it’s complete, you’ve got yourself a brand new copy of your current Ubuntu install. You’re not quite done yet though. Now you’ve got to install Grub on your new disk so you can boot from it. Make sure your new disk is attached to your machine and your old disk is unplugged and boot into the Ubuntu LiveCD.
Once your machine boots up, open up a terminal session and type:
sudo grub
Grub will launch and give you the grub> prompt. Here, type:
find /boot/grub/stage1
You should see something come back that looks like hd(0,0). Jot that down, you’ll need it in a second.
Now, still in the grub> prompt, type:
root hd(0,0)
You’ll put in whatever result you go above - it may be different than hd(0,0).
Once that completes, type:
setup (hd0)
Even if you got a result that differs from hd(0,0) above.
Type:
quit
And you’re out of grub. Restart your machine, removing the LiveCD and you should be up and running on your new hard drive. You may also encounter a problem on your first boot where the system will try to scan your hard drive for bad sectors. If that fails, you’ll find yourself in a root terminal session. Just type:
fsck
Let the disk check finish and you should be good to go.
Technorati Tags: ubuntu, grub, mbr, clone hard drive, attack of the . . . nevermind, how to, copy drives, technology, computers, geeks















January 23rd, 2008 at 1:22 pm
The dd command is one of those amazingly simple things about Gnu/Linux. To be clear, what you are doing here is cloning a ~partition~. It is also possible to clone your entire disk, all partitions, byte for byte, using almost the same procedure with the dd command. I have two identical disks and this is how I do routine backups. If I lose one disk, I just pop in the other as if nothing ever happened. This is also somewhat simpler in that grub and everything will already be installed. The replacement drive will be a turn-key, plug-in replacement for the original drive. I believe it is also possible to clone from a smaller drive to a larger drive without problems and without first making an identically sized partition, though I haven’t tried this.
On my setup (identical 320GB SATA drives) the command is
dd if=/dev/sda of=/dev/sdb bs=32768
The last part of the command is a bytesize option that drastically speeds up the copy. It only took 2 hrs to copy the drive that had about 260GB used. Amazing.
This is also a great thing to do before a distro upgrade. If the upgrade doesn’t work out, just pop in the old drive and start over.
Cheers’!
January 25th, 2008 at 5:10 pm
This method copies the whole content of the disk, doesn’t it? Including all the configuration files, drivers, etc? So if for example the new computer uses another graphic card than the system will be screwed?
January 29th, 2008 at 8:17 pm
In my use of your instructions above, it didn’t work exactly the way you mentioned. Instead of returning
hd(0,0)
it returned:
(hd0,0)
so I had to issue the root command like this:
root (hd0,0)
and everything worked fine. Thanks for the quick tutorial!
January 30th, 2008 at 12:28 am
Here’s what I don’t understand: with dd you’re making a _clone_ of a bootable drive, so why do you need to install grub as an additional step? In other words, if you are copying / and under / is /boot/grub, why do you need a separate step for grub?
February 3rd, 2008 at 4:32 pm
Update: I still don’t understand why, but without the grub step (root (hdx,y), stetup (hdx)) grub will throw an Error 22. Not sure what this step does, as it did not appear to modify any of the files in /boot/grub on the cloned drive…
February 19th, 2008 at 5:00 pm
I tried the procedure above and after about 30 minutes got this response:
dd: reading `/dev/sda1′: Input/output error
Bus error
unfortunately, I did not know that the disks needed to be inactive (the source disk is on a production server running MySql queries.
Now, we are getting these I/O errors on the source disk intermittently even after running fsck sessions.
Is it possible that the “dd” could have corrupted the source disk or is this a coincidence.
February 21st, 2008 at 1:05 pm
@mark:
Chances are your disk has some bad blocks or other errors on it already, and you’ve just never noticed it, until dd attempted to traverse the entire device for the data copy. Check via smartctl, whether SMART has reported anything. ie. smartctl -a -d ata /dev/sda; if not run a short test on the drive to see if anything comes up. You can do some additional testing by checking drive performance - read/write speeds etc. Also look through logs and see if there’s any complaints about ’sda’, such as unreadable sectors, drive seek/ready/complete warnings, and so on : grep sda /var/log/messages*. Depending on the drive’s age (smartctl -a -d ata /dev/sda | grep “Power_On_Hours”), its possible that it’s just starting to show signs of age.
The author failed to mention to use additional arguments for ‘dd’, which are ‘noerror’ and ’sync’. ‘noerror’ causes dd to continue even when it encounters an error, and ’sync’ pads input blocks with nul’s - in other words, if an error was encountered - it will get padded with nul’s, so that the disk image size does not vary from actual partition size in the end.
So really what you should be doing is ‘dd if=/dev/sda1 of=/dev/sdb1 conv=noerror,sync’
Enjoy.
June 6th, 2008 at 10:53 am
It seems not to be a good idea to use dd because of it knows nothing about cloning content.
The right way is :
1. Prepare new drive partition (fdisk, mkfs)
2. Mount prepared partition to /mnt (something like that):
mount /dev/sda2 /mnt
3. Now, clone our filesystem (man tar)
tar -c –one-file-system “/” | tar -x -C “/mnt/”
4. Least, install grub as described in article
The benefit of that way is You don’t need to boot from another media.
June 6th, 2008 at 12:18 pm
@PnD!: As the article explains in the first paragraph - the aim was to show a way to clone your entire setup, not make a copy of your root fs.
There is nothing wrong with cloning your machine by using dd. In many cases it will be faster than creating tarballs. Besides this, what if the disk has a shitton of errors? Your tar won’t be very happy about this, and you’ll probably be giving ‘dd’ another try (with ‘noerror’ args).
October 15th, 2008 at 12:48 am
Thank you for writing an excellent article. I was able to clone my drive with no problems.
February 1st, 2009 at 11:02 am
An excellent article. There is one thing that I don;t understand… Why is my installation now ~22Gb in the drivee when the previous ubuntu installation was only ~10,8Gb?
Did I do something wrong?
Disk usage analyzer shows that only 10,8 are being used by the filesystem, but also says “(used: 21.9Gb available:33.6)”.
Do you have any idea as to what is going on?
November 1st, 2009 at 12:27 am
Hi all,
I’ve used a different method but essential it was the same (I’ve used clonezilla instead of dd), the one than was not usefully for me it was the grub part, because I had to do extra steps in order to fix it out…. I left you following link as well to recovery grub….it helps to me
http://ubuntuforums.org/showthread.php?t=224351
November 21st, 2009 at 1:27 pm
I had a similar problem like Nickolas, that the used space in the second disk is much bigger then that in the first disk.(the difference seems like the capacity of the disks)
To solve this problem, you can just lunch the GParted in Live CD, and let it check your problem disk. It will fix the wrong mount of used space.
December 5th, 2009 at 6:37 pm
hey, I just tried this and I got stucked in something really stupid, sorry for being a noob but, where should I launch the terminal? do I start a live cd session? then from the graphic interface I launch terminal and I’m user ubuntu@ubuntu:$ but non of the given commands will work, then I change user to root@ubuntu:# and I try again, and nothing, if I have to change launch the terminal in the user screen, then how do I exactly do it, I’ve looked all around and I don’t have a clue… please a little HELP, it is my only pc and I’m blocked if I can’t do this… thanks
December 16th, 2009 at 2:55 am
jkmonto:
I’m going through the procedure now, and since no one else has responded, maybe I can help.
You open a terminal using your regular account. “Application => Accessories => Terminal” means look for an Applications (top of your screen) click on that, click on Accessories, then click on terminal. Only ONE command needs to be run as root, and that is the DD command. The rest is how you determine what disks you are going to duplicate from and to. So the command “sudo dd if=/dev/sda1 of=/dev/sdb1″ executes as root (sudo) and duplicates the sda1 disk on sdb1.
You MUST know which disk you are duplicating from and to. If you get it wrong, at best you will waste a lot of time. At worst, you’ll completely crash your system. If you aren’t sure, post the output from the df -h command and ask for more help.
Best of luck,
Thomas
December 16th, 2009 at 3:02 am
As soneone else noted, /dev/sda1 is the first partition on the first hard drive. If you have a second partition, it will not be copied. However the Unix/Linux files system in MUCH smarter than the PC system. If you want to duplicate an entire disk, regardless of partitions and including the grub, use:
dd if=/dev/sda of=/dev/sdb
ALSO, there is a buffer size parameter which can be added. The larger the buffer size, the faster the duplication will finish. (I’ve seen speed increases up to 400% or less than 1/4 of the time without the parameter.) Try:
dd if=/dev/sda of=/dev/sdb bs=512k
December 23rd, 2009 at 8:17 am
Great post, cloned and replaced a failing disk on my laptop. Works perfectly. Thanks!
I wonder if it’d be this easy for a windows system.
January 18th, 2010 at 1:22 am
HI,
Ive just run this to clone my older IDE drive (38gb) to my 500gb SATA drive (which i made a new partition the same size as the older drive).
My question is this, when do i know the cloning has finished? The terminal window will allow me to type something in, but no other details have come up since typing “sudo dd if=/dev/sda1 of=/dev/sdb1
Any ideas?
BTW, that was the second typing of the about command in terminal. When i first did it (before making the same size partition on my new drive) the terminal came back within a few seconds with
1024441+0 records in
1024440+0 records out
524513280 bytes (525mb) copied 18.7347s, 28.0mb
January 18th, 2010 at 1:12 pm
Thanks! Awsome and simple.
January 31st, 2010 at 1:02 am
Great tutorial and great follow up comments…
One question though, can I clone my 500gb SATA dve to a 320gb SATA drive?
Reason is I have a 320gb drive lying around and don’t particularly want to go out and buy another 500gb HDD.
My installation (if this is the correct term) is only 11Gb so it would seem a waste to go for another 500 Gb HDD.
March 17th, 2010 at 2:52 am
THIS TUTORIAL=FAIL
First of all, Ubuntu Server 8.04 does not load a live distro, only an installer, therefore there is no way to simply jump into a shell and install the grub boot loader as you claim.
May 6th, 2010 at 10:31 pm
These instructions are nonsense and don’t work. they’re old, don’t ake into account Grub or UUIDs, and are nonsense in any event.
May 6th, 2010 at 10:32 pm
These instructions are nonsense and don’t work. They’re old, don’t take into account Grub or UUIDs, and are nonsense in any event.
May 17th, 2010 at 8:26 am
I just pop in the other as if nothing ever happened. This is also somewhat simpler in that grub and everything will already be installed. The replacement drive will be a turn-key, plug-in replacement for the original drive.
June 5th, 2010 at 11:55 am
HAving just typed in the command above and watched my HD light come on and stay on- is there any way I can monitor the progress of the copy/clone?
June 30th, 2010 at 6:23 am
@ Ralph:
I had the same problem with dd. No progress check whatsoever. Found a solution. Maybe not the best there is but it works and I can check the progress.
1. Start cloning a partition or disc
2. Open a new terminal en start ‘top’ to get the ‘PID’ from ‘dd’
3. Quit ‘top’ and type the following command: “kill -USR1 PID” where the PID is the PID you got when you used the ‘top’ command. Press Return.
4. Type the command: “watch -n 10 kill -USR1 PID”
5. If you go back now to the terminal where you started ‘dd’ you will get a status report every ten seconds. If you want to get a status report every 15 seconds just change 10 tot 15.
Hope this helps you out like it helped me out.