How to change a user's password from the command line?
Jun 21, 2025 am 12:37 AMThe most common way to change your password from the command line is to use the passwd command. 1. Ordinary users only need to enter passwd and verify the current password before setting a new password; 2. Administrators can modify other user passwords through sudo passwd username, without the original password; 3. If you need to force the user to reset the password next time you log in, you can use sudo chage -d 0 username or sudo passwd -e username; 4. Setting a strong password should avoid common words, combine case, numbers and symbols, and give priority to length rather than complexity; 5. Clear text passwords should not be hard-coded in the script, and it is recommended to use chpasswd or automation tools such as Ansible; 6. Pay attention to permission restrictions, system locking mechanism, read-only password library of container environments and SSH keys that do not affect password login. Master these key points to effectively manage passwords.
Changing a user's password from the command line is actually pretty straightforward, especially on Unix-like systems such as Linux or macOS. You don't need a fancy GUI—just a terminal and the right command.
How to use the passwd
command
The most common way to change a password is with the passwd
command. If you're changing your own password, just type:
passwd
You'll be prompted to enter your current password, then choose a new one. That's it for regular users changing their own passwords.
If you're a superuser or have sudo access and want to change another user's password, you'd run:
sudo passwd username
Replace username
with the actual name of the account you want to modify. You won't be asked for the old password in this case—so be careful who you give sudo rights to.
What about forcing a password reset?
Sometimes you might want a user to reset their password the next time they log in. This is useful if you've set a temporary password or suspect something's off.
To do that, use the chage
command:
sudo chage -d 0 username
This marks the password as expired, so when the user logs in next, they'll be forced to create a new one.
Another option is using passwd
itself:
sudo passwd -e username
Both commands essentially do similar things, but chage
gives you more control over password expiration policies.
Tips for setting strong passwords (without being annoying)
When setting or updating a password, keep these points in mind:
- Avoid dictionary words or simple patterns like
123456
orpassword
- Mix uppercase, lowercase, numbers, and symbols
- Consider length over complexity—longer passwords are often stronger
- Don't reuse passwords across accounts
Most systems already enforce some basic rules via PAM modules or password policies, but it's still good practice to encourage smart habits.
Also, remember: if you're scripting password changes, never hardcode plain text passwords into files. Use secure methods like chpasswd
with input redirection or tools designed for automation like Ansible.
A few gotchas to watch out for
- You can't change another user's password without sudo privileges
- Some systems lock accounts after too many failed attempts—be careful with scripts
- On certain environments (like containers), password databases might be read-only unless you turn them
- If a user has SSH keys set up, changing the password doesn't affect key-based login
And if you ever get stuck or see an error like “Authentication token manipulation error,” double-check filesystem mounts and permissions—it might not even be about your command.
Basically that's it. Most of the time, passwd
is all you need. Just know when and how to use it properly, especially when managing other users.
The above is the detailed content of How to change a user's password from the command line?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

When using Win11 system, sometimes you will encounter a prompt that requires you to enter the administrator username and password. This article will discuss how to deal with this situation. Method 1: 1. Click [Windows Logo], then press [Shift+Restart] to enter safe mode; or enter safe mode this way: click the Start menu and select Settings. Select "Update and Security"; select "Restart Now" in "Recovery"; after restarting and entering the options, select - Troubleshoot - Advanced Options - Startup Settings -&mdash

Passkey is a more advanced and secure way to log in and can be used with a Microsoft account. This article will guide you how to use Passkey in your Microsoft account, introduce its advantages and the differences with passwords. Before we get into the details, we emphasize that if you frequently forget your passwords or need to log in quickly, Passkey will generally be better for you. What is Passkey? How are they different from passwords? Passwords are an advanced and secure authentication method that can replace traditional password logins. Each application has a unique key, ensuring security and preventing threats like phishing. Users can confirm their identity via email and password, or use facial recognition, fingerprint or security key

Wireless networks have become an indispensable part of people's lives in today's digital world. Protecting the security of personal wireless networks is particularly important, however. Setting a strong password is key to ensuring that your WiFi network cannot be hacked by others. To ensure your network security, this article will introduce in detail how to use your mobile phone to change the router WiFi password. 1. Open the router management page - Open the router management page in the mobile browser and enter the router's default IP address. 2. Enter the administrator username and password - To gain access, enter the correct administrator username and password in the login page. 3. Navigate to the wireless settings page - find and click to enter the wireless settings page, in the router management page. 4. Find the current Wi

This article will explore how to solve the problem of wrong password, especially the need to be careful when dealing with BitLocker warnings. This warning is triggered when an incorrect password is entered multiple times in BitLocker to unlock the drive. Usually, this warning occurs because the system has a policy that limits incorrect login attempts (usually three login attempts are allowed). In this case, the user will receive an appropriate warning message. The complete warning message is as follows: The password entered is incorrect. Please note that continuously entering incorrect passwords will cause the account to be locked. This is to protect the security of your data. If you need to unlock your account, you will need to use a BitLocker recovery key. The password is incorrect, beware the BitLocker warning you receive when you log in to your computer

After upgrading the win101909 system, some friends may want to cancel their computer lock screen password. If you don’t know what to do, I think we can find the account options in the computer’s settings window. Let’s take a look at the specific methods. How to cancel the lock screen password in win101909: 1. First press Win+I to open the settings interface. 2. Then find the account--login option, and then set it as shown. How to cancel power-on password login in win101909: 1. Execute regedit during running to open the registry and navigate to the following path: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\WindowsNT\Cu

With the popularity of Douyin, more and more users are beginning to use this platform to enjoy the fun of short videos. In order to protect the security of user accounts, Douyin provides a variety of login methods, among which password login is a common and important method. So, how to set a password to log in to Douyin? 1. How to set a password to log in to Douyin? 1. Open Douyin APP, click the "Me" button in the lower right corner to enter the personal homepage. 2. Under the personal homepage, find the "Settings" option and click to enter. 3. In the settings page, find the "Account and Security" option and click to enter. 4. On the Account and Security page, find the "Login Password" option and click to enter. 5. If a login password has not been set, the system will prompt you to set a login password. Enter the password you want to set

This article details the steps to upgrade Ubuntu 20.04 to 22.04. For users using Ubuntu 20.04, they have missed the new features and advantages brought by version 22.04. In order to get a better experience and security, it is recommended to upgrade to a newer Ubuntu version in time. Ubuntu22.04 is codenamed "Jamie Jellyfish", let's explore how to get the latest LTS version! How to upgrade Ubuntu 20.04 to 22.04 via the command line Mastering the command line will give you an advantage. While it is possible to update Ubuntu via the GUI, our focus will be via the command line. First, let’s check the currently running version of Ubuntu using the following command: $

In the Windows 10 system, the password policy is a set of security rules to ensure that the passwords set by users meet certain strength and complexity requirements. If the system prompts that your password does not meet the password policy requirements, it usually means that your password does not meet the requirements set by Microsoft. standards for complexity, length, or character types, so how can this be avoided? Users can directly find the password policy under the local computer policy to perform operations. Let’s take a look below. Solutions that do not comply with password policy specifications: Change the password length: According to the password policy requirements, we can try to increase the length of the password, such as changing the original 6-digit password to 8-digit or longer. Add special characters: Password policies often require special characters such as @, #, $, etc. I
