I have a set of users I need to add to a new system as part of a BASH script that preps my server. How can I non-interactively add the user, create their home directory, set the group, and enter a default password?

For example, if I needed to add the following users, how would I go about that?

user: node
password: spoon
group: ultraMegaPermissions

password: spoot
group: sortaNotMega

password: spoogle
group: notMega
  • 1
    Which operating system? On my Linux useradd (different than adduser, but form the same shadow package) handles command line parameters: useradd -p spoon -g ultraMegaPermissions -m node. – manatwork Sep 19 '11 at 14:52

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

| improve this answer | |
  • 1
    no, that would be easy and obviously silly =) – James Feb 2 '13 at 19:11

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.

| improve this answer | |
  • Cheers! This worked for me very well for creating a user with a password from within a Dockerfile which used a alpine linux as its base image. Had to add an extra repository RUN echo http://dl-2.alpinelinux.org/alpine/edge/community/ >> /etc/apk/repositories and then install apk add shadow which provides crypt. – Sven M. Jun 9 '17 at 16:48

If you need manually input user password:

echo -n "Enter $MY_USER password: "
read -sa MY_PASS
echo ${MY_USER}:${MY_PASS}::::/home/${MY_USER}:${DEFAULT_SHELL} | newusers

And if you need it totally non-interactive, just run the last command with clear text password ($MY_PASS), which normally not a good idea

| improve this answer | |

In case newuser command do not exists on the server.

You can use take the help of passwd command.

 echo "new_passwd" | passwd  --stdin new_user
| improve this answer | |
  • Does not work on my Ubuntu 14.04 – Champ Jul 28 '15 at 18:25

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.