141

I am trying to understand system administration on Ubuntu. So, as an example, I create a dummy user using

sudo useradd -d /home/linda linda

and passwd to create the password. I check that an entry has been made using cat /etc/passwd

linda:x:1004:1004::/home/linda:/bin/sh

However, when I su - linda, I get

No directory, logging in with HOME=/

and indeed, no home directory has been created. What am I missing?

Thanks.

tchakravarty
  • 1,667
  • 3
  • 14
  • 15

12 Answers12

152

man useradd states:

useradd is a low level utility for adding users. On Debian,
administrators should usually use adduser(8) instead.

Note the low level utility

To add a user, use adduser instead. It's a more high-level utility.


Moreover, looking at the -d option:

   -d, --home HOME_DIR
       The new user will be created using HOME_DIR as the value for the
       user's login directory. The default is to append the LOGIN name to
       BASE_DIR and use that as the login directory name. The directory
       HOME_DIR does not have to exist but will not be created if it is
       missing.

The directory will not be created if it is missing.

Generally, keep away from useradd, use adduser instead.

wjandrea
  • 14,236
  • 4
  • 48
  • 98
  • 2
    In the OP's defense, when I read "On Debian,", I thought it meant on Debian as opposed to Ubuntu distros. I am aware that Ubuntu is built on Debian but thought that a distinction was being made. – Keith Bennett May 15 '15 at 16:11
  • 1
    I used adduser but still the created home directory only contains a file examples.desktop and nothing else. How can I get Ubuntu to create the default folders Desktop, Downloads and so on? (I'm logged in via ssh) – mcExchange Aug 13 '15 at 11:35
  • 1
    Hi, sorry late response and only guessing but I think those folders are created automatically on the first GUI login. Just thought people might still land here ;) – derHugo Nov 06 '17 at 07:59
  • adduser will not create the directory either if someone has changed CREATE_HOME in /etc/login.defs to "no". You can override this with the -m flag. – Noumenon Jun 11 '18 at 22:35
  • that was great thank you it solved my problem also – ali Falahati Nov 30 '20 at 12:18
  • 1
    note for rhel users: i think this question/solution also applies. on centos7 you have to yum install shadow-utils to get adduser and then you can use adduser. – Trevor Boyd Smith Jan 01 '21 at 19:57
52

you can fix this simply by creating the home dir.

mkdir /home/linda
chown linda:linda /home/linda

try logging in again and this should work.

OmPS
  • 739
28

According with man useradd, -d /home/linda option will not create the directory /home/linda, if this is missing. So, you have to create it manually. To do this, run the followings commands in terminal:

sudo -i                            #to get root privileges
mkdir /home/linda                  #to create the directory /home/linda
cp -rT /etc/skel /home/linda         #to populate /home/linda with default files and folders
chown -R linda:linda /home/linda   #to change the owner of /home/linda to user linda

See also: How to make user home folder after account creation?

Radu Rădeanu
  • 169,590
14

Look at /etc/defaults/useradd if you want to change the defaults. Use:

useradd -m -d /home/joe -s /bin/bash.

Luís de Sousa
  • 13,227
  • 26
  • 81
  • 128
beanhead
  • 141
  • 1
    "According with man useradd, -d /home/linda option will not create the directory /home/linda" so no, this is not a solution. – astrojuanlu Nov 02 '17 at 13:59
  • 6
    According to the useradd man page, that's what the -m flag is for:-m, --create-home Create the user's home directory if it does not exist. The files and directories contained in the skeleton directory (which can be defined with the -k option) will be copied to the home directory. useradd will create the home directory unless CREATE_HOME in /etc/login.defs is set to no.@astrojuanlu – Myles May 29 '19 at 12:44
12

Use -m instead of -d, so the directory will be created for you:

sudo useradd -m linda

Also, if linda is a normal user, you might want her to use /bin/bash as default shell:

sudo useradd -m linda -s /bin/bash

p1100i
  • 221
  • 2
  • 5
5

You can also modify /etc/pam.d/common-session to make it so that a user's home directory will be created on first log in. Add the following line to that file.

...
session required pam_mkhomedir.so

This is particularly useful if your system is on a network where the users are managed externally to your machine, by LDAP for instance.

KevinC
  • 248
  • 3
  • 11
3

Add the below entry in /etc/login.defs and save:

CREATE_HOME yes

Now, try to create user accounts. It will create the home directory.

David Foerster
  • 36,264
  • 56
  • 94
  • 147
kavin
  • 31
  • It worked for me. I needed to have a basic script that will remotely add/manage users on different flavors of Linux. After this change my code produces same results on all servers. Thanks. –  Feb 18 '16 at 02:36
  • Life saver, only solution that worked for me. Was using os.system in python to call useradd btw, so it's likely related to that. – Justin G Oct 27 '20 at 17:36
2

useradd command doesn't create a home directory automatically, regardless of the default settings in /etc/login. You have to specify the -m option if you want a home directory for a system account to be created.

2

The most likely reason why you did not have the home directory created is because you did not have the CREATE_HOME yes in /etc/login.defs.

You can fix this by following what @OmPS or @Radu Rădeanu had suggested.

But that many ways to overcome this problem in future by using one of the commands below:

  • Result of sudo adduser linda

    Adding user 'linda'
    Adding new group 'linda' (1001) ...
    Adding new user 'linda' (1001) with group 'linda' ...
    Creating home directory '/home/linda' ...
    Copying files from '/etc/skel' ...
    ****Password confirmation****
    ****Name prompt****
    

    The defaults for adduser are chosen from /etc/adduser.conf if --home option is not specified. Note that it also copies the /etc/skel contents.

  • Use adduser with --home

    sudo adduser --home /home/linda
    

Same as previous option except that you may want this if the users home directory is different than the username that you assigned.

  • Specify base directory to useradd command:

    sudo useradd -b /home
    
  • Use login.defs: Modify /etc/login.defs and add the line below before doing sudo useradd:

    CREATE_HOME   yes
    

Note: if you do man login.defs, it currently says

Much of the functionality that used to be provided by the shadow password suite is now handled by PAM. Thus, /etc/login.defs is no longer used by passwd(1), or less used by login(1), and su(1). Please refer to the corresponding PAM configuration files instead.

  • Use pam_mkhomedir PAM module: from man pam_mkhomedir page, add the line below to /etc/pam.d/login:

    session  required  pam_mkhomedir.so skel=/etc/skel
    
1

Use adduser.

DESCRIPTION

 adduser  and  addgroup  add users and groups to the system according to
   command    line    options    and    configuration    information    in
   /etc/adduser.conf.   They  are  friendlier  front ends to the low level
   tools like useradd, groupadd and usermod programs, by default  choosing
   Debian  policy conformant UID and GID values, creating a home directory
   with skeletal configuration, running a custom script,  and  other  fea‐
   tures.  adduser and addgroup can be run in one of five modes:

useradd you have to add all options yourself. Including permissions and some other things.adduser does this based on sane defaults (and also adds home dir by itself).

If you need to use adduser you probably need the -b option together with the -d option!

Rinzwind
  • 299,756
1

If /home/linda is not present before you add linda as a user, you also would have to add --create-home.

sudo useradd --create-home linda
Thomas
  • 6,223
Abel Tom
  • 641
-1

My issue was that SELinux was preventing creation of home directory upon login by an unprivileged user:

$ ls -ldZ /home/
drwxr-xr-x. root root system_u:object_r:root_t:s0 /home/

That root_t was preventing it from happening. I had to re-label it as:

chcon system_u:object_r:home_root_t:s0 /home/

tash
  • 107
  • 1
    SELinux is not used in Ubuntu. How did you even enable it? – muru Mar 12 '24 at 16:02
  • @muru it's not used in Ubuntu by default, but users can install it from the repositories and set it up on their system themselves. However, SELinux is not well supported in Ubuntu, meaning that you are likely to break things by using it. AppArmor is the application restrictions system that is in place instead of SELinux on Ubuntu by default. – Thomas Ward Mar 12 '24 at 16:52
  • Oh sorry, I did not know about Ubuntu. My solution is based off CentOS 9 (RHEL 9). – tash Mar 14 '24 at 21:13