How-to

The chpasswd Command: Set Passwords From Scripts

chpasswd reads user:password pairs from standard input and updates them in one go. It is the right tool for provisioning scripts, as long as the passwords never touch your shell history.

What does chpasswd do?

It reads lines in the form name:password from standard input and sets each password through PAM, hashing it with the system's default method. Unlike passwd, it does not ask interactively, which makes it suitable for automation, cloud-init style setup scripts and bulk account creation. It needs root.

How do I set one password safely?

The naive form puts the password in your shell history and in the process list:

# avoid: visible in history and ps
echo 'deploy:Summer2026!' | sudo chpasswd

Instead, read the secret without echoing it, or generate it:

read -rs -p 'New password: ' pw; echo
printf 'deploy:%s\n' "$pw" | sudo chpasswd; unset pw

pw=$(openssl rand -base64 18)
printf 'deploy:%s\n' "$pw" | sudo chpasswd
printf '%s\n' "$pw" > /root/deploy.pw; chmod 600 /root/deploy.pw; unset pw

printf is a shell builtin in bash, so the password does not appear as a separate process argument. Prefix commands with a space when HISTCONTROL=ignorespace is set if you must type something sensitive.

How do I set many passwords at once?

sudo install -m 600 /dev/null /root/batch.txt
sudo tee /root/batch.txt >/dev/null <<'EOF'
ana:gR7#kP2vXq9m
bo:Lt4$wN8zHc1e
chen:Yp6!dF3sJr5u
EOF
sudo chpasswd < /root/batch.txt
sudo shred -u /root/batch.txt

Create the file with tight permissions first, feed it to chpasswd, then shred it. Each account must already exist; chpasswd does not create users. Create them first with Create a Linux user. If one line fails, chpasswd reports it and continues with the rest.

Which hashing method is used?

By default the one set in PAM and /etc/login.defs: yescrypt on current Debian and Ubuntu, SHA-512 on the RHEL 9 family. You can force a method:

printf 'ana:%s\n' "$pw" | sudo chpasswd -c SHA512
printf 'ana:%s\n' "$pw" | sudo chpasswd -c YESCRYPT -s 7

-c picks the method and -s the cost or rounds, where supported. Leave the default unless you need compatibility with an older system that reads the same shadow file.

Can I feed already hashed passwords?

hash=$(openssl passwd -6 -salt "$(openssl rand -hex 8)" 'ExampleOnly-2026')
printf 'bo:%s\n' "$hash" | sudo chpasswd -e

-e tells chpasswd the input is already hashed, so it stores it as is. This lets a configuration tool ship hashes instead of plain text. For yescrypt hashes, mkpasswd -m yescrypt from the whois package on Debian generates them.

How do I force a change at next login?

sudo chage -d 0 ana
sudo chage -l ana | head -3

Setting the last change date to zero makes the system ask for a new password at the next password login. Pair it with a random first password sent through a secure channel. Note that SSH key logins skip the password prompt, so the forced change only happens when the user logs in with the password or runs sudo.

Should server users have passwords at all?

For SSH, keys are far stronger, and we recommend turning password login off entirely with Set up passwordless SSH. Passwords still matter for sudo, local consoles and some services that authenticate against PAM. A good pattern: key only SSH, a strong unique password per admin for sudo, and no password at all on service accounts.

Is there a newgrp style tool for groups?

Yes, chgpasswd sets group passwords from the same name:password format. Group passwords are rarely used on servers and are best left empty.

What errors might I see?

MessageCause
user 'x' does not existCreate the account first
line 3: missing new passwordA blank or malformed line in the input
Authentication token manipulation errorRead only root filesystem or a locked shadow file
BAD PASSWORD warningsPAM quality checks; chpasswd still applies the change when run as root

How do I set passwords on FreeBSD?

FreeBSD has no chpasswd, but its pw tool reads a password from a file descriptor. printf '%s\n' "$pw" | pw usermod ana -h 0 sets the password for ana from standard input, and -H 0 accepts an already hashed value instead. Accounts are stored in /etc/master.passwd, and pw rebuilds the password databases for you, so never edit that file without vipw, which does the same job there as on Linux.

Machines for Provisioning Your Own Accounts

ServerProcessorCores / threadsMemoryStorageNetworkPer monthSmallest order
E1Intel Xeon E3-1240 v34 / 816 GB DDR32x 240 GB SSD2x 1 Gbps$30.52$91.56 (3 months)
S1Intel Xeon E-2276G6 / 1264 GB DDR41x 1 TB NVMe2x 10 Gbps$53.32$53.32 (1 month)
S4Intel Xeon E-2388G8 / 1664 GB DDR41x 1 TB NVMe2x 10 Gbps$87.52$87.52 (1 month)

Keep Reading

Create a Linux user

Create human and service accounts with useradd: home folders, shells, UIDs, groups, expiry, skeleton files and system accounts.

Open

The /etc/passwd file

Read the seven fields of /etc/passwd, see how it links to /etc/shadow and /etc/group, and edit accounts safely with vipw and the user tools.

Open

Set up passwordless SSH

Go from a root password to key-only access in one careful pass: user, key, agent, then switch off every password method on the server.

Open

Linux user management

The full life of an account on a server: create, group, change, expire, lock, audit and delete, with the files behind each step.

Open

FAQ

Quick Answers

No. It only changes passwords of existing accounts.

Read them with read -rs, generate them with openssl, or use a file with mode 600.

Yes, with -e. Generate the hash with openssl passwd -6 or mkpasswd.

The system default: yescrypt on current Debian and Ubuntu, SHA-512 on RHEL 9 family releases.

Run chage -d 0 username after setting it.