Reputation: 325
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
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
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
Reputation: 2689
Just use the -p option to useradd
sudo useradd -c USERNAME -p new_password new_user
Upvotes: 1