How to migrate HOME directory in Linux

If you find a reason to move the Linux home directory from one drive to another, Jack Wallen has the step

I’ve had a few cases where it was necessary to move the HOME directory to a Linux desktop or server. This may be due to space or security issues. Either way, migrating the directory containing all user files and directories from one drive to another might seem like a rather daunting task. Although it is a bit of a long process, it is not that difficult.

That said, let’s see how it’s done.

TO SEE: 40+ open source and Linux terms you need to know (TechRepublic Premium)

What you will need

For this to work, you will need a running instance of Linux with at least two disks installed. I’ll demo on Ubuntu Server 20.04, but it should work no matter what distro you’re using.

How to locate destination drive

The first thing we need to do is locate the drive that will house the HOME directories. You need to make sure you know the exact name of the drive. To do this, connect to the server (or desktop) and run the command:

lsblk

You should not only see the name of the drive, but also the mount point of the target drive (Figure A).

Figure A

Our target drive, /dev/sdb, is listed and ready to move.
Our target drive, /dev/sdb, is listed and ready to move.

My disk is already formatted. If this is a new, unformatted disk, you will first need to create a partition with:

sudo fdisk /dev/sdb

Type n, for a new partition, then type p to create a primary partition. Then type 1 to specify the partition number. Press Enter to accept the default values ​​for the next two questions (first sector/last sector). You should now see /dev/sdb1 with the command:

sudo fdisk /dev/sdb

Type p to print the partition information.

Next, you will need to partition the drive to ext4 with:

sudo mkfs.ext4 /dev/sdb1

Let’s create a new directory to serve as a mount point for the new partition with:

sudo mkdir -p /data/home

Mount the drive:

sudo mount /dev/sdb1 /data/home

How to copy files from /home to new drive

Copy everything from /home to the new drive with:

sudo cp -aR /home/* /data/home

Rename /home with:

sudo mv /home /home.bak

Create a new home directory with:

sudo mkdir /home

Unmount /dev/sdb1 with:

sudo umount /dev/sdb1

Remount /dev/sdb1, but this time in the newly created /home directory with:

sudo mount /dev/sdb1 /home

How to permanently mount the new partition

Now we need to make sure that the new partition is mounted, even if the machine is rebooted. For this, we will create an entry in /etc/fstab. Before doing this, we need to find the UUID of the partition with the command:

sudo blkid /dev/sdb1

You should see something like UUID=”13557fad-d203-4448-991b-c8011907dc1d” in the output (Figure B).

Figure B

The output of the blkid command shows the UUID of our partition.
The output of the blkid command shows the UUID of our partition.

Open fstab to modify it with the command:

sudo nano /etc/fstab

At the bottom of the file (Figure C), add something like this:

UUID=ID /home /ext4 defaults 0 2

Where ID is the UUID of the new partition.

Figure C

Added a mount entry for our new partition in /etc/fstab.
Added a mount entry for our new partition in /etc/fstab.

Save and close the file. Remount the partition with:

sudo mount -a

And voila, you have successfully moved the /home directory to its own drive. You no longer have to worry about users consuming all the space on your Linux OS server drive.

Be sure to practice this on non-production machines to ensure you’ve mastered the process, before trying it out on a machine needed for your workflow.

Subscribe to TechRepublic How to make technology work on YouTube for all the latest tech tips for professionals from Jack Wallen.

Comments are closed.