35

I created a new user (testuser) using the useradd command on an Ubuntu server Virtual machine. I would like to create a home directory for the user and also give them root provileges.

However, when I login as the new user, it complains that there is no home directory. What am I doing wrong?

1
  • I answered the main question about creating a home dir. Giving a new user root access is an unrelated issue and should be asked separately. Basically, you just need to add the user to the sudo group.
    – terdon
    Jan 31, 2015 at 16:25

4 Answers 4

46

Finally I found how to do this myself:

   useradd -m -d /home/testuser/ -s /bin/bash -G sudo testuser

-m creates the home directory if it does not exist.
-d overrides the default home directory location. -s sets the login shell for the user.
-G expects a comma-separated list of groups that the user should belong to.

See man useradd for details.

3
  • 1
    what is the -d flag doing, or the -G flag doing May 16, 2018 at 17:57
  • 3
    -m creates the home directory if it does not exist. -d overrides the default home directory location. -s sets the login shell for the user. -G expects a comma-separated list of groups that the user should belong to. Sep 17, 2018 at 14:48
  • One problem I'm having with this method is it doesn't create the default contents in the user dir, like "Downloads" or ".ssh" for example, but there are others. Apr 5, 2021 at 4:21
34

Editor's note: This answer is technically correct to the question as asked, but discussion in the comments clarified that the OP was trying to use useradd in a script rather than directly as a shell command, and adduser is less suited for that context.

When creating a user interactively, it's generally recommended to use adduser rather than useradd.

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

adduser is a friendlier frontend to useradd and will do things like create user directories by default. When you run it with only a username as an argument, you will be prompted to provide additional information such as the password:

$ sudo adduser testuser
Adding user `testuser' ...
Adding new group `testuser' (1002) ...
Adding new user `testuser' (1002) with group `testuser' ...
Creating home directory `/home/testuser' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
Changing the user information for testuser
Enter the new value, or press ENTER for the default
    Full Name []: 
    Room Number []: 
    Work Phone []: 
    Home Phone []: 
    Other []: 
Is the information correct? [Y/n] 

In general, it's recommended to use use adduser instead of useradd since this will also set up the required groups automatically. As explained in man adduser:

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 features.

11
  • 1
    i dont want use adduser command.
    – Beginner
    Jan 31, 2015 at 17:56
  • 5
    @rajcoumar umm, why? That's the right tool for the job. If you insist on using the wrong tool, you will have to manually create the home directory and the user groups and copy the default files from /etc/skel etc., etc. That is why the Official Debian Way® is to use adduser.
    – terdon
    Jan 31, 2015 at 18:14
  • 1
    #terdon ya correct.. but i am writing a shell script using useradd command only
    – Beginner
    Feb 1, 2015 at 11:36
  • @rajcoumar OK, then you have to either create everything manually, or just change the script to use adjuster instead. Just create an /etc/adduser.conf file.
    – terdon
    Feb 1, 2015 at 14:45
  • 7
    adduser is an interactive wrapper to useradd - it is not appropriate in scripted solutions, particularly dockerfiles. Jan 28, 2018 at 20:43
5
useradd -m LOGIN

creates the user's home directory

0

It happened to me when I used the command:

sudo useradd -d /home/ssd2/yuwen yuwen

After I typed in that command, there is no such file (I checked by using cd&ls), and when I use su yuwen, it successfully changed into new user, but the home dir was still unchanged (not ssd2/yuwen as it supposed to be).

I also use command cat /etc/passwd to check if the new user is created. It is created but the home dir is not valid.

The following is the solution if you encounter the same situation like what I described:

  1. Login in the linux server using a super user account which could use sudo
  2. Use cd into the dir where you want to put the new user's dir. In my situation, the dir is /home/ssd2
  3. Use mkdir to create the dir for new user, in my case, it is mkdir yuwen
  4. Use sudo chown yuwen:yuwen /home/ssd2/yuwen to let your new user have the ownership.
  5. Use sudo chmod -R yuwen /home/ssd2/yuwen, authorize it

Then you can use su yuwen, after that use cd ~, pwd to see that you are in the right directory.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .