I have a specific use case where I would really like to be able to change a user's password with a single command with no interactivity. This is being done in a safe fashion (over SSH, and on a system with only one user able to be logged in), so it's fine to expose the new password (and even the old one, if necessary) on the command line. FWIW, it's a Ubuntu system.
I just want to avoid having to add something Expect-like to this system for just this one task.
You could use chpasswd.
You can use
usermod
with the-p
option to provide a password hash (not the actual password). You can generate the password hash using something likemkpasswd -m sha-256
ormkpasswd -m md5
Sure.
sed
script that updates your system's password file (/etc/shadow
,/etc/master.passwd
, whatever it happens to be).The
passwd
utility has a --stdin option that states:Syntax:
Even though you mentioned you don't care, you could put the password in a text file and then do
cat pass.txt
instead of the echo command, that way it doesn't show up in the bash history.If
--stdin
option is not working we can basically use two options:chpaswd
in your script.echo "current_password\nnew_password\nnew_password" | passwd user_name