create partition for home directory

How to Convert Home Directory Into Partition in Linux

By default, Linux places home directory and other system directories under root (/) folder, during installation. While it is fine most of the times, if your system crashes or the root partition is corrupted, then you will lose all your personal files that are present in home directory. So it is better to create a separate partition for root directory, during installation. This will protect your files & data in case of system reinstallation or crashes. In this article, we will learn how to convert home directory into partition in Linux.


How to Convert Home Directory Into Partition in Linux

We have assumed that you have accepted the default installation options during OS installation and your home directory and other system directories are present in root folder. We will move the home directory to a newly formatted disk partition, in the form of a USB drive. You can also do this using SSD drive.


1. Identify Newly Added Drive

First we will identify the newly added drive. By default, you will have a single hard drive /dev/sda with home folder and all system partitions are mounted on root (/) folder. You can get a list of all partitions using the following command.

$ df -Th

Next, we will plug in the USB drive, identified as /dev/sdb mounted at /media/ubuntu/usb. You can confirm this with the following command.

$ lsblk

The above label for USB drive may vary from system to system.


2. Create New Partition in Linux

Although we have added a new disk, in order to be able to add our home directory on this disk, we need to create a partition on it. You can confirm this with the following command.

$ sudo fdisk -l

You will see your disk /dev/sdb but no partitions associated with it. Run the following command to create new partition in it.

$ sudo fdisk /dev/sdb

When prompted, enter ‘n’ option to create new partition, and enter ‘p’ to create primary partition and type ‘1’ to enter partition number. For the next 2 prompts, hit enter to accept default starting and ending sectors for the partition.

To save all changes, enter ‘w’. To confirm changes, enter the following command.

$ sudo fdisk /dev/sdb

This time, when you are prompted, enter ‘p’ to display the details. You will see that a new partition /dev/sdb1 has been created.


3. Format New Partition in Linux

Next, we will format the new partition as ext4. Please note, we will format the partition /dev/sdb1 and not the disk /dev/sdb. You can do so with the following command.

$ sudo mkfs.ext4 /dev/sdb1


4. Mount New Partition in Linux

Next, we need to mount the new partition into our filesystem. First, we will create a mount point for this purpose.

$ sudo mkdir -p /srv/home

Then we will mount the partition on mountpoint with the following command.

$ sudo mount /dev/sdb1 /srv/home

For this purpose, run the following command.

$ sudo df -Th


5. Copy Home Directory Files to New Partition

Next, we need to copy all files & directories in /home to new partition using the following command.

$ sudo cp -aR /home/* /srv/home/

After all the files are copied, you can confirm it with the following command. It will list all files & directories originally present in /home.

$ ls -l /srv/home/


6. Create New Home Directory & Mount Drive

Next, we need to create a new directory where we will mount our home partition. We will rename our home directory to home.bak.

$ sudo mv /home /home.bak

Then we will create a new home directory.

$ sudo mkdir /home

Next we will unmount the new partition and mount it to newly created home directory.

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

You can confirm the change using following commands.

$ cd /home
$ ls -l *

You can verify that our filesystem is mounted on /home using df command.

$ sudo df -Th /dev/sdb1

Please note, the above configuration will last only as long you reboot system. In order to make this change permanent, follow the next step.


7. Permanent Partition Mount in Linux

To mount the new partition at every system boot, we will modify the /etc/fstab file. First, we will get the UUID of the partition using the following command.

$ sudo blkid /dev/sdb1

In the above command’s output, you will see UUID=…

Copy this value. Open /etc/fstab file in text editor.

$ sudo vim /etc/fstab 

Add the following line to the above file. Replace uid with the UUID you copied just above.

UUID=[ uid ]     /home	   ext4	   defaults	0	2

Save and close the file. Run the following command to mount all partitions.

$ sudo mount -a

Now your partition will be mounted on every boot. You can check it with the following command.

$ df  -h /dev/sdb1

In this article, we have learnt how to create a separate partition for home directory. Alternatively, during installation itself, you can configure home directory to have a separate partition.

Also read:

How to Check Remote Port Open in Linux
How to Generate GRUB Password in Linux
How to Boot Into Single User Mode in CentOS
How to Restore Deleted Tmp Directory in Linux
How to Change Linux Partition Label Names

Leave a Reply

Your email address will not be published.