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


Introduction

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. In this article, we will discuss how to change a user's default home directory on a Linux system.

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)

Changing the Default 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.

Use 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"

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

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 −

$ 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/directory:/bin/bash
  • Change the value after the five 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".As you can see, the home directory has been successfully changed to "/new/home/directory".

Move 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 −

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

This command will move all files and directories 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.

It is important to note that 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.

Conclusion

In this article, we have discussed how to change a user's default home directory on a Linux system. We have seen two methods: using the usermod command and editing the /etc/passwd file. Both methods are effective and can be used depending on your specific system requirements and preferences. We also discussed how to move user files to the new home directory using the mv command. Always remember to be careful when making changes to system files, as a mistake could cause problems for your system. Be sure to back up important files before making changes, and test changes thoroughly before deploying them to a production system.

Updated on: 01-Nov-2023

48K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements