How to Change User Password in Linux
- Change the Password of the Current User in Linux
- Change the Password of Other Users in Linux
- Expire the Password After Setting a Password
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.
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
Suraj Joshi is a backend software engineer at Matrice.ai.
LinkedIn