chgpasswd - Linux


Overview

chgpasswd is a command-line utility designed to facilitate the password changes for multiple user accounts in a single operation. It’s commonly employed by system administrators to efficiently manage user passwords in bulk.

Syntax

chgpasswd [options] [username1 username2 ...]

Options/Flags

  • -e: Expire passwords for specified users upon their next login.
  • -m: Min expires – set the minimum number of days before the password can be changed again.
  • -M: Max expires – set the maximum number of days after which the password must be changed.
  • -d: Delete the passwords for the specified users, making their accounts unusable until a new password is set.

Examples

Simple usage:

chgpasswd user1 user2

This command will prompt the user to enter new passwords for both user1 and user2.

Excluding specific users:

chgpasswd --exclude user1 user2 user3

This command will expire the passwords of user2 and user3 but leave user1‘s password unchanged.

Common Issues

User doesn’t receive password change prompt:

  • Ensure that the user’s shell is configured to read the /etc/shadow file (e.g., /bin/bash).
  • Verify that the system’s PAM configuration is set up correctly.

Command fails with "permission denied":

  • Check if the user running chgpasswd has sufficient privileges (typically root).

Integration

chgpasswd can be integrated with other commands for automating password management tasks. For example:

find /home -type d | cut -d'/' -f3 | xargs chgpasswd

This command will change the passwords of all user accounts whose home directories exist in the /home directory.

Related Commands

  • passwd: Change the password of a single user.
  • chage: Manage password aging and expiration policies.
  • pwgen: Generate strong, random passwords.