0

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?

williamsowen
  • 1,157
  • 3
  • 16
  • 24

2 Answers2

3

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
Vladimir Blaskov
  • 6,073
  • 1
  • 26
  • 22
  • 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
1

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.

womble
  • 95,029
  • 29
  • 173
  • 228