12

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?

  • 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 '15 at 16:25
20

The useradd program has been deprecated in favor of adduser. From man 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, you should always 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  fea‐
   tures. 
  • 1
    i dont want use adduser command. – Beginner Jan 31 '15 at 17:56
  • 4
    @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 '15 at 18:14
  • 1
    #terdon ya correct.. but i am writing a shell script using useradd command only – Beginner Feb 1 '15 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 '15 at 14:45
  • 1
    just look at my answer, I don't know its a correct one but it works for me – Beginner Feb 2 '15 at 3:38
19

Finally I finished myself

This is the answer

   useradd -m -d /home/testuser/ -s /bin/bash -G sudo testuser
  • what is the -d flag doing, or the -G flag doing – Alexander Mills May 16 '18 at 17:57
  • you check using man command. just execute " man useradd". – Beginner Jul 18 '18 at 10:35
  • 1
    -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. – Alastair Harrison Sep 17 '18 at 14:48
  • @Beginner you check using man command. That's a zero helpful reply, you could have just explained AFTER you remember people about the man function. – MS Berends Mar 21 at 15:45
  • @AlastairHarrison Praise to you. – MS Berends Mar 21 at 15:45
4
useradd -m LOGIN

creates the user's home directory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.