Resetting and managing user passwords
Once several people work on a server, password resets and locked accounts become routine. These commands cover every case.
How do I set a new password for another user?
Run passwd with the user name as root or with sudo. You are not asked for the old password, because root may set any account's password. The user can log in with the new one immediately; running sessions continue unaffected.
sudo passwd lena # New password: # Retype new password:
For scripts or many accounts at once, chpasswd reads user:password pairs from standard input. Feed it from a protected file, never from the command line, which would leave the password in the shell history and in the process list.
install -m 600 /dev/null /root/newpw.txt printf 'lena:%s\n' "$(openssl rand -base64 18)" > /root/newpw.txt chpasswd < /root/newpw.txt && cat /root/newpw.txt && shred -u /root/newpw.txt
How do I make the user choose their own password?
Expire the temporary one so the next login forces a change. The person logs in with what you gave them and must pick a new password straight away; nobody else then knows it.
passwd --expire lena chage -l lena
Note that a forced change works only for logins that can prompt for it: an SSH key login skips passwords entirely, and the prompt appears the first time the user needs the password, for example for sudo.
Why is an account locked after failed attempts?
Because the faillock module counts failed logins and blocks the account for a while after too many. It is active by default on AlmaLinux, Rocky Linux and Fedora, and can be enabled on Debian and Ubuntu. Check the count and clear it to let the user in again.
faillock --user lena faillock --user lena --reset
The limits live in /etc/security/faillock.conf: deny sets the number of failures and unlock_time the lock duration in seconds. A value of three to five failures with a ten minute lock stops guessing without making typos painful.
How do I lock or unlock an account on purpose?
Use usermod or passwd to lock the password, and expire the account to block every login method including SSH keys. Locking only the password leaves key logins working, which surprises many administrators.
| Goal | Command | Effect on SSH keys |
|---|---|---|
| Disable password only | passwd -l lena | Key logins still work |
| Disable all logins | usermod -L -e 1 lena | Blocked |
| Re-enable | usermod -U -e -1 lena | Restored |
| Remove shell access | usermod -s /usr/sbin/nologin lena | Blocked for shell, SFTP depends on config |
How do I give or remove sudo rights?
Through group membership. Debian and Ubuntu use the sudo group; AlmaLinux, Rocky Linux and Fedora use wheel. Changes apply at the user's next login.
usermod -aG sudo lena # grant (wheel on Enterprise Linux) gpasswd -d lena sudo # revoke sudo -l -U lena # show what lena may run
For narrower rights, such as restarting one service, write a file in /etc/sudoers.d/ and always edit it with visudo -f, which checks the syntax before saving. A broken sudoers file can leave nobody with root rights except through rescue mode.
visudo -f /etc/sudoers.d/deploy # deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart app.service
How do I see who logged in and when?
Use last for successful logins and lastb for failed ones, both read from binary logs in /var/log. journalctl _COMM=sshd --since today shows the SSH side in more detail, including which key fingerprint was accepted. Reviewing these after a password reset confirms that the right person got in and nobody else tried the old password.
What if the only sudo user forgot their password?
Use root if you have its key, or rescue mode if not. From the rescue system, mount the root filesystem and run chroot /mnt passwd lena, exactly as for the root account in Change the root password. On SELinux systems, create /mnt/.autorelabel before returning to disk boot.
How should password policy look on a server?
Long passwords, keys for SSH and no forced periodic changes. Use pam_pwquality to require a minimum length of 14 or more characters, let people log in over SSH with keys only, and keep passwords for sudo prompts and the console of the rescue system. Remove accounts promptly when people leave with userdel -r, after copying anything you need from their home directory. The Hardening a new dedicated server article places these settings in the wider security picture.
# /etc/security/pwquality.conf minlen = 14 dictcheck = 1
Related pages
Change the root password
Set a new root password on a running server, or reset a forgotten one from the rescue system, and decide whether root needs a password at all.
Read moreCreate SSH keys
Generate an Ed25519 key pair on Windows, macOS or Linux, add it to your account before ordering, and manage keys on a running server.
Read moreHardening a new dedicated server
The first hour on a fresh machine: SSH keys, a firewall, automatic updates, brute force blocking and a habit of checking what listens.
Read moreFirst steps with a new server
The first hour after delivery: log in, check the hardware matches the order, update, add a user, lock down SSH and set a firewall.
Read moreQuestions and answers
How do I reset another user's password on Linux?
Run sudo passwd followed by the user name and type the new password twice. Root is not asked for the old one.
How do I force a password change at next login?
Run passwd --expire with the user name. The user must choose a new password the next time a password is requested.
Why does a locked password not stop SSH key logins?
passwd -l only disables the password. Expire the account with usermod -e 1 to block every login method.
How do I unlock an account after too many failed logins?
Run faillock --user with the name and --reset. Limits are set in /etc/security/faillock.conf.
Which group gives sudo rights?
The sudo group on Debian and Ubuntu, and the wheel group on AlmaLinux, Rocky Linux and Fedora.