How to Move Your Linux home Directory to Another Drive

Linux terminal on stylized laptopFatmawati Achmad Zaenuri / Shutterstock

Do you want to move your Linux personal folder to another drive? Here's a simple, step-by-step way to do it that should work with any distribution. Moving your personal folder means that you can reinstall Linux without having to worry about your personal files.

Why keep your personal file separate?

If you are setting up a new machine or adding a hard disk to an existing machine, you may want to have your home directory on a different drive than the default one.

An increasingly popular setup for modern personal computers is to have a medium-sized computer. Hard disk (SSD) containing your operating system and a larger Hybrid semiconductor disk (SSHD) or traditional hard disk (HD) as the main storage for data. Or you can have a single traditional hard drive in your system and you have added a new hard drive for increased storage. Whatever your reasons, here's a quick and easy overview of moving your personal directory.

By the way, if you are installing a Linux system from scratch, you will probably have the option of creating a separate home directory in the setup program of your Linux distribution. As a general rule, all you have to do is use the partitioning options, create a separate partition and mount it to "/ home". However, if you have already installed a Linux distribution, you can use these instructions to move your current home directory to a new location without losing anything or to reinstall your operating system.

Now, before you start, go and make a backup.

RELATED, RELATED, RELATED: How to back up your Linux system

Identify the reader

If you have just installed a drive on a Linux computer, or if you have installed Linux on one of the drives of a new multi-drive computer and you have restarted, there is little evidence that the new drive is even present.

The fdisk command will be list disks and their partitions for us.

sudo fdisk -l

sudo fdisk -l in a terminal window

Scroll through the output until you have identified the new drive. The first drive is named / dev / sda, the second is / dev / sdb, and so on, with the last letter increasing each time. So / dev / sde would be the fifth hard drive in the system.

In this example, the new drive is the second drive to be installed on the system. So we need to look for an entry for / dev / sdb.

Fdisk output in a terminal window with / dev / sdb highlighted

/ dev / sdb is highlighted above. You will notice that there is no line describing a partition on it. It's a brand new record, so it will not have one yet. We must create the partition. We can do it using fdisk. If your hard disk is not / dev / sdb, be sure to substitute the / dev / sdb for the actual drive ID of your new hard drive in the command.

sudo fdisk / dev / sdb

sudo fdisk / dev / sdb in a terminal window

When fdisk prompts you to enter a command, press the letter p. This prints the partition table for the hard disk. We know that it will not, but we will get useful information about the reader. This gives us a good opportunity to make sure that the drive for which we are going to create a score is the one with which we intended to work.

It tells us that the drive is a 1 TB drive, which is what we expect from this test machine. We will proceed.

Create a partition

Press the letter n for a new partition, and then p for a primary partition. When the partition number is requested, press the number 1.

We will create a single partition for the entire disk. Therefore, when you are prompted to enter the first sector, you can press Enter to accept the default. The last sector will be asked and Enter will accept the default value.

to create a partition with fdisk in a terminal window

Although fdisk confirms that it has created a 1 TB Linux partition, which corresponds to partition number 1, nothing has changed on the hard drive yet. As long as you have not given fdisk the command to write the changes to the drive, it is not affected. Once you're sure you're happy with our choices, press the letter w to write the changes to the player.

write the fdisk changes to the drive in a terminal window

The score was written in / dev / sdb. Let's see what just happened. We will use fdisk again on / dev / sdb.

sudo fdisk / dev / sdb

sudo fdisk / dev / sdb in a terminal window

Press the letter p to print this partition table. A partition is now listed for the drive. Since this was the first partition of this drive, it is named / dev / sdb1. A second partition would call / dev / sdb2, and so on.

We do not want to make any changes to the score, so press the letter q to quit.

Create a file system on the partition

We need to create a file system on the partition. This is easily done with the mkfs command. Note that you must include the partition number in the order. Be sure to type / dev / sdb1 (the partition) and not / dev / sdb (the drive).

sudo mkfs -t ext4 / dev / sdb1

sudo mkfs -t ext4 / dev / sdb1 in a terminal window

The file system will be created for you and you will be returned to the command prompt.

Output of the mkfs command in a terminal window

Fit the new disc

To use the new drive, we must mount the partition on a mount point of the file system. In fact, to be perfectly accurate, we do not mount either the drive or the partition, we mount the file system on the partition by grafting it to the file system tree of your system.

The dot / mnt is a place as well as any other. This is only a temporary mount point that allows us to copy data to the new drive. We will use the mount command for mount the file system on the first partition on / dev / sdb, at / mnt.

sudo mount / dev / sdb1 / mnt

sudo mount / dev / sdb1 / mnt in a terminal window

If all goes well, you will be returned to the command line without error message. Let's see if we can change directories for our newly mounted file system.

cd / mnt

cd / mnt in a terminal window

Yes we can. Let's see what's here.

ls -ahl

ls -ahl in a terminal window

We are in our new file system. The default directory "lost + found" is not required, so we can delete it.

sudo rm -rf lost + found

sudo rm -rf lost + found in a terminal window

Copy your personal file

We need to copy everything from the old home directory to the newly mounted file system. Using the r (recursive) and p (keep) options will ensure everyone subdirectories are copied and that file properties, permissions, and other attributes are preserved.

sudo cp -rp / home / * / mnt

sudo cp -rp / home / * / mnt in a terminal window

When copying is complete, use ls to look around and make sure your data is in the expected location in the new file system. In other words, if / mnt was your home directory, is everything present and correct?


ls in a terminal window

You will probably want to be a little more careful than we were on the test machine on which this article was studied. As a safety net, we will rename and keep your old / home directory until you are satisfied that you can safely delete it.

sudo mv / home / home.orig

sudo mv / home /home.orig in a terminal window

And we will create a new empty home directory.

sudo mkdir / home

sudo mkdir / home in a terminal window

We will use this new empty home directory as a mount point for our file system on the new hard drive. We need to unmount it from / mnt and go back to / home. Note that the umount command does not have an "n" after the "m".

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

sudo umount / dev / sdb1 in a terminal window

RELATED, RELATED, RELATED: The structure of the Linux directory, explained

Test your new home directory

Let's see what are the attributes of the / dev / sdb1 partition:

df / dev / sdb1

df / dev / sdb1 in a terminal window

The name of the file system, the size of the partition, and the space used and available on it, as well as its mounting location are indicated. That's now our / home directory. This means that we should be able to reference it exactly as we could with the old / home directory.

If we move to any point of the file system, we should be able to go back to / home using the ~ tilde shortcut.

cd /
cd ~

cd / and other commands in a terminal window to test the home directory

cd / home
cd dave
ls -a

cd / home and other commands to test the / home directory in a terminal window

We can browse the file system and get back to / home using explicit commands and using the ~ shortcut. The folders, files and point files we expected were all present. Everything is beautiful.

If something was missing, we could copy it out of the /home.orig directory, to which we always have access to the root of the file system. But everything is fine.

Now, we need to have / dev / sdb1 mounted automatically each time you start your computer.

Editing fstab

The "fstab" file contains the description of the file systems to be mounted during system startup. Before making any changes, we will make a backup copy on which we can return in case of problems.

sudo cp / etc / fstab /etc/fstab.orig

sudo cp / etc / fstab /etc/fstab.orig in a terminal window

We can now edit the fstab file.

sudo gedit / etc / fstab

sudo gedit .etc.fstab in a terminal window

You must add a line at the bottom of the file to mount our new / home directory. If your drive and partition IDs are different from those used in this example, replace them with the / dev / sdb1 code presented here.

Type the name of the partition at the beginning of the line, and then press the Tab key.
Type the mount point, / home and press Tab.
Type the description of the ext4 file system, and then press the Tab key.
Type the default values ​​for the mount options and press the Tab key.
Type the number 0 for the file system dump option and press the Tab key.
Type the number 0 for the file system check option.

Use gedit to edit the fstab file

Save the fstab file.

Restart your system

We need to restart to verify that everything is running as expected and that you have a seamless connection to your new / home directory.

If this is not the case, you still have the safety net of your initial / home directory and your fstab file which can be restored if necessary. Because of the precautions we have taken (copying the / home directory and fstab files), you can easily restore the state of your system before booting.

sudo is restarting now

sudo now restarts in a terminal window

RELATED, RELATED, RELATED: How to restart or stop Linux using the command line

Final verifications

When your system restarts, just check that your / home directory is on your new hard drive and that your system did not (miraculously) use the old / home directory.

df / dev / sdb1

df / dev / sdb1 in a terminal window

Great, it's mounted on / home. Mission accomplished.

Once you are absolutely sure that you no longer need the security copy of your old / home directory, you can delete it:

cd /
sudo rm -rf home.orig /

sudo rm -rf home.orig / in a terminal window

And of course, if you realize something has not been copied from the old / home to your new / home, you can recover it from the backup you made before you started.

The sweetness of the home

Now that you have separated your / home directory from the rest of the operating system partition, you can reinstall your operating system and your data will not be changed. All you need to do is edit the fstab file to mount your second disk on / home.

And since all your point files are in your / home directory, when you launch your various applications, they will find all your settings, preferences and data.

This avoids relocations and the risks of upgrades.

Leave a Reply

Your email address will not be published. Required fields are marked *

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