How do I add a user and set their password at the same time?
useradd user1 -p user@123
user1 is their username and
user@123 is their password.
useradd, use the
adduser command, which automatically runs
passwd when you call it, unless you use the
$ 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
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
-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.