April 15, 2024

How to Change User Default Home Directory in Linux

When you create a new user on a Linux computer, a default home directory is created. It provides space to store personal files and customized settings. By default every user lands in their home directory when logging into a Linux system and default home directory is ‘/home/$USER’ except the root.

Sometimes it may be necessary to change a user’s default home directory to a different location (a separate mount point) on a Linux system due to application requirements or other factors.

In this tutorial, you will learn how to change the default home directory when adding a new user in Linux and also learn how to change the home directory of an existing user account in Linux.

Changing a User’s Home Directory in Linux

The usermod command is used to modify user’s information on a Linux system. It allows you to change various user attributes including home directory. To learn more about all the attributes, go to the usermod man page.

Syntax:
usermod -m -d [/Path/to/New_home_dir] username

Make a Note: Please replace with the actual path of the new directory, and username of yours.

Suppose we have an account named 'mwadmin' and we would like to change the home directory of the user to the following path '/webserver' from ‘/home/mwadmin’.

To do so, use the following usermod command.

# usermod -m -d /webserver mwadmin
  • -d : To change the user’s home directory
  • -m : The contents of the current home directory will be moved to the new home directory

Also, set the correct permissions as required shown below.

# chown -R mwadmin:mwadmin /webserver

You can verify the user’s home directory change by parsing the /etc/passwd file as shown below.

# cat /etc/passwd | grep -i mwadmin
Changing a User's Home Directory in Linux

Changing Default Home Directory When Adding New User

When creating a new user, useradd command creates the user’s home directory at /home/[username]/ by getting information from the ‘/etc/default/useradd’ file. However, you can specify the home directory using the ‘-d’ switch.

Syntax:
useradd -m -d [/Path/to/New_home_dir] username

Actual command:

# useradd -m -d /webserver mwadmin -c "Middleware Service Account"

Conclusion

In Linux, when you create a new user, the useradd command creates a home directory by default in ‘/home/$USER’. This default behavior can be changed by adding the ‘-d’ parameter to the userradd command. Similarly, the default home directory of an existing user can be changed with the help of the usermod command and the ‘-d’ flag.

Leave a Reply

%d bloggers like this: