How should I add users non-interactively?

Use useradd on Linux, at least.

Use crypt(3) to generate an encrypted password, and then do the following for each:

useradd -m -g [group] -p [crypt output] [user]

-m creates a home directory. -g sets the user's initial login group. -p sets the encrypted password, as returned by crypt(3) (you should note that this option may be unsuitable as the encrypted password will be visible to users listing the processes when the user is being created).

You will probably want to use other options too (at a guess, at least -s), read man 8 useradd for those.


It's an old question, but curious, others can consider the command newusers. This is present on both a RHEL5.5 system and a Ubuntu 12.04 system that I use, so I'd take a guess it will be available in the repositories for most distributions.

From man newusers:

The newusers command reads a file of user name and clear-text password pairs and uses this information to update a group of existing users or to create new users. Each line is in the same format as the standard password file (see passwd(5)) with the exceptions explained below

Tags:

Bash

Useradd