Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

chpasswd

Set the passwords of many users at once, from lines user:password on standard input.

Level
Everyday Linux
Group
Users and groups
Needs root
Yes
Package
shadow-utils

What it does

chpasswd reads lines in the form USER:PASSWORD and sets each password, hashed with the method of /etc/login.defs (SHA-512 or yescrypt). It is the standard way to set passwords in scripts, Kickstart %post sections and provisioning tools, because it works the same on every Linux. With -e the passwords are already hashes.

How to write it

chpasswd [OPTION]...
echo 'USER:PASSWORD' | chpasswd

Options 7

Most used

Options

-e, --encrypted
The passwords are already hashed; store them as they are.
-c, --crypt-method METHOD
Hash method: SHA512, SHA256, YESCRYPT, MD5, DES, NONE.
-s, --sha-rounds ROUNDS
Number of rounds for SHA methods.
-m, --md5
Use MD5 (old, do not use).
-R, --root DIR
Work on the files under DIR (a chroot).
-P, --prefix DIR
Use the files under DIR without chroot.
-h, --help
Show help.

Examples 10

$ run as a normal user# run as root (or with sudo)

  1. 1Set one password in one line

    echo 'mia:RedHat-2026' | chpasswd
  2. 2Set many passwords from a file

    cat newpasswords.txt
    mia:Start-2026-a
    leo:Start-2026-b
    sara:Start-2026-c
    chpasswd < newpasswords.txt
    shred -u newpasswords.txt

    Remove the clear-text file at once.

  3. 3Create users and set passwords in a loop

    for u in mia leo sara; do useradd "$u"; echo "$u:Welcome-$RANDOM" | chpasswd; done
  4. 4Force a password change at the first login

    echo 'mia:Temp-2026' | chpasswd
    chage -d 0 mia
  5. 5Use a prepared hash (no clear text on the server)

    openssl passwd -6 'S3cret-2026'
    $6$Tz8...$Qw...
    echo 'mia:$6$Tz8...$Qw...' | chpasswd -e
  6. 6Choose the hash method

    echo 'leo:Long-Passphrase-2026' | chpasswd -c SHA512
  7. 7Set the root password inside a mounted system

    echo 'root:Recovery-2026' | chpasswd -R /mnt/sysroot
  8. 8Random passwords for a list of users, saved for hand-over

    for u in mia leo; do p=$(openssl rand -base64 12); echo "$u:$p" | chpasswd; echo "$u $p" >> /root/handover.txt; done
    chmod 600 /root/handover.txt
  9. 9Check that the password was set

    grep '^mia:' /etc/shadow | cut -d: -f1,2 | cut -c1-20
    passwd -S mia
  10. 10Kickstart %post example

    cat ks-post.txt
    %post
    echo 'admin:Change-me-2026' | chpasswd
    chage -d 0 admin
    %end

Watch out

  • Passwords typed on the command line land in the shell history. Use a file with rights 600, or chpasswd -e with a hash.
  • pwquality rules are not checked by chpasswd when root runs it.

Files

/etc/shadow
Where the hashes are stored.
/etc/login.defs
ENCRYPT_METHOD (default hash method).

Tips to remember

  • chpasswd is the safe replacement for passwd --stdin in scripts: it works on every distribution.

Exit codes

0Success.
1Error (unknown user, bad line, no rights).

Full manual

This page explains the command in simple words. The original manual has every detail.

In your terminal: man 8 chpasswd