How to Change the Default Home Directory of a User on Linux?

When you create a user on a Linux system, that user is given a default home directory. This home directory is a personal space where the user can store their files and settings. Sometimes it may be necessary to change a user's default home directory on a Linux system. This could be because you want to provide a different location for the user's files or because you want to change the user's name and update the home directory accordingly.

Prerequisites

Before you get started, there are some prerequisites you should be aware of

  • You must have root access on the Linux system. This means that you must be logged in as the root user or have superuser privileges.

  • The new home directory must already exist on the system. This means that the new home directory must be created before a user's default home directory can be changed. (You can use mkdir command to create a directory)

Methods to Change Home Directory

There are two methods you can use to change a user's default home directory on a Linux system: using the usermod command or editing the /etc/passwd file. Let's discuss both methods in detail.

Method 1 Using the usermod Command

The usermod command is a utility that allows you to modify user accounts on a Linux system. You can use it to change various user attributes, including the home directory.

To change a user's default home directory using the usermod command, follow these steps

  • Open a terminal window and log into the Linux system as root.

  • Type the following command to change the default home directory of a user named "john"

$ sudo usermod -d /new/home/directory john
  • Press Enter. The default home directory for user "john" will be changed to "/new/home/directory".

To verify that the home directory has been changed, you can use the following command

$ grep john /etc/passwd

This command will display the entry for user "john" in the /etc/passwd file. The output will look like this

john:x:1000:1000:John:/new/home/directory:/bin/bash

As you can see, the home directory has been successfully changed to "/new/home/directory".

Method 2 Editing the /etc/passwd File

The "/etc/passwd" file is a system file that stores user account information on a Linux system. You can edit this file to change a user's default home directory.

To change a user's default home directory by editing the "/etc/passwd" file, follow these steps

  • Open a terminal window and log into the Linux system as root.

  • Type the following command to open the "/etc/passwd" file in a text editor

$ sudo nano /etc/passwd
  • Find the line that corresponds to the user whose home directory you want to change. The line will look like this

john:x:1000:1000:John:/home/john:/bin/bash
  • Change the value after the fifth colon (:) to your new home directory. For instance

john:x:1000:1000:John:/new/home/directory:/bin/bash
  • Press "Ctrl+X" to exit the text editor.

  • When prompted to save changes, press Y, then press Enter.

To verify that the home directory has been changed, you can use the following command

$ grep john /etc/passwd

This command will display the entry for user "john" in the "/etc/passwd" file. The output will look like this

john:x:1000:1000:John:/new/home/directory:/bin/bash

As you can see, the home directory has been successfully changed to "/new/home/directory".

Moving User Files to New Home Directory

If you've changed a user's default home directory, you may also want to move the user's files to the new home directory. To do this, you can use the following commands

$ sudo mv /old/home/directory/* /new/home/directory/
$ sudo mv /old/home/directory/.* /new/home/directory/

These commands will move all files and directories (including hidden files) from the old home directory to the new home directory. Make sure you replace "/old/home/directory" with the actual path to the old home directory and "/new/home/directory" with the actual path to the new home directory.

Important: This command will overwrite any existing files in the new home directory with the same names as the files being moved. Make sure you back up any important files in your new home directory before running this command.

Additional Considerations

When changing a user's home directory, consider the following

  • Permissions: Ensure the new home directory has the correct ownership and permissions for the user.

  • Symbolic Links: Update any symbolic links that point to the old home directory.

  • User Login: The user should log out and log back in for the changes to take full effect.

Comparison of Methods

Method Advantages Disadvantages
usermod command Safe, standard approach; validates input Requires remembering command syntax
Editing /etc/passwd Direct control; useful for bulk changes Risk of file corruption; no validation

Conclusion

Changing a user's default home directory on Linux can be accomplished using either the usermod command or by editing the /etc/passwd file directly. The usermod command is generally recommended as it's safer and validates input. Always remember to back up important files and test changes in a non-production environment first.

Updated on: 2026-03-17T09:01:38+05:30

65K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements