I use the following command to create a user in a linux machine:

useradd -d /home/dummy -g idiots -m -p 12345689 dummy

The user is created and the home directory as well.
The problem is that I can not log-in to the system using this account since the -p expects the encrypted password returned by crypto.

Question:I want to create a user via a bash script and I don't know the encrypted password by crypto. How can I do it so that I am able to create this user automatically via a script and get arround the problem with the password?


You can use openssl to generate pre encrypted password strings to use with the -p option to useradd

echo "P4sSw0rD" | openssl passwd -1 -stdin


The -1 says to generate a MD5 password hash. The salt is automatically generated.

You can then use

useradd -d /home/dummy -g idiots -m -p $(echo "P4sSw0rD" | openssl passwd -1 -stdin) dummy

to add the user. To do this interactively hiding the password

useradd -d /home/dummy -g idiots -m -p $(read -sp Password: pw ; echo $pw | openssl passwd -1 -stdin) dummy
  • Is openssl command available in ALL linux installations by default?Can I count on it?
    – Jim
    Mar 8 '12 at 10:51
  • Also echo will show the password in the console.How can I avoid this?
    – Jim
    Mar 8 '12 at 10:52
  • @Jim: If not all, then 99%. It's part of the SSH client and server packages, so it's almost always installed, though technically I don't believe it's required. You can count on it being installed by default, but not necessarily that someone has not uninstalled it. Mar 8 '12 at 10:56
  • @Jim: Couldn't say with 100% certainty it'll be on all linux installations.
    – user9517
    Mar 8 '12 at 10:57
  • @Iain:What about echo?How can I avoid that?
    – Jim
    Mar 8 '12 at 10:59

Apparently, you can use

echo "password" | passwd dummy --stdin

I've never tried this.

Alternatively, you could put the user's public key in /home/dummy/.ssh/authorized_keys and forget about passwords entirely. This is the best option security-wise.

  • echo will show the password in the console.How can I avoid this?
    – Jim
    Mar 8 '12 at 10:52
  • You could put the password in a file and use cat passwordfile | passwd dummy --stdin or passwd --stdin dummy < passwordfile. On another note, I just tested this --stdin option on a Ubuntu box and it did not work. The version of passwd I have doesn't support that option. Yours may not either. One of the other answers using chpasswd, openssl or newusers might be better if you must have passwords.
    – Ladadadada
    Mar 8 '12 at 11:53

That`s how I do it:

# cat user-pw_list

# cat CreateUsers.sh
# filename: CreateUsers.sh
# usage: cat "User:passwd" | $0
set -e
# set -x
while read ; do
  # alternative for random passwd, where $RANDOM is a bash function

  echo -e "adding User $USER "
  # for disabled users: /usr/sbin/nologin, otherwise /bin/bash
  /usr/sbin/useradd -c automaticUser -m -k/dev/null -s /usr/sbin/nologin $USER
  echo "$USER:$PWD" | chpasswd --md5 $USER

  ## also add user to samba:
  #echo -e "$PWD\n$PWD" | pdbedit -t -u $USER
  • 1
    Could you please explain this solution?I am not very good with bash scripts
    – Jim
    Mar 8 '12 at 12:33

As you are going to use a bash script, perhaps the good old newusers command would be helpful to you? It reads its input from a text file formatted like this:


And the password in that file should be clear text. You can list as many users as you wish in the input file.

For more information see man newusers.

  • I get:bash: newusers: command not found and No manual entry for newusers
    – Jim
    Mar 8 '12 at 10:54
  • Oh, so SLES does not bundle newusers (by default), even though Fedora, Debian and RHEL seem to do so. Mar 8 '12 at 10:56

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.