Home >Operation and Maintenance >Linux Operation and Maintenance >How to change account password in Linux? (code example)
Both Linux and UNIX-like operating systems use the passwd command to change user passwords. This article will introduce how to use the passwd command to change user passwords. I hope it will be helpful to you.
In Linux, encrypted user passwords and other password-related information are stored in the /etc/shadow file. You can use the passwd command to change the user password.
When changing your password, be sure to use a strong and unique password. Having a strong password is the most important thing you can do to protect your account. Strong passwords usually contain at least 16 characters, using at least one uppercase letter, one lowercase letter, one number and one special character. [Video tutorial recommendation: Linux tutorial]
Change personal account password
Ordinary users want to change their personal Account password, you only need to run the passwd command without any other commands:
$ passwd
Sample output:
Changing password for nick (current) UNIX password: Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully
The system will prompt us to enter the current password first. If the password is correct, it will ask Re-enter and confirm the new password. The next time you log in to the Linux system, you can use the new password to log in.
Note: When entering the password, the password will not be displayed on the screen.
Change another user’s password
As a normal user, you can only change your own password password. However, root users and users with sudo permissions can change the passwords of other users.
We need to log in as the root user, type the following command to change the password of user nick:
$ passwd nick
Or use the sudo command
$ sudo passwd nick
Output: The system will prompt for input and confirmation New password
Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully
Force user to change password on next login
By default, passwords are set to never expire. If you want to force a user to change their password on the next login, you need to use the passwd command with --expireoption, followed by the user's username, for example:
$ sudo passwd --expire nick
This will immediately cause the user account's password to expire.
The next time the user tries to log in with the old password, a message will be displayed forcing them to change their password:
$ ssh nick@192.168.121.209
Output:
WARNING: Your password has expired. You must change your password now and login again! Changing password for nick. (current) UNIX password: Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully Connection to 192.168.121.209 closed.
After the user sets a new password, it will be closed connect.
The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !
The above is the detailed content of How to change account password in Linux? (code example). For more information, please follow other related articles on the PHP Chinese website!