Set a user's password non-interactively

By chimo on (updated on )

Today I ran into a use-case where I needed to set a Linux user's password via script, non-interactively. I don't think this is something I've had to do in the past, and so I learned about the `chpasswd` utility.

The chpasswd command reads a list of user name and password pairs from standard input and uses this information to update a group of existing users. Each line is of the format:

user_name:password

For my purposes, the command is part of a larger script and end up being used as follows:

echo "${username}:${random_password}" | chpasswd