How to Change User Password in Linux

Suraj Joshi Feb 02, 2024
  1. Change the Password of the Current User in Linux
  2. Change the Password of Other Users in Linux
  3. Expire the Password After Setting a Password
How to Change User Password in Linux

We can change the password of users in Linux using the passwd command-line utility. We can only change our password if we are a regular user and to change the password of other users, we must be logged in as the root user.

Change the Password of the Current User in Linux

To change the password of the user currently logged in, we can simply use the passwd command without any options.

passwd

Output:

Changing password for zeppy.
(current) UNIX password: 
Enter new UNIX password: 
Retype new UNIX password: 

It will ask us to type the present password and if the present password is correct, we can set a new password.

If the password change is successful, we get passwd: password updated successfully as an output in the terminal at the end.

Note
new password and old password must differ by more than just case.

Change the Password of Other Users in Linux

To change the password of other users, we must be logged in as a superuser. To change the password of other users, we use the command:

sudo passwd <username>

<username> represents the name of the user whose password needs to be changed.

sudo passwd peter

Output:

Enter new UNIX password:
Retype new UNIX password:

Once the password change is successful, we get the following output:

passwd: password updated successfully

Expire the Password After Setting a Password

Passwords are valid until we don’t change them by default. In some cases, we need to reset passwords for others, and to allow them to reset the password themselves, we use the --expire option with the passwd command.

sudo passwd --expire DelftStack

It will expire the current password for the user DelftStack and the user DelftStack will be prompted to set a new password.

Output:

WARNING: Your password has expired.
You must change your password now and login again!
Changing password for DelftStack.
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Author: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn