5

How do I add a user and set their password at the same time?

I tried:

useradd user1 -p user@123  

Where user1 is their username and user@123 is their password.

  • 1
    Dont! Note: The -p option is not recommended because the password (or encrypted password) will be visible by users listing the processes. Review man useradd – Elder Geek Dec 11 '17 at 20:31
8

Instead of useradd, use the adduser command, which automatically runs passwd when you call it, unless you use the --disabled-login or --disabled-password flags.

Sample run:

$ sudo adduser pixie
Adding user `pixie' ...
Adding new group `pixie' (1001) ...
Adding new user `pixie' (1001) with group `pixie' ...
Creating home directory `/home/pixie' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:                             # password does not show
Retype new UNIX password:                            
passwd: password updated successfully
Changing the user information for pixie
Enter the new value, or press ENTER for the default
    Full Name []: 
    Room Number []: 
    Work Phone []: 
    Home Phone []: 
    Other []: 
Is the information correct? [Y/n] y

See: What is the difference between adduser and useradd?

2

useradd --help shows that the login name has to be the last parameter. It says also that the password given with -p is the encrypted password of the new account which does not seem to be the case in your example.

It's easier to create the user with useradd user1 and set his password afterwards with sudo passwd user1.

Useful options of useradd are -c comment, -m to create his home directory and -s /bin/bash to define his shell.

Which would give sudo useradd -c'new user xy' -m -s /bin/bash user1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.