This step-by-step article will guide you through the process of moving your home directory to a new partition or drive in Linux if you are new to Linux or want to abandon Windows and switch to Linux.
introduce
Most beginners don’t care about creating different partitions when installing a Linux operating system.
Beginners will often choose to use the default partition, and all your file systems (/home, /var, /boot/, /opt, etc.) will be installed in a single partition.
In fact, I chose the default partition scheme in my early days as a Linux novice, and when my operating system crashed, I lost all my personal data in the /home directory because I only created one partition for the entire disk and installed everything in that partition.
The best way to protect your data is to create a separate partition for the primary directory, so if your machine crashes for any reason, the partition won’t be affected unless the entire disk fails.
In addition to a separate partition, the best practice is to back up your data to an external drive, and the Linux platform has a number of backup tools available.
For the purposes of this tutorial, I’ll use the Rocky Linux VIRTUAL machine running in Virtualbox.
Disclaimer: Do not do this on a production system. Moving /home to a new partition or drive may result in data loss and/or system startup failure. If you are new to this, I strongly recommend seeking expert help.
Getting partition information
We can use various tools in Linux to list the details of disk partitions, and in this tutorial we will use the LSBLK and fdisk commands.
To obtain block device information LSBLK using the command, run:
$ lsblk
Copy the code
On my machine, I had a 50GB drive (SDA) and I chose the default partition scheme, so the operating system installer created two partitions (SDA1, SDA2).
Note: The drive naming scheme will vary depending on the type of drive connected to the machine.
As mentioned earlier, we can also use the fdisk command to get block device information.
$ sudo fdisk -l
Copy the code
To check under which partition the home directory is installed, use the df command, which in my case is under /.
$df -h /home Filesystem Size Used Avail Use% Mounted on /dev/mapper/rl-root 46G 2.8g 43G 7%/Copy the code
Creating a new partition
I have inserted another 20GB drive in VirtualBox called SDB.
Let’s create a new partition from the new drive that will mount the primary partition, and I’ll use the fdisk utility to create a partition.
$ sudo fdisk /dev/sdb
Copy the code
Type n to create a new partition.
It will prompt you to select the partition type, partition number, first sector, and last sector. In the last sector, you can enter GB, PB, TB, etc. I am creating a partition of 10 GB, so enter +10G.
Type “W” and it writes the changes to the partition table.
LSBLK now verifies that a new partition has been created (i.e., SDB1 -> 10G) by listing block device information using the following command:
$df -h /home Filesystem Size Used Avail Use% Mounted on /dev/mapper/rl-root 46G 2.8g 43G 7% /Copy the code
Format and mount partitions
Let’s format the newly created partition with the ext4 filesystem using the MKFS command:
$ sudo mkfs.ext4 /dev/sdb1
Copy the code
You can now mount the partition to any directory you want, and since we’re going to mount this directory temporarily, I chose the/MNT directory.
$ sudo mkdir /mnt/home_move/
$ sudo mount /dev/sdb1 /mnt/home_move/
Copy the code
In Linux, move the home directory to the new partition
All directories and files in the home directory should now be copied to install at/MNT /home_move.
I used the following rsync command to copy the data to the new partition, replacing the target directory pathname with your own.
$rsync -av/home/*/mnt/home_move/
Copy the code
After copying data to the new partition, don’t delete the home directory yet, just move it so you can restore it in case anything happens.
$ sudo mv /home /home_old
Copy the code
$ ls -l /home
ls: cannot access '/home': No such file or directory
Copy the code
Create a new /home directory to mount /dev/sdb1.
$ sudo mkdir /home
Copy the code
Remember that partition /dev/sdb1 is already mounted at/MNT /home_move/. You should uninstall it first so that you can mount it to the /home directory.
$ sudo umount /dev/sdb1
$ sudo mount /dev/sdb1 /home
Copy the code
Now that the new partition /dev/sdb1 has been successfully mounted in the /home directory, you can verify it by running the lsblkanddf command.
$LSBLK /dev/sdb SDB 8 0 0 0 0 disk ├ ─sdb1 8 0 0 0 0 0 part /homeCopy the code
$df -h /home/filesystem Size Used Avail Use% Mounted on /dev/sdb1 9.8g 37M 9.3g 1% /homeCopy the code
Use Fstab for persistent mount
Add entries to the file, fstab so that the newly created partition will be automatically mounted during system startup, using the backup fstab of the file as a security measure.
$ sudo cp /etc/fstab /etc/fstab_old
Copy the code
Open fstab and add the following entry to the bottom of the fstab file, replacing /dev/sdb1 with your drive partition name.
/dev/sdb1 /home ext4 defaults 0 0
Copy the code
You can also mount partitions using the UUID of the block device. To find the UUID of a block device, run the following command.
$ lsblk -f /dev/sdb1
$ blkid | grep -i sdb1
Copy the code
Df restarts the machine, logs in again, and then checks the mount location of the /home directory by running the command.
$df -h /home/filesystem Size Used Avail Use% Mounted on /dev/sdb1 9.8g 37M 9.3g 1% /homeCopy the code
The partition home directory during operating system installation
If you created a separate partition for the primary directory during the operating system installation, you can skip all the steps discussed in the previous section. Each installer has a step to configure the disk.
You will be prompted to use either an automatic partition scheme or a custom partition scheme. You should choose the custom partition scheme and create a separate partition.
Here is an example image from my RHEL 8.4 custom partition, where a single 15GB primary partition was created.
conclusion
In this article, we discussed how to move the HOME directory from one partition to another in Linux, and the process is the same if you want to move the HOME directory to a new drive.
Simply create a new partition, format it using the file system of your choice, mount it, after mounting the partition, move the data to the newly created one move the data from the old partition to the new one, and finally add the fstab entry to make it persistent.