sodcalculator

10 Tech How-To

How Linux Stores & Manages User Passwords

Guide 10-59016 · Updated · Step by step

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

  1. 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/passwd file is world-readable, so it contains only an x placeholder in the password field, indicating that the actual hash is in /etc/shadow, which is readable only by root and the shadow group.
  2. View the /etc/passwd file
    Run cat /etc/passwd to 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.
  3. View the /etc/shadow file
    Use sudo cat /etc/shadow to 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.
  4. Understand the hash format
    The hash field typically looks like $type$salt$hash. The $type indicates 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.
  5. Change a user’s password
    Use the passwd command. As root, run sudo passwd username to set or change a password for a specific user. The system will prompt for the new password and then update the /etc/shadow file automatically, generating a new salt and hash.
  6. Manage password aging
    Use the chage command to view or modify password expiration settings. For example, sudo chage -l username lists current aging information, and sudo chage -M 90 username sets the maximum number of days a password remains valid to 90.
  7. Lock and unlock accounts
    To lock a user account (prevent login), use sudo passwd -l username. This adds a ! prefix to the hash in /etc/shadow. To unlock, use sudo passwd -u username.
  8. 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 like password [success=1 default=ignore] pam_unix.so obscure sha512. You can change sha512 to yescrypt or bcrypt if supported, but be cautious: changing the algorithm may affect password verification for existing accounts.
  9. 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. The pam_unix.so module is responsible for verifying passwords against /etc/shadow. Other modules like pam_pwquality.so enforce password strength rules.
  10. Check password strength policies
    Password quality requirements are often configured in /etc/security/pwquality.conf or via the pam_pwquality module. You can set minimum length, complexity, and other rules there. For example, minlen = 12 sets a minimum password length.

Common Mistakes

  • Editing /etc/shadow manually – Always use passwd or chage instead 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 -l for 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 vipw and sudo vigr to 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 with sudo 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 passwd after remounting the filesystem as read-write.
  • Hashes show as ! or * – This means the account has no password or is locked. Set a new password with sudo 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

The whole Tech How-To bed →