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'm in the process of making a bash script to automate adding a user. My password always have pattern, so I am setting that to a variable then... (this is where I am stuck -encrypting-). I found this post but that talks about -stdin which I don't want to use. Besides, I am asked for the password if I omit it anyway. So, how can I encrypt the password from my username automatically?

My script

#!/bin/bash
if [ $# -ne 1 ]
then
  echo "Usage: $0 <user>"
else
  p=${1%.*}
  p=$(echo $p | tr -d 'aeiou' | openssl passwd) $1
  useradd -p $p $1
fi

EDIT: mkpasswd on CentOS doesn't seem to work like the one mentioned in the post I linked.

EDIT2: bash version is 4.1.2(1)-release, if that matters.

share|improve this question

2 Answers 2

I ended up using this, inspired from this post

#!/bin/bash
if [ $# -ne 1 ]
then
  echo "Usage: $0 <user>"
else
  u=${2%.*}
  u=${u//[aeiou]}
  echo "$2:$u" > .tmp
  useradd $2
  < .tmp chpasswd
fi
share|improve this answer

You can use useradd and chpasswd to automate user creation. e.g.

useradd -c "Bob Smith" -m bob echo "bob:mypassword" | chpasswd

If you don't want the plain text password to appear in the script, use the --encrypted flag echo "bob:encrypted_mypassword" | chpasswd --encrypted

share|improve this answer
    
I have a pattern when creating the password, so I'd rather stick to that. With that said, maybe it's time to lose the pattern and use standard stuff. –  Tolga Ö. May 14 at 8:34

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.