The useradd
command in Linux is used to create new user accounts. One of the important options available for this command is -p
or --password PASSWORD
, which allows setting passwords for the newly created user accounts.
Here are several examples of using useradd
with the -p
option:
Example 1: Create a new user john
and set the password immediately.
$ sudo useradd -p 'password123' john
This command creates a user account named john
with the password set to password123
.
To verify if the user was successfully created, you can use:
$ grep john /etc/passwd
If john
is listed in the output, the user account was created.
Example 2: Create a user alice
and set her password using an encrypted password string.
$ sudo useradd -p '$6$3.4Fajk$Gy5dZf4Bkp1l1puM4vWb2LWf1xCM72BY1.sE6D0B/7OIGTb9NArS.eUvw0B2m3.u6Onz5sBwFtD/ghJvDAg9u/' alice
In this command, the password is set using an encrypted password string, ensuring stronger security.
Example 3: Create a user smith
and set the password using an MD5 hash.
$ sudo useradd -p '$1$av7iwD7b$Asnkj234.DXG0RlsN8h7Y1' smith
This command demonstrates setting a password using an MD5 hash for the user smith
.
Example 4: Set password for an existing user jane
using the passwd
command after creation.
$ sudo useradd jane $ sudo passwd jane
This sequence first creates the user jane
and then prompts to set a password interactively.
Example 5: Create a user guest
and set an empty password.
$ sudo useradd -p '' guest
Setting an empty password can be useful in some scenarios but is generally discouraged for security reasons.
These examples illustrate different ways to use the useradd
command with the -p
option to set passwords for user accounts in Linux. Each method offers flexibility depending on the security requirements and administrative needs of the system.
Discussion about this post