How to Create Home Directory for Existing User in Linux

Written by: Bobbin Zachariah   |   Last updated: August 6, 2023

By default when you create a user in Linux, users default home directory is created under /home. If you noticed on Ubuntu and Debian derivated distributions, the useradd command won't create a home directory by default.

Let's think of a situation where you have already created a user but the home directory is missing. In this tutorial, I will show you how to create a default home directory for an existing user in Linux.

Create default home directory for existing user

Here I am using Ubuntu 22.04 and going to create a user named ' bob' using useradd command:

$ sudo useradd bob

Useradd command has added an entry home directory in /etc/passwd file

$ grep bob /etc/passwd
bob:x:1003:1003::/home/bob:/bin/sh

If I try to login as the user using su -, it shows that it's logging in with Home=/. This means the user home directory is not created.

$ su - bob
Password:
No directory, logging in with HOME=/

In Linux, a user's default home directory is /home. To create a default home directory use mkhomedir_helper command. The mkhomedir_helper command can create the home directory with the correct permissions and even populate the home directory with default files (like .bashrc, .bash_profile, etc.) which are often sourced from /etc/skel.

Make sure to run mkhomedir_helper command as root or user with sudo access.

$ sudo mkhomedir_helper bob
create home directory for existing user

The command creates a home directory named "/home/bob" and copies default files.

$ ls -al /home/bob
total 20
drwxr-xr-x 2 bob  bob  4096 Jun  1 02:26 .
drwxr-xr-x 5 root root 4096 Jun  1 02:26 ..
-rw-r--r-- 1 bob  bob   220 Jun  1 02:26 .bash_logout
-rw-r--r-- 1 bob  bob  3771 Jun  1 02:26 .bashrc
-rw-r--r-- 1 bob  bob   807 Jun  1 02:26 .profile

For a graphical environment (such as GNOME or XFCE ), if you are missing subdirectories in the home directory, the user needs to log out and log in back.

When the user login the first time all subdirectories such as Pictures, Documents, Videos, and Downloads folders can be created in the home directory.

Another method is to delete the user and create a new user using -m or --create-home option.

The following command creates a home folder (-m)  and  set the specified home directory (-d) as the value for the new user's login:

$ sudo useradd -m -d /home/bob01 bob01

Manually Perform

Alternatively, you can manually create the home directory and set appropriate permissions and ownership.

Create a home directory:

Note: Before you create a home directory, you should verify that the user indeed exists on the system.

mkdir /home/tom

Set the Appropriate Permissions and Ownership:

chown tom:tom /home/tom
chmod 755 /home/tom

This sets the user tom as the owner of the directory. Also sets the permissions of the /home/tom directory such that the owner has read, write, and execute permissions, while the group and others only have read and execute permissions.

Copy Default Files:

cp -a /etc/skel/. /home/tom/

Systems usually have a /etc/skel directory that contains default configuration files for new user accounts. Here we copy the contents of this directory to the new home directory /home/tom. Finally, you can verify by checking ls -la /home/tom.

Note: If the user's record ( ie /etc/passwd file) is not reflected with the new home directory you can use usermod -d /home/tom tom command.

To conclude, If you are a Ubuntu fan you should be now using adduser command, it's recommended by Debian. If you have an existing user, now you should be able to add default directory.

Thanks for reading and please drop your suggestions on the below comment section.

Related Read: How to Delete a User with Home Directory on Ubuntu

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin Zachariah is an experienced Linux engineer who has been supporting infrastructure for many companies. He specializes in Shell scripting, AWS Cloud, JavaScript, and Nodejs. He has qualified Master’s degree in computer science. He holds Red Hat Certified Engineer (RHCE) certification and RedHat Enable Sysadmin.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment