I found a guide that explains how to set a user's password. I'm trying to automate it and send an e-mail to the user like:

userid created with password XYZ.
request to change the initial password.

According to the doc above, an encrypted password needs to be created using Python and fed to the usermod command like this:

 usermod -p "<encrypted-password>" <username>

Are there any other simpler ways to do this? I don't want to download any special utility to do it; it should be generalized as much as possible.

Edit: Even the method given in the above link doesn't seem to work for me:

bash-3.00# python
Python 2.4.6 (#1, Dec 13 2009, 23:43:51) [C] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import crypt; print crypt.crypt("<password>","<salt>")
>>> ^D
bash-3.00# useradd -g other -p "sOMrcxm7pCPI" -G bin,sys -m -s /usr/bin/bash mukesh2
UX: useradd: ERROR: project sOMrcxm7pCPI does not exist.  Choose another.
UX: useradd: sOMrcxm7pCPI name should be all lower case or numeric.
  • here is one more link i found but there seems a security issue also with using -p option in usermod when somebody uses ps to see process listing the password is visible
    – munish
    Dec 6, 2012 at 23:54

4 Answers 4


You can use chpasswd to do it, like this:

echo "username:newpassword" | chpasswd

You can pipe into chpasswd from programs other than echo, if convenient, but this will do the trick.

Edit: To generate the password within the shell script and then set it, you can do something like this:

# Change username to the correct user:
# This will generate a random, 8-character password:
PASS=`tr -dc A-Za-z0-9_ < /dev/urandom | head -c8`
# This will actually set the password:
echo "$USR:$PASS" | chpasswd

For more information on chpasswd, see http://linux.die.net/man/8/chpasswd

(Command to generate password was from http://nixcraft.com/shell-scripting/13454-command-generate-random-password-string.html)

  • thanks @Dominick hmm, chpasswd is perhaps in AIX...i havn't used it. and evrytime i have to write a password ...can't it be generated by the script like in the questions link...unfortunately even that didn't work for me
    – munish
    Dec 6, 2012 at 23:02
  • @munish I'm not quite sure what you mean by generate in the script, but I updated my answer to hopefully be more helpful. Dec 6, 2012 at 23:23
  • Does not work. "Authentication token manipulation error"
    – Cerin
    Jun 30, 2014 at 23:17
  • 1
    @Cerin By any chance are you doing something like sudo echo "username:newpass" | chpasswd? Because the elevated permissions from sudo do not pass through the pipe, so chpasswd would be running as a normal user. It can be fixed by moving the sudo, as in echo "username:newpass" | sudo chpasswd. There are also other problems that can cause that error message, but permissions errors like this are probably the most common. Jul 2, 2014 at 13:38
  • 2
    Passwords on the command line could be visible to other logged in users on the same machine using w, ps, or other commands which show info about other processes. Passwords set this way should be changed by the user as soon as possible.
    – Mnebuerquo
    Jan 28, 2016 at 18:46

You can use OpenSSL to generate the random password (16 characters, in this case):

# 1000 bytes should be enough to give us 16 alphanumeric ones
p=$(openssl rand 1000 | strings | grep -io [[:alnum:]] | head -n 16 | tr -d '\n')

Then feed the hashed password to useradd or usermod

# omit the "-1" if you want traditional crypt()
usermod -p $(openssl passwd -1 "$p") <username>


Since posting this in 2012, newer versions of OpenSSL have added functionality to the openssl passwd command. Instead of using the -1 option to get an MD5-based hash, modern versions also support -5 for SHA256-based hashes and -6 for SHA512-based hashes.

Credit where due: The password generation is adapted from a similar method that uses /dev/urandom instead of openssl.


useradd should work (I've done it on Ubuntu). Maybe check that each of your args are correct (thee groups exist, the path is right to bash). You can run the command with just a password and user, and then use userdel to remove and then retry with more parameters, to see what one causes the issue (brute force approach).

There is also newusers (see the man page), at least under Ubuntu, where you give it a file with passwd file like info, including plain text passwords and it will create those users. Nice way to do many users at once.

  • +1 for mentioning newusers command for Ubuntu
    – Levon
    Oct 3, 2013 at 20:49

simplest way I found :

echo ${PASSWD} | passwd --stdin username_here
  • Did you even test this? passwd: unrecognized option '--stdin'
    – Cerin
    Jun 30, 2014 at 23:19
  • Yes, I used this many times but, as with almost any other command, it is not the defacto solution to the problem at hand. Some variations of passwd binary, do not support the --stdin switch.
    – MelBurslan
    Jul 21, 2014 at 20:18

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.