On an Ubuntu 14.04.2 LTS the following command:
adduser fu
and then entering a password bar
and just pressing Enter on all other questions, creates a user that allows me to log on and has the following characteristics in /etc/passwd
:
fu:x:1002:1002:,,,:/home/fu:/bin/bash
However, doing a:
useradd fu --password bar --groups adm,cdrom,sudo,dip,plugdev,lpadmin --shell /bin/bash --create-home
creates in /etc/passwd
fu:x:1002:1002::/home/fu:/bin/bash
and does not allow me to log on and adding --user-group
does not help neither...
As I need useradd
(for a docker script) how do I proceed?
P.S. I've had a look at /usr/sbin/adduser
already, but as it's perl
it's not obvious to me which parameters to add to adduser
to have the same functionality as useradd
This might not be working because your password
bar
is not encrypted. The man page foruseradd
states:This question on Stack Overflow has a few answers which might help you with your script, as the question asks about adding a user along with a password.
As the accepted answer was not updated on that question based on user comments, I will reproduce the solution here:
That command will change the password for a user
fu
tobar
. The user needs to have been created using useradd, of course.Another solution would be to use
chpasswd
, as the second highest voted answer suggests:I have tested both of these solutions, and they both work.
Edited to remove
adduser
info and indicate which suggestions from linked question actually work.Problem #1:
The commas in the user entry in
/etc/passwd
are just field separators for user's informations such as "Full Name", "Room Number", "Work Phone" and "Home Phone", so I guess that omitting them is the same as having them with blank fields;Problem #2:
From the Trusty
man
page foruseradd
:This means that the password expected by
useradd
is an already encrypted password (judging from my test throughSHA-512
): in fact the string passed as an argument to the-p
option is written as plain text in/etc/shadow
and passwords read from here upon login are expected to be encrypted.So the most straightforward solution is to set the password using
passwd
instead:If you can't use
passwd
, this python command in a subshell as an argument to the-p
option should do:Command breakdown:
python -c 'import crypt; print crypt.crypt("<string>", "$<encryption_type>$<encryption_salt>")'
*<string> = string to encrypt; <encryption_type> = encryption type; <encryption_salt> = encryption salt
Crypts a
<string>
using<encryption type>
and<encryption_salt>
using thecrypt()
library callIn this case:
<string>
=bar
, the password to encrypt<encryption_type>
=6
,SHA-512
<encryption_salt>
=< /dev/urandom tr -dc 'a-zA-Z0-9' | head -c 32
, random 32 character string