How to migrate the HOME directory on 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 instances in which relocating the HOME directory on either a Linux desktop or server was necessary. This could be because of space or security issues. Either way, it might seem like a rather daunting task to migrate the directory containing all user files and directories from one drive to another. Although it is a bit of a lengthy process, it’s not all that difficult.
With that said, let’s see how it’s done.
SEE: 40+ open source and Linux terms you need to know (TechRepublic Premium)
What you’ll need
To make this work, you’ll need a running instance of Linux with at least two drives installed. I’ll be demonstrating on Ubuntu Server 20.04, but this should work no matter the distribution you’re using.
How to locate the destination drive
The first thing we need to do is locate the drive that will house the HOME directories. You need to make certain you know the exact name of the drive. To do that, log into the server (or desktop) and issue the command:
lsblk
You should not only see the drive name but the mount point for the target drive (Figure A).
Figure A
My drive is already formatted. If this is a new, unformatted drive, you’ll need to first create a partition with:
sudo fdisk /dev/sdb
Type n, for a new partition and then type p to create a primary partition. Following that, type 1 to specify the partition number. Hit Enter to accept the defaults 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 out the information for the partition.
Next, you’ll need to partition the drive as 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 the files from /home to the 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, only this time to the newly-created /home directory with:
sudo mount /dev/sdb1 /home
How to permanently mount the new partition
We now need to make sure the new partition is mounted, even if the machine is rebooted. For that, we’ll create an entry in /etc/fstab. Before we do that, we have 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
Open fstab for editing with the command:
sudo nano /etc/fstab
At the bottom of the file (Figure C), add something like the following:
UUID=ID /home /ext4 defaults 0 2
Where ID is the UUID of the new partition.
Figure C
Save and close the file. Remount the partition with:
sudo mount -a
And there you have it, you’ve successfully moved the /home directory to its own drive. You no longer need to worry that users will consume all of the space on your Linux OS server drive.
Do make sure to practice this on non-production machines to make sure you’ve got the process down, before trying it on a machine necessary to your workflow.
Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.
For all the latest Technology News Click Here
For the latest news and updates, follow us on Google News.