How to Move Home Directory to New Partition or Disk in Linux?


In the Linux operating system, the home directory holds user-specific files, configurations, and personal data. As your usage grows, you might find the need to move your home directory to a new partition or disk to accommodate more space or optimize system performance. This can be a valuable solution, especially when dealing with limited disk space or utilizing separate storage devices.

In this blog post, we will explore the process of moving the home directory to a new partition or disk in Linux. We'll cover the necessary steps to prepare the new partition, discuss the home directory structure, and provide a detailed guide on moving the home directory while ensuring data integrity.

Understanding the Home Directory Structure

Before we delve into moving the home directory, let's take a moment to understand its structure and the purpose of key directories within it. Familiarizing yourself with the home directory structure will help ensure a smooth transition during the move.

Overview of the Home Directory

The home directory is typically located at /home/username, where "username" represents the name of the user. It serves as the central location for storing user-specific files, including documents, downloads, configurations, and personal settings.

Key Directories and their Purposes

Within the home directory, several key directories play essential roles 

  • Desktop  This directory contains files and shortcuts displayed on the user's desktop.

  • Documents  It holds user documents, such as text files, spreadsheets, and presentations.

  • Downloads  This directory is used for storing downloaded files from the internet.

  • Music, Pictures, Videos  These directories store media files related to music, pictures, and videos, respectively.

  • Public  The "Public" directory allows sharing files with other users on the system.

  • .config  This hidden directory contains configuration files for various applications.

  • .bashrc, .bash_profile  These hidden files store user-specific configurations for the Bash shell.

Understanding the purpose of these directories will help you ensure their successful migration when moving the home directory.

In the next section, we will cover the essential steps to prepare the new partition or disk for the home directory move.

Preparing the New Partition or Disk

Before we can proceed with moving the home directory to a new location, we need to prepare the new partition or disk where the home directory will be relocated. This involves creating the partition, formatting it with the appropriate filesystem, and mounting it in the system.

Creating a New Partition or Disk

To create a new partition, you can use various tools such as fdisk, parted, or gparted. The specific method may vary depending on your Linux distribution and the partitioning tool you prefer. Here's a general outline of the steps involved 

  • Identify the disk on which you want to create the partition (e.g., /dev/sdb).

  • Launch the partitioning tool (fdisk, parted, gparted) for the selected disk.

  • Create a new partition with the desired size and type.

  • Save the changes and exit the partitioning tool.

Follow the documentation or guides specific to your Linux distribution for detailed instructions on creating a new partition.

Formatting the Partition or Disk

After creating the new partition, you need to format it with a suitable filesystem. The choice of filesystem depends on your requirements and compatibility with the Linux distribution you're using. Common filesystems include ext4, XFS, and Btrfs. Here's an example of formatting the partition with ext4 

sudo mkfs.ext4 /dev/sdb1

Replace /dev/sdb1 with the appropriate partition identifier.

Mounting the New Partition or Disk

Once the partition is formatted, you can proceed to mount it in the system. Follow these steps −

  • Create a mount point directory where the new partition will be mounted. For example 

sudo mkdir /mnt/new_home
  • Mount the partition using the mount command 

sudo mount /dev/sdb1 /mnt/new_home

Replace /dev/sdb1 with the correct partition identifier, and /mnt/new_home with the desired mount point.

In the next section, we will discuss the preparations needed before moving the home directory.

Preparing for the Home Directory Move

Before actually moving the home directory to the new partition or disk, there are a few important preparations to make. These steps will help ensure a smooth and successful transfer of data while minimizing the risk of data loss.

Create a Backup of the Existing Home Directory

Before making any changes, it's essential to create a backup of your existing home directory. This serves as a safety net in case anything goes wrong during the move. You can use various backup methods, such as copying the entire home directory to an external storage device or using a backup tool like rsync.

For example, to create a backup of your home directory using rsync, you can use the following command 

rsync -av --progress /home/username /path/to/backup/directory

Replace /home/username with the path to your current home directory and /path/to/backup/directory with the destination where you want to store the backup.

Verify available Disk Space

Ensure that the new partition or disk has sufficient available disk space to accommodate the contents of your existing home directory. You can check the available disk space using the df command 

df -h /mnt/new_home

Replace /mnt/new_home with the mount point of the new partition or disk.

Make sure the available space is adequate to hold all the files and directories within your current home directory.

With the backup in place and the availability of disk space confirmed, you're ready to proceed with moving the home directory to the new partition or disk.

Moving the Home Directory

Moving the home directory involves copying its contents to the new partition or disk and updating the necessary configurations. Follow the steps below to successfully move the home directory to the new location.

Create a Temporary Mount Point

Before initiating the move, create a temporary mount point to facilitate the transfer of files. This temporary mount point will be used to mount the new partition or disk.

For example, create a temporary mount point directory named "temp_home" using the following command 

sudo mkdir /mnt/temp_home

Mount the New Partition or Disk

Mount the new partition or disk using the mount command. Use the following command −

sudo mount /dev/sdb1 /mnt/temp_home

Replace /dev/sdb1 with the correct partition identifier and /mnt/temp_home with the temporary mount point you created.

Copy the Contents of the Home Directory to the New Location

To copy the contents of the existing home directory to the new location, use the rsync command. It ensures a reliable and efficient transfer while preserving file attributes and permissions. Execute the following command 

sudo rsync -av --progress /home/username/ /mnt/temp_home/

Replace /home/username/ with the path to your existing home directory and /mnt/temp_home/ with the temporary mount point you created.

The trailing slashes in the source and destination paths are important to ensure the correct behavior of rsync.

Update the Filesystem Table

To ensure the new partition or disk is mounted as the home directory, you need to update the filesystem table (/etc/fstab). Open the file using a text editor, such as nano or vi, and add the following entry 

/dev/sdb1 /home ext4 defaults 0 2

Replace /dev/sdb1 with the correct partition identifier and ext4 with the appropriate filesystem type if different.

Save the changes and exit the editor.

Verifying the Home Directory Move and Troubleshooting

After moving the home directory to the new partition or disk, it's important to verify that the move was successful and address any potential issues that may arise. Follow the steps below to ensure a smooth transition and troubleshoot any problems that may occur.

Reboot the System

To activate the changes and ensure the new partition or disk is mounted correctly as the home directory, it's recommended to reboot the system. This will also help identify any issues related to the home directory move during the boot process.

sudo reboot

Verify the New Home Directory

After the system reboots, log in to your user account and ensure that the new home directory is being used. You can check this by running the following command 

echo $HOME

The output should display the path to the new home directory, such as /home/username.

Test User-specific Configurations and Applications

Verify that your user-specific configurations, settings, and applications are functioning as expected in the new home directory. Test various applications, check personalized settings, and ensure that everything is working properly.

If you encounter any issues with specific applications or configurations, it may be necessary to update the relevant paths or settings to reflect the new home directory location.

Restore from Backup (if needed)

If you encounter any critical issues or data loss during the home directory move, you can restore from the backup created earlier. Follow the backup restoration process specific to your chosen method, whether it's copying files manually or using a backup tool like rsync.

Conclusion

Moving the home directory to a new partition or disk in Linux allows for better organization, improved performance, and efficient disk management. By following the step-by-step process outlined in this guide, you can successfully relocate your home directory while preserving your data and configurations.

Updated on: 09-Aug-2023

396 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements