How to Change User Password in Linux
We will show you how to change user password in Linux. Changing user passwords in Linux could be one of the most common tasks you will have to perform while you are administering a multi-user server. This is a very simple task though and in this tutorial, we will show you how to change the user password on a Linux VPS regardless of which distribution you are currently using.
To be able to run commands on the server you need to connect to the Linux server via SSH. Note that only the root user, as well as system users with sudo privileges, can change a password for another user. If you log in to the server with a non-sudo user, you can only change your current user’s password.
How to change your current user password in Linux
To change the password of your current user, run the following command in the terminal:
You will be asked to enter your current password for verification, then you can enter the new password twice. In the example below, we are changing the password of the user1 user:
[user1@vps /]$ passwd Changing password for user user1. Changing password for user1. (current) UNIX password: New password: Retype new password: passwd: all authentication tokens updated successfully.
The most common errors while setting up a new password for your system user are the following:
- BAD PASSWORD: The password is shorter than 8 characters - BAD PASSWORD: The password fails the dictionary check - it is too simplistic/systematic - BAD PASSWORD: The password is the same as the old one
If you get some of the above errors while setting up the new password, it means you are using old or weak password. Generally, a password utilizing at least 14 characters including alphanumeric and grammatical symbols is sufficient. Never use passwords based upon dictionary words or significant dates.
To generate a strong password from the command line you can use one of the following commands:
openssl rand -base64 14
< /dev/urandom tr -dc A-Za-z0-9 | head -c14; echo
This will generate 14 characters random string which you can use as your new password. Feel free to edit the commands accordingly if you want the password to consists of more characters.
How to change the password for a different user in Linux
As we mentioned earlier in this blog post, you can only change the password for a different user if you are logged in as root or if your system user has sudo privileges. If you are logged in as root you should run the following command in the terminal:
Replace user1 with the name of the user that you want to change its password. When you are changing the password for a different user as root, you don’t have to specify the old password, and the output after successful password change should look like the one below:
[root@vps /]# passwd user1 Changing password for user user1. New password: Retype new password: passwd: all authentication tokens updated successfully.
If the user that you are currently logged in as has sudo privileges and you want to change the password for a different user, you must use sudo in your command:
sudo passwd user1
In our example below, we are logged in as sudouser, and we are changing the password for user user1:
[sudouser@vps /]$ sudo passwd user1 We trust you have received the usual lecture from the local System Administrator. It usually boils down to these three things: #1) Respect the privacy of others. #2) Think before you type. #3) With great power comes great responsibility. [sudo] password for sudouser: Changing password for user user1. New password: Retype new password: passwd: all authentication tokens updated successfully.
First, you will be prompted to enter the password of your current user to verify that you have sudo privileges and once the verification is successful you will be asked to enter the new password for the other user.
Again, make sure that you are using a strong password for all your users because using a weak password for some of your system users will put the other users at huge risk.