The Wayback Machine - https://web.archive.org/web/20240927010414/https://www.geeksforgeeks.org/passwd-command-in-linux-with-examples/
Open In App

How to Change User Password in Linux | passwd Command

Last Updated : 19 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Securing user accounts is a fundamental aspect of maintaining a robust and secure Linux system. One essential task is changing user passwords regularly to prevent unauthorized access. The passwd command in Linux provides a straightforward and effective way to modify user passwords. This article will guide you through the process of changing user passwords using the passwd command, offering insights into its various options and best practices.

Understanding the `passwd` Command

The passwd command in Linux is a powerful tool that allows system administrators and users to manage password-related tasks. Its primary purpose is to change user passwords, but it offers additional functionalities such as updating password aging policies, unlocking accounts, and more.

Basic Syntax of `passwd` in Linux

 passwd [options] [username] 

Here,

[options] = include various parameters to customize the password-changing process.

[username] = the target user account for which you want to change the password.

Options Available in `passwd` Command in Linux

Option

Description

-d, –delete

Deletes the user password, making the account password-less.

-e, –expire

Immediately expires the account password, prompting the user to change it on the next login.

-h, –help

Displays help related to the `passwd` command.

-i, –inactive

Sets the number of days after password expiration before the account is deactivated.

-k, –keep-tokens

Changes the password only if it is expired, keeping authentication tokens if not expired.

-l, –lock

Locks the password of the user, preventing login with a password. Other authentication methods like SSH keys can still be used.

-n, –mindays

Changes the minimum number of days between password changes, preventing the user from changing the password for a specified period.

-q, –quiet

Enables quiet mode, suppressing the usual message “Changing password for $user” during password change.

-r, –repository

Changes the password for a specified repository.

-R, –root

Applies changes in the specified directory and uses configuration files from that directory. This temporarily changes the root directory for the passwd process.

-S, –status

Shows the password status of a user, including last change date, minimum age, maximum age, warning period, and inactivity period.

-a, –all

Shows password status for all users. Requires the use of -S option.

-u, –unlock

Unlocks the password of an account, allowing login with the password.

-w, –warndays

Changes the number of days before password expiration to display a warning.

-x, –maxdays

Sets the maximum number of days a password remains valid. After MAX_DAYS, the user is forced to change the password.

1. How to Change User Password in Linux

To change a user password using the passwd command, follow these steps:

Step 1: Open a terminal on your Linux system.

Step 2: Type the following command and press Enter:

passwd 

how to change user password in Linux

how to change user password in Linux

  • If you are not the root user, you will be prompted to enter your current password to proceed.
  • You will then be prompted to enter the new password. Type the new password and press Enter.
  • Retype the new password when prompted to confirm.
  • The password for the specified user account is now changed.

Note: Linux does not display any characters on the screen while you type the password for security reasons.

2. How to Change Another User’s Password in Linux

To change the password for a user other than the one currently logged in, you can use the following steps:

Step 1: Open a terminal on your Linux system.

Step 2: Type the following command and press Enter:

passwd user1 

How to change another user password

How to change another user password

  • If you are not the root user, you may be prompted to enter your current password to proceed.
  • You will then be prompted to enter the new password. Type the new password and press Enter. Linux does not display any characters on the screen while you type the password for security reasons.
  • Retype the new password when prompted to confirm.
  • The password for the user “john” is now changed.

Note: sudo can be used to invoke root privileges by normal users, and can change the password for root itself. This is particularly helpful when a user is member of admin group (holds a position in sudoers list (/etc/sudoers) and can use commands with sudo) and the root password is not set, which is case with many common distributions of linux.

sudo passwd root 

3. How to Change the User Password Forcely

To force a user to change their password at the next login, use the following command:

passed -e user1

changing the user password forcely

changing the user password forcefully

This immediately expires the user’s password, requiring them to change it the next time they log in.

4. How to Lock and Unlock User Account in Linux

To lock or unlock a user account, use the following commands:

To lock the account:

passwd -l user2

The password for the user “user2” is now locked, preventing login.

To unlock the account:

passwd -u user2

The password for the user “user2” is now unlocked, allowing login again.

5. How to Set Password Expiry

The -x option allows you to set the maximum number of days a password is valid. Example:

passwd -x 30 user3

This sets a maximum password age of 30 days for the user “user3”

Processing in passwd command:

  • Verify current user password : Once the user enters passwd command, it prompts for current user password, which is verified against the password stored in /etc/shadow file user. The root user can bypass this step and can directly change the password, so as the forgotten passwords may be recovered.
  • Verify password aging information : In Linux, a user password can be set to expire after a given period of time. Also, a user can be prohibited to change his/her password for a period. This password aging information (and the password itself) is stored in a file /etc/shadow.
  • Change the password : After authentication, the user is prompted to enter the new password and verify it by retyping the password.
  • /etc/shadow file: The shadow file is a list of colon separated values with 9 fields, as shown below:

user1:$6$x8wAJRpP$EWC97sXW5tqac10Q2TQyXkR.1l1jdK4VLK1pkZKmA2mbA6UnSGyo94Pis074viWBA3sVbkCptSZzuP2K.y.an/:17887:0:99999:7:::

Processing in passed Command

Processing in passed Command

  • field 1: User name.
  • field 2: Encrypted Password.
  • field 3: Number of days since January 1, 1970 to when the password was last changed.
  • field 4: Minimum number of days for which password can not be changed. (value 0 means it can be changed anytime).
  • field 5: Number of days after password must be changed. (value 99999 means that the password never expires).
  • field 6: Number of days to warn user for expiring password.
  • field 7: Number of days after password expires that the account is disabled.
  • field 8: The number of days from January 1, 1970 to the date when an account was disabled.
  • field 9: This field is reserved for some possible future use.

Conclusion

In this article, we learned how to change user passwords in Linux using the passwd command. We explored its various features, from basic password changes to more advanced options like forcing updates, locking and unlocking accounts, and setting password expiry. By understanding these functionalities, Linux users and administrators can strengthen the security of their systems. Regularly updating passwords and following best practices play a crucial role in safeguarding against unauthorized access. The passwd command serves as a handy tool for managing these aspects, making it an essential part of maintaining a secure Linux environment.

How to Change User Password in Linux | passwd Command – FAQs

How to Change a User’s Password in Linux?

To change a user’s password in Linux, the passwd command is commonly used. If you are a regular user wanting to change your own password, simply type:

passwd

You will be prompted to enter your current password for verification, followed by the new password twice (the second time for confirmation).

If you are an administrator (or have sudo privileges) and need to change the password for another user, you specify the username after the passwd command:

sudo passwd username

This will prompt you to enter a new password for the specified user, without needing to enter the current password.

What is the passwd command used for besides changing passwords?

Besides changing user passwords, the passwd command also allows for managing various aspects of users’ passwords, including:

  • Setting Password Expiry Information: Administrators can use passwd to change the password aging/expiry policy.
  • Locking and Unlocking User Accounts: passwd can disable or enable user accounts by locking or unlocking them.
  • Changing Account or Password Expiry Dates: It allows setting or changing the date when a user’s account will be disabled or when the password must be changed.

How to force a user to change their password at next login?

To force a user to change their password at the next login, use the passwd command with the --expire option:

sudo passwd --expire username

This command effectively expires the password immediately, prompting the user to set a new password the next time they log in.

What are some common options for the passwd command?

The passwd command includes several options that provide control over user passwords and accounts:

  • -d: Deletes a password, making the account passwordless, subject to system policy.
  • -e, –expire: Forces the user to change their password at the next login.
  • -l, –lock: Locks the user account.
  • -u, –unlock: Unlocks the user account.
  • -n, –mindays DAYS: Sets the minimum number of days between password changes to DAYS.
  • -x, –maxdays DAYS: Sets the maximum number of days the password is valid.
  • -w, –warndays DAYS: Sets the number of days a user is warned before their password expires.
  • -i, –inactive INACTIVE: Sets the number of days after a password expires until the account is permanently disabled.


Previous Article
Next Article

Similar Reads

How to Fix “passwd: Authentication token manipulation error” in Linux
Encountering the "passwd: Authentication token manipulation error" when trying to change your password in a Linux system can be frustrating. This error usually indicates that the password change attempt was unsuccessful, and it can happen for various reasons. There can be many reasons for that like if you don't supply the password output will give
3 min read
Understanding the /etc/passwd File
The /etc/passwd file is the most important file in Linux operating system. This file stores essential information about the users on the system. This file is owned by the root user and to edit this file we must have root privileges. But try to avoid edit this file. Now let's see actually how this file look This file contains one entry per line. Tha
3 min read
How to Force User to Change Password at Next Login in Linux
For security reasons, you must frequently change the password of accounts on the system. In this article, we are going to see how we can force the user to change the password on the next login in Linux. We can do that by two methods, one is using `passwd` command and the other is by using the `chage` command. Using passwd commandTo force a user to
3 min read
How to Change Root Password in Kali Linux?
Securing your Kali Linux system is crucial for protecting sensitive data and maintaining the integrity of your environment. One of the fundamental aspects of system security is regularly changing the root password. The root account in Linux has superuser privileges, making it a prime target for attackers. By changing the root password periodically,
5 min read
How to add User in Linux | useradd Command
useradd is a command in Linux that is used to add user accounts to your system. It is just a symbolic link to adduser command in Linux and the difference between both of them is that useradd is a native binary compiled with the system whereas adduser is a Perl script that uses useradd binary in the background. It makes changes to the following file
5 min read
How to Delete User in Linux | userdel Command
Managing user accounts is an essential aspect of Linux system administration. Understanding how to delete a user in Linux is crucial, whether you need to remove an unused account, revoke access for a departing employee, or clean up your system for security reasons. Here, we will explore the 'userdel' command, a powerful tool in Linux for removing u
6 min read
Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
How to Change The Primary Group of a User in Linux?
In the field of Linux system administration, learning the modification of the primary group is essential. In this article, we are going to deliver concise details about user groups and how to manage them. What is a Primary Group?In Linux, every user is associated with a primary group. The primary group is a fundamental concept in Unix-like operatin
7 min read
How to Change File Ownership in Linux | chown Command
In the Linux operating system, file ownership is a crucial aspect of system security and user management. The `chown` command, short for "change owner," is a powerful tool that allows users to change the owner of files and directories. This command is particularly useful in scenarios where administrators need to grant or revoke access to specific r
9 min read
How to Change the Directory in Linux | cd Command
Mastering the cd command in Linux is a fundamental skill for anyone working in the Linux environment. The cd command allows you to change directories in Linux, making it easier to navigate through the file system and manage your files efficiently. Whether you're a beginner or an experienced user, understanding how to use the cd command in Linux is
6 min read
GithubFind3r - Fast Command Line Repo/User/Commit Search Tool
GithubFind3r is an automated tool developed in Node JS which is a CLI-based tool used to get information about the Repositories, Commits, and information about the Users from GitHub. GithubFind3r is available on GitHub, it's free and open-source to use. The GithubFind3r tool displays the results in a more sophisticated and more attractive manner. N
2 min read
Kali Linux - Password Cracking Tool
Password cracking is a mechanism that is used in most of the parts of hacking. Exploitation uses it to exploit the applications by cracking their administrator or other account passwords, Information Gathering uses it when we have to get the social media or other accounts of the C.E.O. or other employees of the target organization, Wifi Hacking use
5 min read
How to reset the root password of RedHat/CentOS Linux
RedHat Linux is a worldwide used OS in servers and networking devices. Red Hat lists Adobe, Ampersand and many among the top 500 fortune companies. They list Cisco, Dell, HP, and IBM as partners. Resetting the root password of RHEL is not a difficult task but rather you need to know 4 commands and you are into the root account! Root account in Linu
2 min read
Fcrackzip Tool - Crack a Zip File Password in Kali Linux
The fcrackzip utility and wordlists are included by default in Kali to crack passwords for these compressed files. Because of their compact size and encryption algorithm, we frequently use zipped files. These zipped files have a password protection feature that ensures the files' confidentiality. When you've forgotten your password and are stuck tr
3 min read
Password Cracking with Medusa in Linux
In today's world, the majority of people are generally aware that malicious users and hackers can steal their confidential information through various attacks. However, most people don't really understand the specific attacks that they are vulnerable to. So in this article, We are going to learn about Password Attacks. Password Attacks means a thir
3 min read
How to Create a Password Protected ZIP File in Linux?
Linux provides Zip command to work with a file like compressing the file and decompressing with a password. It's not come with built-in you need to install from an external source. The Zip command has two different utility(zip and unzip). zip is used for compressing the file and unzip is used for decompressing the file. Installation Use this comman
2 min read
How to Show Asterisks While Typing Sudo Password in Linux?
In this article, we are going to see how to display asterisks as feedback when you type the password in the terminal in Linux. Here the user has invoked the sudo command to install vim text editor, but the Asterisks are not shown here when the user typed a password. The password feedback feature can be enabled in the /etc/sudoers file but after cre
1 min read
Wfuzz Download - Web Application Password Cracker in Kali Linux
Brute-Forcing is the technique to discover the hidden directories and files on the target server. We can even brute-force usernames and passwords. All this process is done through an automated tool. Quickly Request-Response methodology is executed on the domain. Wfuzz tool is an automated tool used to perform all types of brute-forcing on the targe
2 min read
How to Reset a Lost Password in Ubuntu and Other Linux Distributions
This article explains how to reset lost a password in a Linux distribution. If you have forgotten your Linux password, this article is undoubtedly for You. This guide uses Ubuntu 23.04, but the same method applies to other versions of Ubuntu and other Linux distributions. Reset password using recovery mode.Step 1: During startup, from the Grub menu
3 min read
How to Reset Forgotten Password on Kali Linux
Kali Linux is a Linux distribution used in the Cybersecurity domain. It is maintained and funded by Offensive Security. Kali Linux is Debian based and it uses the Debian repository for most of its packages. This Linux distribution is designed for digital forensics and penetration testing. It has 600+ Penetration testing and network security tools p
4 min read
Resetting a root password in Linux without external media
We have to follow 8 steps to crack root password without using any external media Step-1 : Reboot the system and interrupt the boot loader countdown timer by pressing any key except enter. Step-2 :  Find the entry that is normally booted, and change it to halt execution during the initial ram disk. 2.1. Use the cursor keys to highlight the entry th
2 min read
Reset a lost Linux administrative password and Explanation
The recovery mode does not work always because many systems will ask for the root password for logging in. Now, since you do not know the root password in the first place, that trick will fail. In such a case, you can try these which is the easiest ways to get reset the linux password. 1. Use 'sudo su' or 'sudo -i' sudo passwd root or passes sudo s
2 min read
How to change time zone from command line on RHEL 9
Correctly setting the time zone on a Red Hat Enterprise Linux (RHEL) 9 system is crucial for ensuring accurate time synchronization and proper functioning of various applications and services. While graphical tools are available for configuring the time zone, administrators often prefer command-line methods for efficiency and automation. Here, we w
4 min read
Configure Passwordless Sudo For A Specific User in Linux
Linux has a tough permission rule. Being a root user you are permitted to do anything however the normal user has only some allowed permissions. To run many other commands, they required to ask permissions from the superuser, using keyword sudo after which they need to enter the password. This requirement of the password after sudo command can be r
1 min read
Anonymity and Privacy For Linux User
Today, more than 3 billion of the world's population is connected to the virtual world known as the internet in some way or another. And the size of the hard-wares and data centers is on the increase every year due to insufficient users' information holding capacity. The lack of awareness and knowledge behind the proper functioning of the internet
6 min read
Deleting a User in Linux using Python Script
Deleting a user from your system or server via a python script is a very easy task. You just need to pass the username of the user and the script will remove the details and all the files of that user.This python script uses userdel Linux command to delete the user.You can directly use userdel command if you remember the command but if you don't th
2 min read
Creating a User With an Expiry Date in Linux
Linux is a multi-user system and thus, it allows more than one person to interact with the system at the same time. There are numerous ways in which we can create new users in Linux. The system administrator has the responsibility to manage different users of the system. For types of users in Linux refer Users in Linux System Administration. User a
2 min read
How to Block or Disable Normal User Logins in Linux?
Here we will see how to block or disable normal user logins in Linux. This is a good idea to prevent Normal users from connecting to your system. We will see how to block Normal user logins using /etc/nologin file. We are going to tell the users that what is actually happening by showing them a message. The main function of /etc/nologin file is to
2 min read
Scylla – Phone Number & User Information Gathering Tool in Kali Linux
Scylla is a free and open-source tool available on Github. Scylla is based upon the concept of Open Source Intelligence (OSINT). This tool is used for information gathering. Scylla is written in python language. You must have python language installed in your Kali Linux in order to use the Scylla tool. Scylla is an advanced tool that allows it's us
4 min read