
In this article, we will delve into the process of changing a user’s password using the usermod
command in Ubuntu. This command is a powerful tool that allows you to modify a user’s settings, including their password. However, it’s important to note that using usermod
to change passwords requires the password to be in an encrypted format.
To change a user’s password using the usermod
command in Ubuntu, you can use the -p
option followed by the encrypted password. You can generate an encrypted password using commands like openssl passwd
, crypt()
, or mkpasswd
. Alternatively, you can use the passwd
command for a simpler and safer way to change passwords.
Understanding the usermod Command
The usermod
command in Ubuntu is a user account modification tool. It’s used to change various user account settings, including user’s home directory, shell, password, and others. The syntax for the usermod
command is as follows:
usermod [options] LOGIN
In this article, we will focus on the -p
option, which allows you to set the user’s password. However, this option requires the password to be in an encrypted format.
Changing User Password with usermod
Using openssl passwd
Firstly, you can use the openssl passwd
command to generate an encrypted password. Here’s how:
- Open your terminal.
- Type
openssl passwd
and hit Enter. - You will be prompted to enter the password you want to encrypt. After entering it, the terminal will output the encrypted version of your password.
Example:
$ openssl passwd
Password:
Verifying - Password:
$apr1$ZzZzZzZz$abcdefgh1234567890abcdef.
Now, you can use this encrypted password with the usermod
command:
usermod -p '$apr1$ZzZzZzZz$abcdefgh1234567890abcdef.' username
Replace username
with the actual username for which you want to change the password.
Using crypt() hashed password
Another method is to use a crypt()
hashed password. You can generate a hashed password using a script or command like Perl:
SALT="Q9"
PLAINTEXT="secret_password"
HASH=$(perl -e "print crypt('${PLAINTEXT}','${SALT}')")
In this script, SALT
is a two-character string that will be used to generate the hash, and PLAINTEXT
is the password you want to hash. The crypt()
function will return the hashed password.
Then, use the usermod
command with the hashed password:
usermod -p ${HASH} username
Using mkpasswd and usermod
If mkpasswd
is not available on your system, you can install it by installing the whois
package:
apt install whois
Then, generate the encrypted password using the mkpasswd
command:
mkpasswd -m sha-512 your-secret-password
Finally, use the usermod
command with the encrypted password:
usermod -p 'encrypted-password' username
Using passwd Command
While the usermod
command can change a user’s password, it’s generally safer and more straightforward to use the passwd
command. This command changes the password interactively, prompting you to enter and confirm the new password:
sudo passwd username
Conclusion
In this article, we explored various ways to change a user’s password using the usermod
command in Ubuntu. While this command is powerful and flexible, it’s important to remember that it requires passwords to be in an encrypted format. For most users, the passwd
command offers a simpler and safer way to change passwords.
No, the usermod
command requires the password to be in an encrypted format. You can use methods like openssl passwd
, crypt()
, or mkpasswd
to generate the encrypted password before using the usermod
command.
To generate an encrypted password using openssl passwd
, open your terminal and type openssl passwd
. You will be prompted to enter the password you want to encrypt. After entering it, the terminal will output the encrypted version of your password.
To generate a hashed password using the crypt()
function, you can use a script or command like Perl. Set a SALT
variable as a two-character string and a PLAINTEXT
variable as the password you want to hash. Then, use the crypt()
function with these variables to generate the hashed password.
If the mkpasswd
command is available on your system, you can generate an encrypted password by typing mkpasswd -m sha-512 your-secret-password
in the terminal. Replace your-secret-password
with the actual password you want to encrypt.
Yes, the passwd
command provides a simpler and safer way to change a user’s password interactively. You can use the command sudo passwd username
, where username
is the actual username for which you want to change the password. The command will prompt you to enter and confirm the new password.