passwd - Linux


Overview

The passwd command in Linux is primarily used for changing the password of user accounts. It’s a vital tool for managing security and user access controls on Unix-like operating systems. The command can also display password aging information, set or change the account expiration, and enforce password policies.

Syntax

The general syntax for the passwd command is as follows:

passwd [options] [username]
  • username: Optional; specifies the user account for which to change the password. If omitted, the command will apply to the current user.

Options/Flags

Here are some of the commonly used options and flags for passwd:

  • -k: Only set a password if none is set.
  • -l: Lock the specified account by disabling the password.
  • -u: Unlock the specified account.
  • -d: Delete the current password for the specified user, making the account passwordless.
  • -e: Expire the user password, forcing the user to change it on the next login.
  • -S: Display password status information for a specified user.
  • -n: Set the minimum number of days between password changes.
  • -x: Set the maximum number of days a password will remain valid before it must be changed.
  • -w: Set the number of days of warning before a password change is required.

Examples

  1. Changing Your Own Password:

    passwd
    

    Simply type passwd and follow the prompts to change your own password.

  2. Changing Another User’s Password (as root):

    sudo passwd username
    

    Replace username with the actual user name.

  3. Locking and Unlocking User Accounts:

    sudo passwd -l username
    sudo passwd -u username
    

    Lock or unlock the account of username.

  4. Setting Password Expiry:

    sudo passwd -e username
    

    Force username to change their password upon next login.

Common Issues

  • Permission Denied: Normal users may try to change others’ passwords without proper rights. Ensure you are using sudo if trying to change another user’s password.
  • Weak Password: passwd refuses weak passwords by default depending on system configuration. Choose stronger passwords or adjust password policy settings.

Integration

The passwd command can be integrated with scripts or combined with other commands. For example, to change passwords in bulk, one might use:

for user in user1 user2 user3; do
  echo "NewPassword123" | passwd --stdin $user
done

Additionally, passwd can be part of user account management scripts that handle setups, deletions, locks, and unlocks.

  • chage: Modify user password expiry information.
  • usermod: Modify user account details.
  • whoami: Displays the username of the current user.

Further reading and resources on passwd can be found in the official Linux man pages (man passwd), or the documentation available on most Linux distributions.