1. Overview
In this tutorial, we’re going to see how to change the default home directory of a user on Linux. By default, it’s /home/{username}.
We’ll show how to change it for a new user, as well as move the existing content to a new location.
2. Creating a User
Let’s begin by creating a user called baeldung, in the default way, using the useradd command:
$ sudo useradd -m baeldung
We used -m so that useradd would create the home directory at the default location if it doesn’t already exist.
Note that we needed sudo since we’ll require root permissions to create and modify the user accounts configuration.
We can also create a user and set the location for the home directory at the same time by adding the argument -d:
$ sudo useradd -m -d /home/baeldung baeldung
3. Changing the Default Home Directory
So, we’ve created the baeldung user, and its home directory is /home/baeldung.
Now we’re going to change the user’s home directory to /usr/baeldung with usermod -d:
$ sudo usermod -d /usr/baeldung baeldung
The usermod command modifies a user account information. We specify the desired home directory with the -d parameter.
Let’s check if we correctly changed the home directory:
$ cd ~
$ pwd
/usr/baeldung
Actually, there’s a problem with the usermod command that we’ve just run.
We could have some files already created in our previous home folder. So, we’ll waste space in disk if we forget to delete or move those files in the future.
Let’s modify our command then to also move the existing content to the new location with -m:
sudo usermod -m -d /usr/baeldung baeldung
We can now see that our files for the user baeldung have been moved to /usr/baeldung and the old directory deleted.
4. Conclusion
In this tutorial, we’ve introduced two commands, useradd and usermod, which we can use to add and then modify the user and their home directory. We saw how to move the user’s old home directory content in the process.