I'm writing a shell script to automatically add a new user and update their password. I don't know how to get passwd to read from the shell script instead of interactively prompting me for the new password. My code is below.
adduser $1 passwd $1 $2 $2
For those who need to 'run as root' remotely through a script logging into a user account in the sudoers file, I found an evil horrible hack, that is no doubt very insecure:
I stumbled upon the same problem and for some reason the
--stdin
option was not available on the version ofpasswd
I was using (shipped in Ubuntu 14.04).If any of you happen to experience the same issue, you can work it around as I did, by using the
chpasswd
command like this:This is the definitive answer for a teradata node admin.
Go to your
/etc/hosts
file and create a list of IP's or node names in a text file.Put the following script in a file.
Okay I know I've broken a cardinal security rule with ssh and root but I'll let you security folks deal with it.
Now put this in your
/usr/bin
subdir along with yoursetpwd.srvrs
config file.When you run the command it prompts you one time for the User ID then one time for the password. Then the script traverses all nodes in the
setpwd.srvrs
file and does a passwordless ssh to each node, then sets the password without any user interaction or secondary password validation.Have you looked at the
-p
option ofadduser
(which AFAIK is just another name foruseradd
)? You may also want to look at the-P
option ofluseradd
which takes a plaintext password, but I don't know ifluseradd
is a standard command (it may be part of SE Linux or perhaps just an oddity of Fedora).from "
man 1 passwd
":So in your case
[Update] a few issues were brought up in the comments:
Your
passwd
command may not have a--stdin
option: use thechpasswd
utility instead, as suggested by ashawley.If you use a shell other than bash, "echo" might not be a builtin command, and the shell will call
/bin/echo
. This is insecure because the password will show up in the process table and can be seen with tools likeps
.In this case, you should use another scripting language. Here is an example in Perl:
Nowadays, you can use this command: