Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

chpasswd

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

Niveau
Dagelijks Linux
Groep
Gebruikers en groepen
Root nodig
Ja
Pakket
shadow-utils

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Wat het doet

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.

Zo schrijf je het

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

Opties 7

Meest gebruikt

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.

Voorbeelden 10

$ als gewone gebruiker# als root (of met 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

Let op

  • 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.

Bestanden

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

Tips om te onthouden

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

Exitcodes

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

Volledige handleiding

Deze pagina legt het commando uit in eenvoudige woorden. De originele handleiding heeft elk detail.

In je terminal: man 8 chpasswd