Migrate Your Linux Installation Between Machines

It’s bound to happen that you have an awesome Linux setup … until the latest and greatest new processor comes out. But don’t worry, with a Linux installation you can move the things you want to keep to your new machine, and get a fresh installation on the things you don’t.

Since you’ll essentially be backing up then restoring your files, you’ll need to get your backup media ready. There are a couple of things you’ll want to keep in mind before you do so:

  • When backing up “normal” files, such as those from your home folder, you’ll typically be importing them into a new home folder. In this case, the ownership of those files will be set to your default permissions (which are your username as both the owner and group, e.g. “user:user”), But when migrating system files, you’ll need to make sure that the ownership remains intact.
  • Likewise with the permissions (to get a rundown on Linux permissions, take a look at MTE’s primer on the subject). This means you’ll need to back these files up to a Linux filesystem that supports the same ownership and permission structure.
  • It may be difficult to back these files and directories up while the machine is running. There may be programs or processes that are constantly updating them – some may even be locked. You may need to turn the machine of and boot using a Live CD or USB drive in order to get the data you need.

When moving your existing Linux set-up to new hardware, there are some directories you’ll want to include:

  • /home/ – Of course, you’ll want to make sure you take your working files (documents, e-mail, media, and all the application settings you spent so long tweaking). Fortunately, Linux keeps all these under the “/home/[user's name]/ directory for each user. You can just grab the whole “/home” directory to back this up for all users.
  • /etc/ – This directory contains global configurations. Configurations such as web server settings, service definitions (e.g. when that web server starts automatically at boot, it’s a file in “/etc/init.d/” that defines this), and some default configurations (i.e. if you install a new program, the settings that are created for you the first time you run it).
  • /root/ – Any files you create after a “sudo” command will be stored here by default. This directory doesn’t get used much in Ubuntu (the “sudo” command saves you from actually having to log in as root), but better safe than sorry.
  • /var/ – Data generated and maintained by system programs such as daemons is stored within the /var/ directory. Mail (if you run your own mail server) and server data (like web files for Apache) are two examples.

If you’re moving to a machine with the same architecture (i.e. “i386″ for 32-bit processors, or “amd64″ for 64-bit), you may also want to add the following… they might be empty, but then it doesn’t cost you anything!

  • /opt: Completely “optional” software may be installed here, e.g. applications packaged for your distribution that are not included in any official repositories.
  • /usr/local/: Custom software, particularly software you compile from source code, can be installed in this directory. Other types of programs are those that you simply extract from an archive (such as a “tar.gz” file).

You may look at the list of files above and think, “What about all my packages?!?” Don’t worry; there’s a way to grab a list of all those applications that will allow you to reinstall them all with a single command. This is a good way to save you from having to transfer tens of gigabytes of files to your backup media, only to have to transfer them back again.

With the above prepared, let’s move that Linux!

1. As noted above, it’s a good idea to shut your machine down and boot using a live distro.

2. Once you’re there, mount the hard drive containing the old Linux installation. We’ll call this path “/mount/path/to/install” below.

3. First, grab that list of all the packages you installed with the following command:

sudo dpkg --get-selections > /mount/path/to/install/home/[your user name]/packagelist.txt

This will place the text-file list of packages in your home directory, which we’ll back up right now.

4. The next step is to back up your files. It’s best to do this as root, since that’s who will own some of the files you’re backing up. By the same token, be sure that any settings or flags will be set to preserve both ownership and permissions. You can either use a command-line program like rsync or GUI program such as Backintime

5. Now, get Linux installed on your new machine.

6. Once the installation is done, reboot, but back into a live distro.

7. Now, restore all the files you backed up above. Again, you may need to be root to make sure all the permissions and ownership are preserved.

8. Almost done… run the following commands to restore all the packages you had previously in one shot:

sudo dpkg --set-selections < /mount/path/to/install/home/[your user name]/packagelist.txt

All the packages you had before are now set for installation, but not yet installed. The last step is the following command to install them:

sudo apt-get -u dselect-upgrade

Once that’s complete, you can boot into the Linux installation on your shiny new machine.