Actually it's not hard to clone a running Linux machine. In fact it doesn't really matter if the system is running or not.
1. Preparing the destination machine
1.1 Boot from live CD
If you do not have an OS installed or want to overwrite it you need to boot from a Live CD. I like to use Debian Live or Ubuntu but it doesn't really matter which distro you pick.
1.2 Partition the drive
Preferably I use gparted for this task since it is really easy to use. If you do not have a GUI in you Live environment I suggest cfdisk.
If you use BIOS it's a bit easier. You only need to create the root partition and set the boot-flag.
Format the disk as whatever filesystem you want to use.
For UEFI you need to create 2 partitions. One boot partition and one for the OS itself.
The first one should have about 256-512MB and have the boot and esp-Flag.
Format the first one as FAT32 and the second one as whatever filesystem you want to use.
mount /dev/sdX /mnt
A small step you should not forget.
2. Transfer files to the destination machine
You can transfer all the files from the source machine to the destination machine without making a copy. I will show you how.
2.1 Pull from source
You need to do this on the destination machine. Also you need to be able to log in as root on the source machine over SSH otherwise you probably don't have sufficient permissions to copy all the files.
ssh root@source-machine 'tar -cvp -C / --exclude=/example/path/ --one-file-system .' | tar -C /mnt/ -x
This command sends
tar -vp ... to the source. Because we didn't specify -f the output is sent to
stdout which is sent to the destination machine over SSH.
| tar -C /mnt/ -x we directly extract the archive to the destination disk.
2.2 Push to destination
Similar to the above command but you would need to run the command on the source machine. Also make sure your user has sufficient permissions to write to the destination. In other words: you need to be able to login as root.
tar -cvp -C / --exclude=/example/path/ --one-file-system . | ssh root@destination-machine 'tar -C /mnt/ -x'
2.3 tar explained
- -c: create new archive
- -v: verbose (show 'progress')
- -p: preserve permissions
- -C <directory>: cd into <directory> so /etc is also /etc in the archive no matter where you are.
- --exclude=<path>: exludes that file/directory
- --one-file-system: do not include other filesystems like /proc, /sys, /mnt, /media, /run and /dev. Also /home if it is a seperate partition. If you do not use this flag you need to exclude them using multiple --exlude.
- . is the current directory. Since we specified -C it is / in our case.
Alright, now back to the destination machine.
3.1 Bind mount the required directories
cd /mnt for i in /dev /dev/pts /proc /sys /run; do sudo mount --bind $i .$i; done
If you are using UEFI you also need to bind mount the EFI partition:
mkdir /mnt/boot/efi mount --bind /boot/efi /mnt/boot/efi
Finally we can chroot into the cloned filesystem.
When using UEFI you probably install the grub-efi-amd64-bin package:
apt install grub-efi-amd64-bin
Then install the bootloader to the right drive:
grub-install /dev/sdX update-grub
If you do not encounter any errors you should be good to go.
Since the partition UUID changed from the old system you also need to make sure to change the root partition in /etc/fstab. Otherwise the system won't boot.
find the UUID of the root partition and copy it.
open /etc/fstab Comment out the old root partition and add a new line like this:
UUID=<UUID> / <FS> noatime,errors=remount-ro 0 1
Make sure UUID is the UUID of the root partition.
FS is your chosen filesystem. Probably ext4.
After all that you can exit the chroot, reboot the machine and hope you did everything right.
Again, I'm not responsible for any possible damage on your computer and/or Operating System