Linux Commands

How to Change Home Directory Linux

When you create a new user in your Linux system, a home directory is created for them. The home directory contains the individual files for the particular user and can be accessed using the tilde (~). The home directory is the directory that opens when you log in to a Linux system.

Suppose you are looking for how to change your home directory in Linux. This guide details all the steps to follow. Let us dig in!

Understanding the Linux Home Directory

The home directory is the working space for a user in a Linux system, except the root. Each user has a home directory when you have multiple users in your system. The absolute path for the home directory is /home/[username]/. This absolute path is the default location for the home directory for all users except the root.

Above, we have verified that we are in the user’s home directory named kyletech.

There are different ways of accessing the home directory in Linux. Graphically, when you open files, clicking the Home option opens the Linux home directory for the logged-in user.

If you are in a different location and want to access the home directory on your command line, use any below commands. Verify your current location using the pwd command.

$ cd ~
$ cd
$ cd $Home

Suppose you have multiple users in your system. You can access the /home directory to view the available home groups using the command below.

$ cd /home

In the below output, we notice that accessing the home groups gives us the list of users in the system. For this case, we have two users and you can access the contents of their home directory provided you are root.

Note that the home directory is a subdirectory of your root directory, whereas the root directory is the topmost level in your system drive. The home directory is denoted using the tilde (~), while the root is denoted using the slash (/).

How to Change the Home Directory Linux

So far, we have discussed the Linux home directory and how to access it. Suppose you are uncomfortable with the current home directory or want to switch it to another location. You can achieve that provided you are root.

For this example, we will use a user named linuxhint. Let us verify the user’s home directory before we change it.

The current home directory is /home/linuxhint. Let us change it to a new directory named changed.

First, switch back to the root account.

Create the new directory that you want to use as the home directory. In our case, we will use changed/

$ sudo mkdir /home/changed

Note that you must be root to create a directory in /home or use the sudo keyword.

For the target user, assign them ownership of the newly created folder using the chown command.

$ sudo chown -R linuxhint:linuxhint /home/changed

With the ownership changed, we can now use the usermod command to change the home directory for user Linuxhint from /home/linuxhint to /home/changed/ with the -d option.

$ sudo usermod -d /home/changed linuxhint

Although the command above will change the home directory for the target user, it doesn’t move the existing contents. So, a better approach is to change the home directory and move the existing files in the current home directory to the new home directory using the -m flag. For that, use the command below.

$ sudo usermod -d -m /home/changed linuxhint

Once you execute the command, switch to the user and check their new home directory. You will notice it changed to the one we created.

That is how to switch the home directory in Linux.

Change Default Home Directory When Adding New User

When creating a new user, Linux automatically creates their home directory in the /home/[username]/. However, you can specify the home directory using the -d flag. Use the syntax below.

$ sudo useradd -m -d /home/[target-directory] username

Here is an example of creating a user named demo whose home directory is /home/test.

Conclusion

Linux creates a home directory when you create a new user. By default, the home directory is the user’s username in the /home. However, you can change this home directory using the usermod command. This post details everything about the Linux home directory and how to change it.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.