Copy a Hard Drive to Any VM Anywhere with SSH

Have you ever wanted to import an OVA into services like Linode, Vultr, AWS, and others? Have you ever wanted to get your VM off of one of those services? Well let me introduce the magic of SSH, dd, and Gzip.

 

To get started, you need to boot Linux on both VM’s and have root access. To make it easy, you can boot Ubuntu live CD’s on them. The machine you are copying to (the new one) needs to be accessible on the open internet.

 

On the live CD on the new machine, type the following:

sudo -i
passwd

And input a somewhat secure password.

 

Next, edit the sshd config to allow root login:

nano /etc/ssh/sshd_config

Then set

PermitRootLogin yes

Save and exit with control+x.

 

Next, start the SSH daemon:

systemctl start sshd

 

Now you should be able to connect to the new machine. On the old machine, try to ssh in to the box with the root user and theĀ  password you set.

 

Next, you need to check the drive labels for both the old and the new machine. fdisk -l will help you with that. Finally, on the old machine, run this long command, and it will begin the disk transfer:

 

dd if=/dev/OldDrive | gzip -c | ssh root@NewIP 'gunzip - | dd of=/dev/NewDrive status=progress'

You can also clone the old disk to an img file, which can then be read to other disks later. This can be done by modifying the above command slightly to be:

 

dd if=/dev/OldDrive | gzip -c | ssh NewComputer@NewIP 'gunzip - | dd of=/path/somewhere/clone.img status=progress'

Final example, if you are cloning to a file you should be able to ssh from the new machine to the old one like this:

 

ssh root@OldIP 'dd if=/dev/OldDrive | gzip -c' | gunzup - | dd of=/path/somewhere/clone.img status=progress'



About: Ryan Parker

I currently work as a system administrator for the local school district. I also have a side job helping small to medium business with their technology. One of my hobbies is building out infrastructures for myself, friends, and clients.


Leave a Reply

Your email address will not be published. Required fields are marked *