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 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.

fosstechi

Recent Posts

How to List or Display all Cron Jobs in Linux

Cron is a command-line job scheduler available on Linux operating systems that allows users to…

1 year ago

How to Disable IPv6 on RHEL System

In computing, there are two types of IP addresses, IPv4 and IPv6. By default, IPv6…

1 year ago

How to View Contents of tar.gz File in Linux

A tarball is commonly known as a TAR file, short for tape archive, which is…

1 year ago

How to Create tar.gz File in Linux

A tar archive is a file that stores a collection of directories and files, also…

1 year ago

How to Identify LUN Mapped to VxVM Disk in Linux

On Linux, you can quickly find out how LUNs are mapped to the underlying OS…

1 year ago

How to List Available Package Updates in Ubuntu

Keeping your Ubuntu system up-to-date is critical to ensuring a system's stability and security. This…

1 year ago

This website uses cookies.