passwd Command in Linux

Learn via video courses
Topics Covered

Overview

The passwd command in Linux is a powerful tool used for managing user passwords. It is primarily designed to change the user's password but also allows administrators to modify other password-related parameters such as expiration date and locked status. In this article, we will discuss the syntax, options, and various use cases of the passwd command.

Linux passwd Command Syntax

The syntax for the passwd command is as follows:

Where:

  • options: Options are flags that modify the behavior of the passwd command. They can be used to change password expiration settings, lock/unlock user accounts, or display account status.
  • user: The user whose password is being modified. If no user is specified, the command will assume the user running the command.

passwd Command Options:

  • -l, --lock: Locks the specified user's account, preventing them from logging in.
  • -u, --unlock: Unlocks the specified user's account, allowing them to log in again.
  • -e, --expire: Immediately expires the specified user's password, requiring them to set a new one on their next login.
  • -S, --status: Displays the password status of the specified user.

Example Usages

  • Change current user's password:

    Output:

    Explanation: The passwd command without any arguments allows the current user to change their password. They will be prompted to enter their current password and then enter the new password twice.

  • Lock a user account:

    Output:

    Explanation: The -l flag locks the specified user's account, preventing them from logging in. This command requires root privileges (sudo).

Tips

  • Always use strong and unique passwords for each user account.

  • Regularly change passwords and enforce password expiration policies.

Advanced Use Cases of passwd Command in Linux

  • Unlock a user account:

    Output:

    Explanation: The -u flag unlocks the specified user's account, allowing them to log in again. This command requires root privileges (sudo).

  • Expire a user's password:

    Output:

    Explanation: The -e flag immediately expires the specified user's password, forcing them to set a new one on their next login. This command requires root privileges (sudo).

  • Display password status:

    Output:

    Explanation: The -S flag displays the password status of the specified user. The output shows the username, password status (P for set), last password change date, minimum age, maximum age, warning period, and inactivity period. This command requires root privileges (sudo).

Conclusion

  • passwd command is used to manage user passwords in Linux.

  • It allows changing, locking, unlocking, and expiring passwords.

  • Always use strong and unique passwords for security purposes.

  • Regularly update passwords and enforce expiration policies.