Skip to Content

3 ways to change user home directory in Linux

This article is part of the following series.

 

In Linux, the home directory is the default directory for user files. It’s also where your user profile is stored. If you need to change your home directory for any reason, there are a few ways to do it.

In this blog post, we will discuss three methods that you can use to change your home directory in Linux. 

Get Your Free Linux training!

Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!
  • change home directory for the existing user with usermod command in Linux
  • change the default home directory for a new user from file /etc/default/useradd in Linux
  • change home directory for new user with useradd command in Linux

 

Procedure to change home directory for the existing user in Linux

  • Open the terminal.
  • Switch to root account with sudo su – or su – command
  • Type usermod -d new_directory username and press Enter. Replace new_directory with the actual path of the new home directory, and replace username with the actual username of the user you want to change the home directory for.
  • The output will show that the user’s home directory has been changed.
  • log out and log back
  • check the user’s new home directory with the command echo $HOME

 

change home directory for the existing user with Usermod command in Linux

We can change the home directory for an existing user in Linux by using usermod command. To do this, simply run the following command:

usermod -d /new/home/directory username

This will change the home directory for the user username to /new/home/directory.

Keep in mind that you will need to log out and log back in for the changes to take effect.

For example:

usermod -d /export/john john

This will change the home directory for the user “john” to “/export/john”.

The -m option in usermod command is used to move the content of the old home directory to the new home directory.

If you don’t specify the -m option, then the content of the old home directory will not be moved to the new home directory.

It is recommended that you use the -m option when changing the home directory for an existing user.

For example:

usermod -d /export/john -m john

This will change the home directory for the user “john” to “/export/john” and will also move the content of the old home directory to the new home directory.

The usermod command can also be used to change a user’s shell, home directory, user group, and more.

Change default home directory for new users in Linux

The default home directory for users in Linux can be changed in /etc/default/useradd file.

You need to edit this file and change the value of HOME variable as follows:

HOME=/new/default/home/directory

Save and close the file.

The next time you create a new user, it will use “/new/default/home/directory” as home directory. 

Keep in mind that this change will not affect existing users.

#cat /etc/default/useradd 
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=30
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes

You can also use useradd -D to change the default home directory with the following command in Linux.

useradd -D -b /new/default/home/directory

This command will write the new setting to file /etc/default/useradd.

Change home directory for new user with useradd command in Linux

As we said above, there is a default home directory for Linux users. When you create a new user with useradd command, the default home directory will be used.

If you want to change the home directory for a new user in Linux, you need to use -d option as follows:

useradd -d /new/home/directory username

This will create a new user “username” with “/new/home/directory” as the home directory.

How to check user home directory in Linux

You can check user home directory in Linux using the following command:

grep username /etc/passwd | cut -d: -f6

This will print the home directory for the user “username”.

You can also use getent command as follows to display information about a particular user from password database:

getent passwd username | cut -d: -f6

This will also print the home directory for the user “username”.

Conclusion

In this blog post, we have discussed three methods that you can use to change your home directory in Linux. We hope this has been helpful. If you have any questions or feedback, please leave a comment below.

Thank you for reading this blog post!