How to set and change a user password in Linux

Both Linux and UNIX operating systems use the passwd command to change the user password. The password used to update the user's authentication token (password) is stored in the / etc / shadow file.

The passwd command changes the password for the user and group accounts. Ordinary users can only change the password for their own account, advanced users (or root) can change the password for any account. The administrator of a group can change the password for the group.

The passwd command also changes account information, such as the user's full name, the user's login shell, or the password expiration date and time.

Set user password in Linux

Enter the following passwd command to change your own password:

$ passwd

Sample outputs:

Changing password for vivek (current) UNIX password: Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully

The user is first prompted for his or her old password if available. This password is then encrypted and compared with the stored password. The user has only one chance to enter the correct password. Advanced users are allowed to skip this step in order to change the forgotten password. New passwords are checked for complexity. As a general rule of thumb, a password must consist of 10 to 20 characters that include one or more of the following conditions:

  1. Include lower case letters
  2. Include capital letters
  3. Digits 0 to 9
  4. Punctuation / space character

Change the password for another user account in Linux

You need to log in as the root user, enter the following command to change the password for the vivek user :

# passwd vivek

Or:

$ sudo passwd vivek

Sample output:

Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully

In which, vivek is the username or account name.

Passwords do not appear on the screen when you enter them. For example:

How to set and change a user password in Linux Picture 1How to set and change a user password in Linux Picture 1

Change the Linux group password

When the -g option is used, the password for the group will be changed. In this example, change the password for the sales group :

# passwd -g sales

The -r option is used with the -g option to remove the current password from the group. This gives the group access to all members. The -R option is used with the -g option to restrict the group to all users.

Change user password on Linux

As a Linux system administrator (sysadmin), you can change the password for any user on your server. To change the password on behalf of the user:

Step 1. First, log in or use 'su' or 'sudo' to access the 'root' account on Linux. Run the command:

sudo -i

Step 2. Then type passwd tom to change the password for the tom user .

The system will prompt you to enter your password twice.

To change or set a new root (superuser) password:

$ sudo passwd

Force Linux users to change their password the next time they log in

By default, Linux passwords never expire for a user. However, you can force the user to change their password the next time you log in via the GUI or CLI method. The syntax is very simple:

$ sudo passwd -e {username} $ sudo passwd --expire {username}

For example:

$ sudo passwd -e marlena

The system will confirm it:

passwd: password expiry information changed.

When a user tries to log in via the ssh command, they should see the following on the screen:

$ ssh marlena@192.168.2.25 Last login: Fri Dec 4 04:12:48 2020 from 192.168.2.105 WARNING: Your password has expired. You must change your password now and login again! Changing password for marlena. Current password: New password: Retype new password: passwd: password updated successfully Connection to 192.168.2.25 closed.

Lock and unlock your account's user password

Note that the following local commands do not disable the account. Users can still log in with another authentication code, such as the SSH key. To disable the account, the administrator should use the command usermod --expiredate 1 {username} or sudo passwd --expire {username} .

Additionally, users with a locked password are not allowed to change their password to comply with the privacy policy set by sysadmin.

You can lock the password as follows:

$ sudo passwd -l {username}

This option disables the password by changing it into a value does not match the value can be encrypted (it adds mark! At the beginning of the password in the file / etc / shadow. To unlock password, to try on:

$ sudo passwd -u {username}

The above command option re-enables the password by changing the password back to the previous value. In other words, return the value before using the -l option .

4 ★ | 2 Vote