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
You could use chpasswd
echo $1:$2 | chpasswd
Tested this on a CentOS VMWare image that I keep around for this sort of thing. Note that you probably want to avoid putting passwords as command-line arguments, because anybody on the entire machine can read them out of 'ps -ef'.
That said, this will work:
Sometimes it is useful to set a password which nobody knows. This seems to work:
The only solution works on Ubuntu 12.04:
But the second option only works when I change from:
To:
See explanations in original post: Changing password via a script
Read the wise words from:
I quote:
It goes on to explain how you can set your
shadow(5)
password properly, and shows you the GNU-I-only-care-about-security-if-it-doesn't-make-me-think-too-much-way of abusingpasswd(1)
.Lastly, if you ARE going to use the silly GNU passwd(1) extension
--stdin
, do not pass the password putting it on the command line.The last is the best you can do with GNU
passwd
. Though I still wouldn't recommend it.Putting the password on the command line means anyone with even the remotest hint of access to the box can be monitoring
ps
or such and steal the password. Even if you think your box is safe; it's something you should really get in the habit of avoiding at all cost (yes, even the cost of doing a bit more trouble getting the job done).You can use the
expect
utility to drive all programs that read from a tty (as opposed to stdin, which is what passwd does). Expect comes with ready to run examples for all sorts of interactive problems, like passwd entry.