By Wael

Posted :

How to assign a home directory , using useradd

useradd is a tool which can be used to create a new user , and to update the default options , used when creating a new user .

One of the options , that can be set by using useradd , is the user home directory . The options that can be used , to set a user home directory , are :

assign a home directory using useradd tutorial
The -m or --create-home options

By default , and unless the -m or --create-home options are used , useradd will not create the home directory , for the user .

The -m or --create-home options , do not create the base directory , specified by using the HOME variable , or the base directory specified by using the -b BASE-DIR or --base-dir BASE_DIR options .

The -m or --create-home options , do not also create , any directory , beside the last directory , when used with the -d HOME_DIR or --home HOME_DIR options .

When the -m or --create-home options are used , the files found in the skeleton directory , will be copied , to the created user directory .

@root:~$ useradd -D
# Display , the default options 
# used by useradd 
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/sh
SKEL=/etc/skel
CREATE_MAIL_SPOOL=no
# The skeleton directory location , is 
# /etc/skel 

@root:~$ ls -al /etc/skel/
# List the content of the skeleton directory 
total 20
drwxr-xr-x   2 root root 4096 Apr 19 16:19 .
drwxr-xr-x 119 root root 4096 May  7 14:59 ..
-rw-r--r--   1 root root  220 Apr 18  2019 .bash_logout
-rw-r--r--   1 root root 3526 Apr 18  2019 .bashrc
-rw-r--r--   1 root root  807 Apr 18  2019 .profile
# It contains , the .bash_logout , .bashrc
# and .profile files . 
# These files are copied to the user 
# home directory , when the 
# -m or --create-home option are used . 
# The -m or --create-home options , create 
# the user home directory .
If no options are used ?

If no options are used , useradd will check if the HOME variable in /etc/default/useradd is defined .

If defined , useradd will append the username , to the value specified in HOME , to specify the user HOME directory .

If not defined , useradd will append the username , to /home , to specify the user HOME directory .

@root:~$ cat /etc/default/useradd | grep HOME
# grep the value of the variable HOME , in 
#/etc/default/useradd . The variable 
# Home is commented out .
# HOME=/home

@root:~$ useradd user-one
# Create a user named user-one . 
# No options are used , and the
# HOME variable in /etc/default/useradd 
# is commented out , as such 
# useradd , will append the username , to :
# /home , to specify the user home directory . 

@root:~$ cat /etc/passwd | grep user-one
# grep user-one from /etc/passwd
user-one:x:1001:1001::/home/user-one:/bin/sh
# user-one home directory is set to /home/user-one

@root:~$ ls /home/ | grep user-one
# gives no result , since when 
# the user was created , 
# the -m option was not used , 
# as such , the user home 
# directory is not created .

The value for HOME can be set by using , useradd -D -b HOME or useradd -D --base-dir HOME . The path specified in HOME , must exist , it will not be created .

@root:~$ useradd -D --base-dir /invalid_path
# Set the home variable inside 
# /etc/default/useradd to /invalid_path
# A trailing backslash is permitted .

@root:~$ cat /etc/default/useradd | grep HOME
# HOME=/home
HOME=/invalid_path
# The value for HOME is set to /invalid_path
# The HOME value , is the value for the 
# base directory , if no options are used . 

@root:~$ useradd -m user-two
# The -m option is used . 
# The -m option creates the user home 
# directory , but does not create the base 
# directory .
# In this case , the base directory is 
# set to /invalid_path , which 
# does not exist , as such 
# the useradd command fails .
useradd: cannot create directory /invalid_path/user-two

@root:~$ useradd user-two
# Create user-two , without creating its 
# home directory . 

@root:~$ cat /etc/passwd | grep user-two
# grep user-two from /etc/passwd
user-two:x:1002:1002::/invalid_path/user-two:/bin/sh
# user-two was created , his home directory 
# is set to /invalid_path/user-two , but 
# user-two home directory , is not created . 
If the -b BASE_DIR or --base-dir BASE_DIR options are used

These options , override the value of the base directory , set in the HOME variable . The base directory value , will become BASE_DIR , and the user home directory will be set to : BASE_DIR plus his username .

@root:~$ useradd --base-dir /tmp user-three
# Create user-three , with the base directory 
# set to /tmp

@root:~$ cat /etc/passwd | grep user-three
# grep user-three from /etc/passwd
user-three:x:1003:1003::/tmp/user-three:/bin/sh
# user-three home directory is set to /tmp/user-three 

@root:~$ ls /tmp | grep user-three
# outputs nothing , since user-three
# directory is not created , because the 
# -m option was not used .
If the -d HOME_DIR or --home HOME_DIR options are used

These options , set the home directory of the user , to the path specified in HOME_DIR , and overrides the HOME variable , and the -b BASE-DIR or --base-dir BASE_DIR options .

@root:~$ useradd -m --home /tmp/tmp-usr user-four
# Create a user-four .
# The -m option is used , 
# as such the user home  directory 
# will be created .
# The --home option is used , hence , 
# the home directory will be set to the 
# path : /tmp/tmp-usr  

@root:~$ cat /etc/passwd | grep user-four
# grep user-four from /etc/passwd
user-four:x:1004:1004::/tmp/tmp-usr:/bin/sh
# user-four home directory is set to 
# /tmp/tmp-usr

@root:~$ ls /tmp/ | grep user-four
# The -m option was used , as such 
# the user home directory was created .
tmp-usr

@root:~$ useradd -m --home /tmp/invalid-path/home-user-five user-five
# user-five home directory , is set to :
# /tmp/invalid-path/home-user-five 
# using the --home option . 
# The directory invalid_path does not 
# exist , and the -m option will not 
# create it . 
# The -m option , will only create , the 
# last directory in the specified path . 
# As such useradd fails .
useradd: cannot create directory /tmp/invalid-path/home-user-five