Linux useradd and password from commandline?

1

Based on the documentation here:

https://linux.die.net/man/8/adduser

I am trying to create a new user with a default home dir in Ubuntu 17.10 and specify the password:

useradd john -m -p mypassword

The user is created just fine, but if I try next to SSH to the machine using the new user with the specified password (mypassword) I get:

$ ssh john@my-host
john@my-host's password: 
Permission denied, please try again.
john@my-host's password: 

So why does it not work with the password I used when creating the user?

Based on below answer useradd requires an encrypted password. From the command line I can do:

$ echo -n testpass | makepasswd --crypt-md5 --clearfrom -
testpass   $1$eaXbTWAK$8flrqGzhmtWPjzRcrxWBf/

But how do I pass that to useradd, to make it possible to do the whole thing on one line?

u123

Posted 2017-11-18T16:19:34.570

Reputation: 359

Answers

3

From the adduser(8) man page:

-p, --password PASSWORD

The encrypted password, as returned by crypt(3).

Specifying the password as plaintext won't work.

Ignacio Vazquez-Abrams

Posted 2017-11-18T16:19:34.570

Reputation: 100 516

0

You can combine multiple command using the $(some-cm) construct. This will cause bash to evaluate the parenthesise first and use the return value in its place:

echo foo $(echo bar)

will evaluate the second command first and then it will evaluate the first command. You can us this to first generate your password and then putting it into the adduser command.

Or you use command chaining:user=john; useradd -m $user && passwd $user. This will still prompt you for the password though.

paradoxon

Posted 2017-11-18T16:19:34.570

Reputation: 596