In this article, we’ll explore how to change a user’s password in Linux with clear and concise command examples.
Checking Current User Password
Before diving into changing passwords, it’s a good practice to verify the current password for the user. The following command allows you to check the password status for a specific user:
sudo passwd -S username
Replace “username” with the actual username you want to check. The output will display information about the user’s password, including its status.
Changing the Password
To change a user’s password, the passwd
command comes to the rescue. Here’s the basic syntax:
sudo passwd username
After executing the command, you’ll be prompted to enter the new password twice for confirmation. Make sure to create a strong and secure password following your system’s password policy.
Forcing Password Expiry
In certain scenarios, administrators may want to force a user to change their password upon the next login for security reasons. The chage
command helps achieve this:
sudo chage -d 0 username
This command sets the last password change date to the epoch, forcing the user to change their password at the next login.
Expiring Passwords
If you wish to set an expiration date for a user’s password, you can use the chage
command again. The following example sets the maximum password age to 90 days:
sudo chage -M 90 username
Adjust the number according to your security policy.
Locking and Unlocking User Accounts
In some situations, you might need to temporarily lock a user account, perhaps due to security concerns. The passwd
command can be used to lock and unlock an account:
- To lock an account:
sudo passwd -l username
- To unlock an account:
sudo passwd -u username