Step-by-Step Guide to Moving the /home Directory on Ubuntu to a Second Drive

In Ubuntu, the default setup usually involves mounting the entire system on a single partition. While this is simple to set up, there are times when you may want to move the /home directory to a second drive, especially if you're running low on space or want better organisation. However, moving /home while the system is live presents some challenges, as it’s actively in use when you’re logged in.

This guide walks you through the steps to move your /home directory to a new drive or partition, ensuring a smooth transition without disrupting your current session.

Prerequisites

Before proceeding, make sure you’ve already partitioned and formatted the second drive you plan to use. If not, check out [How to Set Up a Multi-Drive SSD/HDD Ubuntu System] for instructions.

Steps to Move /home to a New Drive

1. Mount the New Partition

First, ensure the new partition is mounted. For this guide, we'll assume the new partition is mounted at /media/home. If it's mounted elsewhere, adjust the commands accordingly.

2. Begin by Switching to the Root User

To avoid any permission issues during the process, switch to the root user:

$ sudo su -

3. Synchronise the Old /home to the New Partition

We'll use rsync to copy the contents of your current /home directory to the new partition.

# rsync -aXS /home/. /media/home/.
  • -aXS ensures that all files, permissions, and special attributes are copied.
  • The trailing /. on both paths is critical to ensure correct file copying.

4. Rename the Current /home Directory

Before we mount the new partition to /home, we need to rename the current /home directory to keep a backup.

# mv /home /home_backup

5. Create a New Mount Point for /home

Now, create a fresh /home directory that will act as the mount point for the new partition.

# mkdir /home

6. Update the /etc/fstab File

The /etc/fstab file controls how partitions are mounted during boot. We need to update this file to point the new partition to /home.

  1. Open /etc/fstab in a text editor of your choice (e.g., nano, vim).
# nano /etc/fstab
  1. Look for the line that corresponds to your new partition. It might look like this:
UUID=7b9210d7-219f-4fc8-8501-677277a05f19 /home-tmp ext4 errors=remount-ro 0 2
  1. Change /home-tmp (or whatever the current mount point is) to /home:
UUID=7b9210d7-219f-4fc8-8501-677277a05f19 /home ext4 errors=remount-ro 0 2

7. Apply the Changes

Once /etc/fstab is updated, run the following command to apply the new mount points:

# mount -a

This should mount the new partition at /home. However, you might notice that df -h still shows the old mount point (e.g., /home-tmp) for the partition. This is because the partition is now mounted at both locations.

8. Unmount the Old Mount Point

We need to unmount the old mount point to avoid any conflicts:

# umount /home-tmp

Now, if you run df -h again, the partition should appear mounted as /home.

9. Log Out and Log Back In

The old /home is still available as /home_backup. This was done to ensure your current session could still access the necessary files. To complete the transition, log out of your session and log back in. Your system should now be using the new /home partition, with all user data intact.

10. Clean Up the Old /home Backup

Once you confirm everything is working correctly, you can remove the old /home backup:

$ sudo rm -rf /home_backup

Further Notes

  • Backup First: Before making any significant changes to your system, always ensure you have a backup of important data.
  • Multiple Scenarios: This guide focuses on moving /home to a separate drive, but it can be adapted for other situations, such as moving /home to a new partition on the same drive.
  • Custom Partitioning: If you prefer having more control over system organisation, consider setting up custom partitions for /home during the initial installation of Ubuntu.