Use Linux ‘passwd’ Command Non-Interactively via Stdin

Updated

Linux terminal on an Ubuntu system

To set a Linux password without a prompt, pipe a user:password line into chpasswd as root. It works on Debian, Ubuntu, RHEL and Fedora, and because printf is a builtin in bash and dash, the password never appears in ps:

printf '%s:%s\n' "$user" "$pw" | sudo chpasswd

passwd --stdin does the same for one account, but only where your passwd has the option. RHEL, Fedora, Debian 13 and Ubuntu 26.04 LTS have it; Debian 12 and Ubuntu 22.04 and 24.04 do not. Neither needs expect, because there is no prompt to answer.

Change a password non-interactively with chpasswd

Each line on standard input is user:password, and one run can update many accounts. The first colon ends the user name, so a password can contain colons and a user name cannot. A line with no colon fails with missing new password. If the lines come from a file, create it under umask 077 and delete it afterwards; chpasswd(8) warns about readable files of clear-text passwords.

On Debian and Ubuntu, chpasswd hashes through PAM (/etc/pam.d/chpasswd includes common-password), so you get whatever pam_unix.so is configured for. The -e option (the lines already hold hashes) and -c SHA512 (choose the algorithm) bypass PAM entirely, and the man page says this is “not recommended”. On RHEL, chpasswd does not use PAM and follows ENCRYPT_METHOD in /etc/login.defs.

Use passwd without a prompt: which systems have --stdin

Upstream shadow-utils added -s, --stdin to passwd in 4.15.0 (8 March 2024), so a system built from shadow 4.15 or newer has it. Red Hat’s own passwd 0.80, used by RHEL 8 and 9, also has --stdin, and RHEL 10 and Fedora now ship the shadow-utils passwd instead. Write --stdin rather than -s, since 0.80 has no short form, and run it as root:

printf '%s\n' "$pw" | sudo passwd --stdin alice
System passwd --stdin Default hash for new passwords
Debian 12 (shadow 4.13) No yescrypt
Debian 13 (shadow 4.17.4) Yes yescrypt
Ubuntu 22.04 LTS (shadow 4.8.1) No yescrypt
Ubuntu 24.04 LTS (shadow 4.13) No yescrypt
Ubuntu 26.04 LTS (shadow 4.17.4) Yes yescrypt
RHEL 8 and 9 (passwd 0.80) Yes SHA-512
RHEL 10 (shadow 4.15) Yes yescrypt
Fedora 43 and 44 (shadow 4.18, 4.19) Yes yescrypt

To check a machine, run passwd --help | grep stdin. sudo getent shadow alice | cut -d: -f2 | cut -c1-3 prints the hash prefix: $y$ is yescrypt and $6$ is SHA-512. I checked these versions in package sources on 1 October 2026.

Set a hashed password with usermod -p

usermod -p expects a crypt hash, not a password. It stores the string exactly as given, so plaintext becomes a broken hash and password logins fail, and its man page warns that “the password (or encrypted password) will be visible by users listing the processes”. I make the hash from stdin and pass it to chpasswd -e instead, which keeps it off the command line:

hash=$(printf '%s\n' "$pw" | openssl passwd -6 -stdin)
printf '%s:%s\n' alice "$hash" | sudo chpasswd -e

On Debian and Ubuntu, mkpasswd (package whois) does the same: mkpasswd -m sha-512 -s reads stdin, and -m yescrypt makes a yescrypt hash. If you type a hash into usermod -p, single-quote it: inside double quotes the shell expands every $ part and you store something else.

openssl passwd -6 makes a SHA-512 hash ($6$) and cannot make yescrypt. The crypt(5) page recommends yescrypt ($y$) for new hashes and calls SHA-512 acceptable, but $y$ needs libxcrypt 4.2 or newer on the machine that checks it, and RHEL 8 ships 4.1.1, so use $6$ for hashes that travel between systems.

Keep the password out of ps and shell history

Anything on a command line sits in /proc/<pid>/cmdline, and unless /proc is mounted with hidepid=1 or higher, every local user can read it with ps. proc(5) lists hidepid=0, “Everybody may access all /proc/pid directories”, as the default.

So usermod -p, useradd -p, openssl passwd -6 'secret' and mkpasswd 'secret' all expose what you give them, and so does sudo sh -c "echo alice:$pw | chpasswd", because the expanded string becomes the shell’s own arguments. I keep the ps demonstration and the upstream, Debian and Ubuntu version checks in a script you can run, /verify/passwd-without-prompt.sh; it never touches an account.

Shell history is the other leak. Bash stores each command before it expands variables and writes the history to ~/.bash_history when the shell exits, so a password typed into a command line is saved, while a variable saves only its name. In bash, read the password silently instead; IFS= keeps leading and trailing spaces and -r keeps backslashes:

IFS= read -rsp 'New password for alice: ' pw; echo
printf '%s:%s\n' alice "$pw" | sudo chpasswd
unset pw

I use printf '%s' rather than echo: POSIX leaves echo‘s handling of backslashes implementation-defined, and dash’s builtin turns pa\nss into two lines. A here-string, sudo chpasswd <<< "alice:$pw", also keeps the password off the command line, but bash before 5.1 writes it to a temporary file. If the account only needs SSH access, skip the password: useradd without -p leaves it locked, and passwd(1) notes that a locked user may still log in with an SSH key.

Force a password change at the next login with chage -d 0

If the password only has to last until the first login, expire it as soon as it is set:

sudo chage -d 0 alice

sudo passwd -e alice does the same. Run it last: chpasswd, passwd --stdin and usermod -p all stamp today’s date as the last change, so any of them would undo an earlier expiry. sudo chage -l alice then shows “password must be changed” for the last password change (see chage(1)).

Fix errors from passwd --stdin and chpasswd

Message Cause Fix
passwd: unrecognized option '--stdin' shadow older than 4.15 (Debian 12, Ubuntu 22.04 and 24.04) Use chpasswd
only root can use --stdin/-s option (Red Hat’s passwd: Only root can do that.) Not running as root Prefix the command with sudo
chpasswd: line 1: missing new password The line has no colon Write user:password
chpasswd: line 1: user 'alice' does not exist The account is missing (shown with -e, -c and on RHEL) Create it first with useradd
chpasswd: (line 1, user alice) password not changed PAM refused the change; the lines above give its reason, such as User not known to the underlying authentication module Fix what those lines report
Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.