Published on

How to move /home from Ubuntu to another partition securely

Authors
  • avatar
    Name
    Marcio Moreira Junior

Are you looking to optimize your Ubuntu installation by moving the /home directory to a separate partition? This can enhance performance, improve system organization, and facilitate easier backups. In this guide, we'll cover the necessary steps to securely relocate your /home directory without risking data loss or system instability. Join me on this informative journey as we explore best practices and critical command-line techniques.


Benefits of Moving /home

Moving the /home directory to a separate partition can provide several significant advantages:

  • Data Protection: Isolating user data ensures that system and application files remain untouched during OS upgrades or reinstalls.
  • Easier Backups: A distinct partition allows for more straightforward backup strategies that focus only on user files.
  • Performance Improvement: Depending on your file system, placing /home on a dedicated disk can lead to performance enhancements.

Preparing the New Partition

Before we begin the process, ensure that you have a new partition available for your /home directory. You can use the lsblk command to verify your disk and partition layout:

lsblk

Creating a New Partition

If needed, create a partition using tools like gparted or fdisk. For this example, we'll assume the new partition is /dev/sdb1. You'll want to format it to a suitable file system, commonly Ext4 for Ubuntu:

sudo mkfs.ext4 /dev/sdb1

Mounting the New Partition Temporarily

Next, create a temporary mount point and mount the new partition:

sudo mkdir /mnt/new_home
sudo mount /dev/sdb1 /mnt/new_home

Once mounted, you can copy data from the existing /home to the new location. It's crucial to ensure that no user is logged in during this process, as files may be in use. You can check active users with:

who

Copying Data Safely

To copy the data securely, use rsync, which preserves permissions, ownership, and timestamps:

sudo rsync -avh /home/ /mnt/new_home/

The -a flag ensures that all attributes are maintained. The expected output may look like:

sending incremental file list
created directory new_home
sent 1,199,480 bytes  received  278 bytes  438,703.75 bytes/sec
done

Updating the /etc/fstab File

Now that the data is copied, it’s time to inform the system about the new partition. Open the /etc/fstab file:

sudo nano /etc/fstab

Add the following line to mount the new /home partition at boot:

/dev/sdb1    /home    ext4    defaults    0    2

Be sure to replace /dev/sdb1 and the file system type if necessary.

Unmounting and Testing

Before unmounting, you might want to double-check the copied data:

ls -la /mnt/new_home

Once confirmed, unmount the temporary mount point:

sudo umount /mnt/new_home

Finally, remount all filesystems defined in /etc/fstab:

sudo mount -a

This command ensures that the new /home partition is active following a reboot.

Troubleshooting

If you encounter issues, check the /etc/fstab entries for typos or misconfigurations. The command below will help troubleshoot any mounting problems:

sudo dmesg | grep sdb

This will show outputs related to the /dev/sdb device, aiding in diagnosing any mounting errors.

Conclusion

Moving the /home directory to a different partition is an excellent way to segregate user data from system files, improving both backup strategies and overall system management. Following the steps outlined above, you can accomplish this task securely without compromising your data integrity. Always ensure to have a reliable backup before performing such operations, and enjoy the benefits of a well-organized Ubuntu system!