Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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.
share|improve this question
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 '12 at 23:54

4 Answers 4

up vote 8 down vote accepted

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

(Command to generate password was from

share|improve this answer
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 '12 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. –  Dominick Dec 6 '12 at 23:23
Does not work. "Authentication token manipulation error" –  Cerin Jun 30 '14 at 23:17
@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. –  Dominick Jul 2 '14 at 13:38

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>

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

share|improve this answer

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.

share|improve this answer
+1 for mentioning newusers command for Ubuntu –  Levon Oct 3 '13 at 20:49

simplest way I found :

echo ${PASSWD} | passwd --stdin username_here
share|improve this answer
Did you even test this? passwd: unrecognized option '--stdin' –  Cerin Jun 30 '14 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 '14 at 20:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.