How can I quickly add a lot of users to an Ubuntu server from the command-line?

6,628

Solution 1

Type "man adduser" at a shell prompt. It will give you documentation on how to use adduser. In general, man will give you documentation on any CLI command.

But you really want to use useradd and not adduser.

In short:

useradd -c "Real Name" -m -g primarygroup -G secondarygroup1,secondarygroup2 username

Oh, and if you want to set the password at the same time, you have to pre-encrypt it. I continually rewrite this little program to do Unix-standard salt encryption:

#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[]) {

        char *salt;

        if (argc < 2 || argc > 3) {
                fprintf(stderr, "Usage: %s string [salt]\n", argv[0]);
                exit(1);
        } else {
                if ( argc == 2 ) {
                        salt = argv[1];
                } else {
                        salt = argv[2];
                }
                printf("%s\n", crypt(argv[1], salt));
                exit(0);
        }
}

With this compiled as crypt, you can then add the option:

-p `crypt password`

Ideally, you don't ever want to put passwords in a command line, though, as there are multiple places it can show up (ps output, shell history, etc.), none of which are particularly secure.

Solution 2

You can create a file containing the list of usernames, etc., and use the newusers command. It wants the file to look like the format of /etc/passwd with a few exceptions, one is that the password is plain text (newusers encrypts it).

newusers userfile.txt

It doesn't handle multiple groups, though.

Solution 3

If you need to add that many users and are constantly rebuilding machines then it might warrant using Puppet. That's a fairly simple recipe in itself and you can expand from there to cover other tasks.

Solution 4

Since it's ubuntu, the syntax is slightly different. It probably matches debian:

adduser --gecos "Real Name" --gid PRIMARYGROUP USERNAME
usermod -G SECONDARYGROUP1,SECONDARYGROUP2 -p $(openssl passwd -1 -salt shaker "USERPASSWORD") USERNAME
Share:
6,628

Related videos on Youtube

Jayesh Elamgodil
Author by

Jayesh Elamgodil

Updated on September 17, 2022

Comments

  • Jayesh Elamgodil
    Jayesh Elamgodil over 1 year

    I've just become familiar with basic usage of adduser. I'd like to eliminate the interaction/prompts. Since I'll be rebuilding this machine often, I'd like to script the process or at worst just copy and paste a complete statement that adds the user to the right group and sets the password in one command.

    Something like:
    sudo adduser username1 password primarygroup secondarygroup,ternarygroup "full name1"
    sudo adduser username2 password primarygroup secondarygroup,ternarygroup "full name2"
    sudo adduser username3 password primarygroup secondarygroup,ternarygroup "full name3"
    sudo adduser username4 password primarygroup secondarygroup,ternarygroup "full name4"
    ...
    sudo adduser username999 password primarygroup secondarygroup,ternarygroup "full name999"

  • djhowell
    djhowell over 14 years
    To set the password from a script you can do: echo "mypa$$word" | passwd myuser --stdin
  • Steve Townsend
    Steve Townsend over 14 years
    If you had used useradd as wfaulk wrote, it would have worked :) useradd is the scriptable version.
  • Dan Carley
    Dan Carley over 14 years
    Beware of passwords being visible in process lists and saved in your shell history.
  • Jayesh Elamgodil
    Jayesh Elamgodil over 14 years
    That looks like something I could use in the future, thanks for the link.
  • Sumeet Kashyap
    Sumeet Kashyap over 14 years
    -1 for whoever named useradd and adduser - I can never remember which way round they aer..