BLOG POSTS
    MangoHost Blog / Create or update passwords for existing users in Linux – passwd
Create or update passwords for existing users in Linux – passwd

Create or update passwords for existing users in Linux – passwd

Passwd is a package in Linux that is used to create or update passwords for existing users. It is a command-line tool that allows users to change their passwords or update the passwords of other users, provided they have the necessary permissions.

The passwd package is an essential part of any Linux distribution as it ensures the security of user accounts by allowing users to set strong passwords and regularly update them. It is used in various scenarios, such as when a user forgets their password and needs to reset it, or when a system administrator needs to update the passwords of multiple users.

The passwd package is written in the C programming language and is typically included as part of the core utilities in a Linux distribution.

Official page of passwd

The official page of passwd can be found at the following link: https://man7.org/linux/man-pages/man1/passwd.1.html

Installation

The passwd package is usually pre-installed on most Linux distributions. However, if it is not installed, it can be easily installed using the package manager of the respective distribution.

Installing passwd on Ubuntu/Debian

To install passwd on Ubuntu or Debian, open a terminal and run the following command:

sudo apt-get install passwd

Installing passwd on CentOS/RHEL

To install passwd on CentOS or RHEL, open a terminal and run the following command:

sudo yum install passwd

Common passwd commands

Here are some common passwd commands along with their descriptions:

1. passwd

The passwd command without any options is used to change the password of the current user. It prompts the user to enter their current password and then enter a new password.

2. passwd [username]

The passwd command followed by a username is used to change the password of a specific user. It prompts the user to enter the current password of the specified user and then enter a new password.

3. passwd -l [username]

The passwd command with the -l option followed by a username is used to lock the password of a specific user. This means that the user will not be able to log in using their password.

4. passwd -u [username]

The passwd command with the -u option followed by a username is used to unlock the password of a specific user. This allows the user to log in using their password again.

5. passwd -d [username]

The passwd command with the -d option followed by a username is used to delete the password of a specific user. This means that the user will be able to log in without entering a password.

Similar packages

There are several similar packages to passwd that serve the same purpose of creating or updating passwords for existing users. Some of these packages include:

  • chpasswd: This package is used to update passwords in batch mode by reading a file containing username and password pairs.
  • usermod: This package is used to modify user account settings, including the password.
  • chage: This package is used to change the password expiry information for a user.

Example scripts

Here are three example scripts that demonstrate the usage of the passwd package in automation:

1. Script to change password for multiple users

#!/bin/bash

users=("user1" "user2" "user3")

for user in "${users[@]}"
do
echo "Changing password for $user"
passwd $user
done

This script prompts the user to enter a new password for each user specified in the array.

2. Script to lock user accounts

#!/bin/bash

users=("user1" "user2" "user3")

for user in "${users[@]}"
do
echo "Locking account for $user"
passwd -l $user
done

This script locks the password for each user specified in the array, preventing them from logging in using their password.

3. Script to unlock user accounts

#!/bin/bash

users=("user1" "user2" "user3")

for user in "${users[@]}"
do
echo "Unlocking account for $user"
passwd -u $user
done

This script unlocks the password for each user specified in the array, allowing them to log in using their password again.

List of passwd functions and constants

Function/Constant Description
passwd Change the password of the current user
passwd [username] Change the password of a specific user
passwd -l [username] Lock the password of a specific user
passwd -u [username] Unlock the password of a specific user
passwd -d [username] Delete the password of a specific user

Conclusion

The passwd package is an essential tool in Linux that allows users to create or update passwords for existing users. It is used in various scenarios, such as when a user forgets their password or when a system administrator needs to update passwords for multiple users. The passwd package is written in C and is typically included as part of the core utilities in a Linux distribution. It provides a set of commands that can be used to change, lock, unlock, or delete passwords for users. The passwd package is widely used by system administrators and regular users alike to ensure the security of their accounts and protect against unauthorized access.



This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.

This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.

Leave a reply

Your email address will not be published. Required fields are marked