This brief guide explains how to create home directory for existing user in Linux and Unix-like operating systems using mkhomedir_helper
command.
Introduction
As you know already, we can create a new user in Linux and Unix-like systems using adduser
command, right? Yes, that's right! When we add a new user, the home directory for the respective user is automatically created by default.
There is also another command to create users in Linux. It is called useradd
.
After I created a few users using useradd
command in Alpine Linux, I noticed that the $HOME directories for the users are not created. I am not aware of it before.
After reading the useradd
command's man pages, I came to realize that I should have included -m
(or --create-home
) option to create new users with $HOME directory like below:
$ sudo useradd -m user_name
Or,
$ sudo useradd --create-home user_name
But, I forgot to include this option and went on to create a few new users. If you are anything like me, don't delete the existing users and recreate them with -m
option.
You can easily create home directory for an existing user in Linux using mkhomedir_helper
command, without deleting and recreating the user.
Create Home Directory For Existing User In Linux
The mkhomedir_helper
is a helper program for the pam_mkhomedir
module. The pam_mkhomedir
PAM module will create a user's home directory if it does not exist when the session begins.
First, this module creates the home directories and then populates them with contents of the specified skel directory. The default value of umask
is 0022
and the default value of path-to-skel is /etc/skel
.
Allow me to show you an example to explain how to create the home directory for an existing user with the help of mkhomedir_helper
command.
First, let us create a new user named ostechnix using useradd
command:
$ useradd ostechnix
Set password to the user:
$ passwd ostechnix
Now switch to the new user:
$ su - ostechnix
You will encounter with the following error message:
su: warning: cannot change directory to /home/ostechnix: No such file or directory
As you see in the above output, the home directory for the user ostechnix does not exists, hence this error.
Let us verify it by looking into the /home
directory:
$ ls -l /home/ total 0 drwx------ 1 vagrant vagrant 72 Dec 15 13:19 vagrant
Well, it is true that there is no home directory for the user ostechnix.
So let us create the home directory for the existing user (i.e. ostechnix in our case) with mkhomedir_helper
command.
To do so, first log out from the user ostechnix if you already logged in:
$ exit
And run the following command to create home directory for the user called ostechnix:
$ sudo mkhomedir_helper ostechnix
Please note that you must run the above command as root
user or different sudo
user.
Now switch to the user ostechnix:
$ su - ostechnix
This time you will not get any warning message, because the home directory for the user called ostechnix has been created with mkhomedir_helper
command.
[ostechnix@archlinux ~]$ pwd
/home/ostechnix
For more details, refer man pages:
$ man mkhomedir_helper
You know now how to create home directories after creating the users. This can be useful when you forgot to include -m
option while creating users with useradd
command.
Instead of using useradd
, you can use adduser
command which will create user's home directories automatically without any options.
Related read:
- How To Move Home Directory To New Partition Or Disk In Linux
- Add, Delete And Grant Sudo Privileges To Users In Alpine Linux
- Add, Delete And Grant Sudo Privileges To Users In Arch Linux
- Add, Delete And Grant Sudo Privileges To Users In Fedora
- Add, Delete And Grant Sudo Privileges To Users In CentOS
- Add, Delete And Grant Sudo Privileges To Users In Ubuntu
Featured image by FreeCliparts from Pixabay.
1 comment
Thanks for the tip about using `mkhomedir_helper`.