Alex Wiley
Alex Wiley

Reputation: 325

Linux Shell: useradd options

Could anybody give me a little help with the following line:

sudo useradd -c USERNAME new_user | passwd new_user

What I am trying to do is add a new user to the system and set up their password all in one line without having to do two commands. When I enter this line it tells me that the user new_user does not exist. Could anybody help me out here or explain to me why this is not working?

Also if there is a better way of doing this then please feel free to let me know.

Thanks you, Alex

Upvotes: 0

Views: 2853

Answers (4)

Savana Rohit
Savana Rohit

Reputation: 40

You can also try this one

adduser username;passwd username;

Upvotes: 0

barti_ddu
barti_ddu

Reputation: 10299

For interactive password entry the one-liner could be:

sudo $SHELL -c "useradd UNAME && passwd UNAME"

For pre-defined password you could do something like:

useradd -p $(mkpasswd --method=sha-512 --rounds=10 SECRET) UNAME

However, I'd suggest against that :)

To learn what encryption method is used on your system you can peek at /etc/shadow: if the hash begins with $6$- it is SHA-512 (as in the example).

Upvotes: 2

wiredolphin
wiredolphin

Reputation: 1641

You can use the -p option of the useradd command, but you need to provide the encrypted password, for example making use of openssl library:

sudo useradd -c USERNAME new_user -p $(openssl passwd -crypt new_user_password)

You can also use other encryption methods but you cannot use a clear password since useradd manual states that -p parameter takes "The encrypted password as returned by crypt(3)".

Upvotes: 1

Neil Masson
Neil Masson

Reputation: 2689

Just use the -p option to useradd

sudo useradd -c USERNAME -p new_password new_user 

Upvotes: 1

Related Questions