Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
This article explores 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 moving the home directory, it's essential to understand its structure and the purpose of key directories within it. 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 Contains files and shortcuts displayed on the user's desktop.
Documents Holds user documents, such as text files, spreadsheets, and presentations.
Downloads Used for storing downloaded files from the internet.
Music, Pictures, Videos Store media files related to music, pictures, and videos, respectively.
Public Allows sharing files with other users on the system.
.config Hidden directory containing configuration files for various applications.
.bashrc, .bash_profile Hidden files storing user-specific configurations for the Bash shell.
Preparing the New Partition or Disk
Before moving the home directory, prepare the new partition or disk where it will be relocated. This involves creating the partition, formatting it with an appropriate filesystem, and mounting it in the system.
Creating a New Partition
To create a new partition, you can use tools such as fdisk, parted, or gparted. Here's a general outline using fdisk
# List available disks sudo fdisk -l # Open fdisk for the target disk (replace /dev/sdb with your disk) sudo fdisk /dev/sdb # Inside fdisk: # n - create new partition # p - primary partition # 1 - partition number # Enter - accept default start sector # Enter - accept default end sector (or specify size like +50G) # w - write changes and exit
Formatting the Partition
After creating the partition, format it with a suitable filesystem. ext4 is commonly recommended for Linux home directories
sudo mkfs.ext4 /dev/sdb1
Creating and Using a Temporary Mount Point
Create a temporary mount point to access the new partition
sudo mkdir /mnt/new_home sudo mount /dev/sdb1 /mnt/new_home
Moving the Home Directory
The actual move involves copying data, updating configurations, and ensuring proper permissions. Follow these steps carefully to avoid data loss.
Step 1: Create a Backup
Before making any changes, create a backup of your existing home directory
sudo rsync -av --progress /home/username /backup/location/
Step 2: Copy Home Directory Contents
Use rsync to copy the home directory contents to the new partition while preserving permissions and attributes
sudo rsync -av --progress /home/username/ /mnt/new_home/
Note: The trailing slashes are important for correct rsync behavior.
Step 3: Update the Filesystem Table (/etc/fstab)
Edit /etc/fstab to ensure the new partition mounts as /home at boot
sudo nano /etc/fstab
Add the following line (replace /dev/sdb1 with your partition)
/dev/sdb1 /home ext4 defaults 0 2
Step 4: Rename the Old Home Directory
Rename the old home directory as a precaution before the final switch
sudo mv /home /home.old sudo mkdir /home
Verification and Testing
After completing the move, verify that everything works correctly.
Reboot and Test
Reboot the system to activate the changes
sudo reboot
After rebooting, verify the new home directory is active
echo $HOME df -h /home ls -la /home/username
Testing Applications and Configurations
Test your applications and verify that user-specific configurations work correctly. Check that
Desktop files and shortcuts appear correctly
Application settings are preserved
File permissions are intact
Hidden configuration files (.bashrc, .config) function properly
Troubleshooting Common Issues
| Problem | Cause | Solution |
|---|---|---|
| Permission errors | Incorrect file ownership | Run sudo chown -R username:username /home/username
|
| Applications not loading settings | Missing hidden config files | Ensure rsync -a was used to preserve all files |
| Home directory not mounting | Incorrect fstab entry | Check and correct the /etc/fstab entry |
| Boot failure | Corrupted fstab | Boot from live USB and restore original fstab |
Cleanup
Once you've verified everything works correctly, clean up the temporary files and directories
# Remove the old home directory backup (after thorough testing) sudo rm -rf /home.old # Remove temporary mount points sudo umount /mnt/new_home sudo rmdir /mnt/new_home
Conclusion
Moving the home directory to a new partition or disk in Linux provides better disk space management and improved system organization. By following this step-by-step process ? preparing the new partition, copying data with rsync, updating /etc/fstab, and thorough testing ? you can successfully relocate your home directory while preserving all user data and configurations.
