Is it possible / any recommendations on writing a shell script to automate the process of creating a new user/ftp user:

Currently the process:

sudo useradd -d /ftp-files/user-a -m user-a
sudo passwd user-a

The issue is that I need to manually enter the passwords when prompted:

Enter new UNIX password: [enter pwd]
Retype new UNIX password: [enter pwd-same]
passwd: password updated successfully

With lots of accounts to generate this gets very time consuming.. If I could do it all via a script, that would be great.. But is it possible to input passwords on the fly via a bash script?


You can use something along the lines of:

sudo useradd -d /ftp-files/$1 -m $1
echo $2 | sudo passwd --stdin $1

And then invoke the script like this:

./createuser <username> <password>

For more information about the --stdin flag, you can check man 1 passwd.

EDIT : Unfortunately, it turns out that the passwd command in Ubuntu does not support the --stdin flag (at least according to this man page), so you will have to go with the chpasswd option:

sudo useradd -d /ftp-files/$1 -m $1
echo "$1:$2" | sudo chpasswd
  • Thanks for the reply - looks what I need. However, for some reason it keeps throwing.. chpasswd: unrecognized option '--stdin' for chpasswd and passwd..? Strange – williamsowen Jun 28 '12 at 15:04
  • It looks like Ubuntu's passwd command doesn't support --stdin, I added a workaround using chpasswd, please check the edited section above. – Vladimir Blaskov Jun 28 '12 at 15:32

I would say you probably want to use chpasswd rather than passwd. The manpage gives you all the necessary details. There's also an entertaining read on why this is a bad idea.

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.