Linux stores user passwords in a hashed, salted format rather than as plain text, and manages them through a combination of system files, the Pluggable Authentication Modules (PAM) framework, and commands like passwd. This guide explains the underlying mechanisms and walks through the practical steps to inspect and manage password storage on a typical modern Linux system.
What You’ll Need
- A Linux system with a standard installation (e.g., Ubuntu, Debian, Fedora, CentOS).
- Root or sudo access to view password hashes and modify password settings.
- Basic familiarity with the terminal and command-line tools.
Step-by-Step
- Understand the key files
Linux stores user account information in two main files:/etc/passwd(user account details) and/etc/shadow(password hashes and aging information). The/etc/passwdfile is world-readable, so it contains only anxplaceholder in the password field, indicating that the actual hash is in/etc/shadow, which is readable only by root and the shadow group. - View the /etc/passwd file
Runcat /etc/passwdto see the structure. Each line has seven colon-separated fields: username, password placeholder, UID, GID, GECOS (full name/comment), home directory, and login shell. For example:jdoe:x:1001:1001:John Doe:/home/jdoe:/bin/bash. - View the /etc/shadow file
Usesudo cat /etc/shadowto see the password hashes. Each line has nine fields: username, password hash, last change date, minimum days, maximum days, warning days, inactive days, expiry date, and a reserved field. The password hash field is the core of password storage. - Understand the hash format
The hash field typically looks like$type$salt$hash. The$typeindicates the hashing algorithm:$1$is MD5,$2y$or$2a$is bcrypt,$5$is SHA-256,$6$is SHA-512, and$y$is yescrypt (used on newer systems). The salt is a random string added to the password before hashing to prevent rainbow table attacks. The final part is the resulting hash. - Change a user’s password
Use thepasswdcommand. As root, runsudo passwd usernameto set or change a password for a specific user. The system will prompt for the new password and then update the/etc/shadowfile automatically, generating a new salt and hash. - Manage password aging
Use thechagecommand to view or modify password expiration settings. For example,sudo chage -l usernamelists current aging information, andsudo chage -M 90 usernamesets the maximum number of days a password remains valid to 90. - Lock and unlock accounts
To lock a user account (prevent login), usesudo passwd -l username. This adds a!prefix to the hash in /etc/shadow. To unlock, usesudo passwd -u username. - Configure password hashing algorithm
The default hashing algorithm is set in the PAM configuration. On most systems, you can find it in/etc/pam.d/common-password(Debian/Ubuntu) or/etc/pam.d/system-auth(RHEL/Fedora). Look for a line likepassword [success=1 default=ignore] pam_unix.so obscure sha512. You can changesha512toyescryptorbcryptif supported, but be cautious: changing the algorithm may affect password verification for existing accounts. - Understand PAM’s role
PAM (Pluggable Authentication Modules) is the framework that handles authentication. It reads the configuration files in/etc/pam.d/to decide which modules to use. Thepam_unix.somodule is responsible for verifying passwords against /etc/shadow. Other modules likepam_pwquality.soenforce password strength rules. - Check password strength policies
Password quality requirements are often configured in/etc/security/pwquality.confor via thepam_pwqualitymodule. You can set minimum length, complexity, and other rules there. For example,minlen = 12sets a minimum password length.
Common Mistakes
- Editing /etc/shadow manually – Always use
passwdorchageinstead of manually editing the shadow file. A typo can lock a user out or break authentication. - Using weak hashing algorithms – MD5 is outdated and vulnerable. Ensure your system uses at least SHA-512 or yescrypt.
- Ignoring file permissions – /etc/shadow must remain readable only by root. If permissions are too open, any user could see hashes and attempt offline cracking.
- Assuming passwords are encrypted – They are hashed, not encrypted. There is no way to decrypt them; verification is done by hashing the input and comparing.
- Forgetting to lock accounts – When you delete a user, their account may still exist in /etc/passwd. Always remove or lock accounts properly.
Tips
- Use strong, unique passwords and enforce password complexity through PAM.
- Regularly audit password aging with
chage -lfor all users. - Consider using key-based SSH authentication to reduce reliance on passwords for remote access.
- Backup /etc/shadow and /etc/passwd before making changes.
- Use
sudo vipwandsudo vigrto safely edit /etc/passwd and /etc/group; they lock the files and run syntax checks.
Troubleshooting
- User cannot log in – Check if the account is locked by looking for a
!or*in the shadow file. Unlock withsudo passwd -u username. Also verify the shell in /etc/passwd is valid (e.g., /bin/bash). - Password change fails – Ensure the new password meets the complexity requirements set in pwquality.conf. If the error is about the hash algorithm, check that the PAM configuration is correct.
- Forgot root password – Boot into recovery mode (or single-user mode) and reset the root password using
passwdafter remounting the filesystem as read-write. - Hashes show as
!or*– This means the account has no password or is locked. Set a new password withsudo passwd username. - PAM errors – If you see PAM-related errors, check the syntax of files in /etc/pam.d/. A misconfigured line can prevent all logins. Use a live CD or recovery mode to fix.
Frequently Asked Questions
Q: Are Linux passwords encrypted or hashed?
A: They are hashed, not encrypted. Hashing is one-way; you cannot recover the original password from the hash. Verification involves hashing the entered password with the same salt and comparing.
Q: What is the salt in a password hash?
A: A salt is a random string added to the password before hashing. It ensures that identical passwords produce different hashes, making rainbow table attacks ineffective.
Q: How do I find out which hashing algorithm is being used?
A: Look at the prefix of the hash in /etc/shadow. For example, $6$ indicates SHA-512, $y$ indicates yescrypt, and $2y$ indicates bcrypt.
Q: Can I manually set a password hash in /etc/shadow?
A: Yes, but it is risky. You can generate a hash with tools like mkpasswd or openssl passwd, but using passwd is safer and less error-prone.
Q: What is the difference between /etc/passwd and /etc/shadow?
A: /etc/passwd contains public user information and is readable by all. /etc/shadow contains password hashes and aging data, readable only by root and the shadow group, to prevent offline cracking.
Q: How do I enforce password expiration for all users?
A: Use chage with the -M option for each user, or set a default in /etc/login.defs (e.g., PASS_MAX_DAYS 90). New users will inherit the default.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Custom Alphanumeric Passcode iPhone: How to Set
- HomePod Software Update: How to Install
- How to Shuffle Photos in Slideshow on iPhone
- Quick Look Not Working on Mac? Fixes That Work
- How to See Deleted WhatsApp Messages on iPhone
- QUIC Protocol Error in Windows 10: Fixes