Take the 2-minute tour ×
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>")
<sOMrcxm7pCPI
>>> ^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
add comment

4 Answers

up vote 3 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:
USR=username
# 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)

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
add comment

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
add comment

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
add comment

simplest way I found :


PASSWD="mySeCr3t-default-pa55" 
echo ${PASSWD} | passwd --stdin username_here
share|improve this answer
add comment

Your Answer

 
discard

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.