Home Linux Administration How To Move Home Directory To New Partition Or Disk In Linux

How To Move Home Directory To New Partition Or Disk In Linux

By Karthick
Published: Updated: 29K views

If you are new to Linux or someone who plans to ditch windows and switch to Linux, then this article will be of great use to you. This step by step article will walk you through the procedure to move home directory to new partition or drive in Linux.

Introduction

Most beginners when installing Linux operating systems will not care to create different partitions.

The newbies will usually choose to stick with the default partition where all your file systems (/home, /var, /boot/, /opt, etc.) will be mounted under a single partition.

In fact, I also chose the default partition scheme in my early days as a newbie to Linux. When my operating system is crashed, I lost all my personal data sitting in /home directory. This is because I created only one partition for my whole disk and mounted everything in that partition.

Lesson learned! The best way to protect the data is to create a separate partition for the home directory. So If your machine crashes for any reason, the partition will not be affected unless the entire disk is faulty.

Along with separate partitions, it is a best practice to backup your data to an external drive. There are many backup tools available for Linux platform. We have published many guides on Linux Backup topic. Check the following links and choose the one that suits for your requirement.

For the purpose of this guide, I will be using Rocky Linux virtual machine running in Virtualbox.

Disclaimer: Do not try this on a production system. Transferring /home directory to new partition or drive may cause data loss and/or system boot failure. If you're newbie, I strongly advice you to seek for experts help. Neither the author nor OSTechNix team is responsible for any damages. You have been warned!

Get Partition Information

We can list disk partition details using various tools in Linux. In this guide, we will use lsblk and fdisk command.

To get the block device information using the lsblk command, run:

$ lsblk
Display Block Device Information
Display Block Device Information

In my machine I have a 50GB drive (sda). I opted for a default partitioning scheme, so there are two partitions (sda1, sda2) created by the OS installer.

NOTE: The drive naming scheme will vary depending upon the type of drive attached to your machine.

As stated earlier, we can also use the fdisk command to get the block device information.

$ sudo fdisk -l
Check Block Device Information Using Fdisk
Check Block Device Information Using Fdisk

To check under which partition the home directory is mounted, use the df command. In my case, it is mounted under /.

$ df -h /home

Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/rl-root   46G  2.8G   43G   7% /

Create New Partition

I have inserted one more drive with size 20GB into the virtualbox. The drive is known as sdb.

View Block Device Details
View Block Device Details

Let’s create a new partition from the new drive where the home partition will be mounted. I will be using the fdisk utility to create a partition.

$ sudo fdisk /dev/sdb

Type "n" to create a new partition.

Create New Partition
Create New Partition

It will prompt you to choose the partition type, partition number, first sector, and last sector. In the last sector, you can give input in terms of GB, PB, TB, etc. I am creating a 10 GB partition, so give input as +10G.

Enter Partition Type And Size
Enter Partition Type And Size

Type "w" and it will write the changes to the partition table.

Save Partition Changes
Save Partition Changes

Now verify if the new partition (i.e. sdb1 -> 10G) is created by listing the block device information with the lsblk command:

$ lsblk /dev/sdb
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb      8:16   0  20G  0 disk 
└─sdb1   8:17   0  10G  0 part  

Format and mount the Partition

Let’s format the newly created partition with the ext4 file system using mkfs command:

$ sudo mkfs.ext4 /dev/sdb1
Format Partition With Ext4 File System
Format Partition With Ext4 File System

Now the partition can be mounted to any directory as you wish. Since we are going to mount this directory temporarily, I am choosing /mnt directory.

$ sudo mkdir /mnt/home_move/
$ sudo mount /dev/sdb1 /mnt/home_move/
Mount Partition
Mount Partition

Move Home Directory To New Partition In Linux

Now all the directory and files from the home directory should be copied to the newly created partition mounted under /mnt/home_move.

I use the following rsync command to copy the data to the new partition. Replace the destination directory path name with your own.

$ rsync -av /home/* /mnt/home_move/

Once the data is copied to the new partition, do not remove the home directory yet. Just move it so in case of any mishaps you can revert back.

$ sudo mv /home /home_old
$ ls -l /home
ls: cannot access '/home': No such file or directory

Create a new /home directory to mount /dev/sdb1.

$ sudo mkdir /home

Remember the partition /dev/sdb1 is already mounted under /mnt/home_move/. It should be unmounted first, so it can be mounted under /home directory.

$ sudo umount /dev/sdb1
$ sudo mount /dev/sdb1 /home

Now the new partition /dev/sdb1 is successfully mounted under /home directory. You can run the lsblk and df command to verify it.

$ lsblk /dev/sdb
sdb           8:16   0   20G  0 disk 
└─sdb1        8:17   0   10G  0 part /home
$ df -h /home/
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1       9.8G   37M  9.3G   1% /home

Persistent Mount Using Fstab

Add entry to the fstab file so the newly created partitions will automount during system startup. Take the backup of the fstab file as a safety measure.

$ sudo cp /etc/fstab /etc/fstab_old

Open the fstab and add the following entries to the bottom of the fstab file. Replace /dev/sdb1 with your drive partition name.

/dev/sdb1 /home ext4 defaults 0 0
Fstab Entry
Fstab Entry

You can also use the UUID of a block device to mount the partition. To find the UUID for a block device, run the following commands.

$ lsblk -f /dev/sdb1
$ blkid | grep -i sdb1
Find UUID
Find UUID

Reboot the machine, log in back, and check where the /home directory is mounted by running the df command.

$ df -h /home/
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1       9.8G   37M  9.3G   1% /home

Partition Home Directory During OS Installation

You can skip all the steps discussed in previous sections if you have created a separate partition for home directory during the os installation. Every installer will have a step to configure the disk.

You will be prompted with an auto partition scheme or custom partition scheme. You should choose a custom partition scheme and create separate partitions.

Below is a sample image from my RHEL 8.4 custom partition where a separate home partition with 15GB is created.

Manual Partitioning
Manual Partitioning

Conclusion

In this article, we have discussed how to move the home directory from one partition to another partition in Linux. The procedure is same if you wanted to move the HOME directory to a new drive.

Just create a new partition, format with you filesystem of your choice, and mount it. After mounting the partition, move the data to newly created Move the data from old partition to new one. Finally, add the fstab entry to make it persistent.

If you have any suggestions or feedback, please post them in the comment section and we would be happy to respond back.

Featured image by Pixabay.

You May Also Like

1 comment

Carlos Daniel October 19, 2024 - 1:49 am

Thanks for this tutorial, Karthick. You only forgot to remove the /home_old directory in the end. Cheers

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More