When a user is created in Linux, its default home directory is automatically created: “/home/username”. A user’s home directory contains all the individual user system files, including the user configuration files, installed programs, and other user-specific files.
This write-up will teach you about creating a user with a different home directory in Linux.
Note: To create a user with a default or different directory, you must be a root user or have sudo privileges.
How to Create a User With a Different Home Directory?
The useradd command is utilized to create the new user. The useradd command provides many built-in options; the “m” or “–create-home” options allow the users to create the dedicated home directory. For instance, to create a user named “User1” with the default directory, use the below command:
$ sudo adduser -m User1
The new user named “User1” is created with the default home directory. To check the user‘s home directory, use this command:
$ cat /etc/passwd | grep User1
The output shows that User1 is created with the default home directory, that is, “/home/User1”.
The “d” option allows creating a user with a different home directory instead of the default directory. The command to create the custom home directory instead of the default home directory, the general syntax is as follows:
$ sudo useradd -m -d <home-directory-name> <user-name>
The above command explanation is:
- useradd: This command creates the new user.
- m: Users can create dedicated home directories with this option.
- d: It allows the user to create a different home directory.
- home-directory-name: Put the different home directory name here.
- user-name: Change it with the username whose home directory is created.
To create a user named “User2” with a different home directory in Linux “/users/User2”, use the below command:
$ sudo useradd -m -d /users/User2 User2
The user “User2” is created with a different home directory named “/users/User2”.
To verify the home directory of the newly created user, use the below cat command:
$ cat /etc/passwd | grep User2
The different home directory “/users/User2” other than the default is created, as shown in the output.
We can now switch to the User2 home directory with the “su – <user-name>” command and can check the user’s home directory name with the “pwd” command:
$ sudo su - User2 $ pwd
The output shows that the user id switched to the home directory of the specified user User2 and its home directory is “/users/User2”.
How to Change the Home Directory of the Existing User?
We can change the default home directory of the existing user with the “usermod” command. The command to change the home directory of the user “User1” to a different directory, “/users/NewUser1” is given below:
$ sudo usermod -d users/NewUser1 User1
To verify that the home directory of the existing user “User1” is changed, use the below cat command:
$ cat /etc/passwd | grep User1
It shows that the home directory of the User1 is changed to /users/NewUser1.
To create a user with a different home directory in Linux, we can use the “d” option of the useradd command to specify the desired directory. The new user is created with the default home directory “/home/username” while using the “sudo useradd -m -d <home-directory-name> <user-name>” command will create the user with a different directory.
Moreover, to change the existing user’s home directory, we can use the “usermod” command, as discussed in the article.