How can I add a user and set the password for them at the same time?
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?
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
.