Linux User Management – Add User Without Knowing Encrypted Password

linuxslesunixuser-accountsuser-management

I use the following command to create a user in a linux machine:

useradd -d /home/dummy -g idiots -m -p 12345689 dummy

The user is created and the home directory as well.
The problem is that I can not log-in to the system using this account since the -p expects the encrypted password returned by crypto.

Question:I want to create a user via a bash script and I don't know the encrypted password by crypto. How can I do it so that I am able to create this user automatically via a script and get arround the problem with the password?

Best Answer

You can use openssl to generate pre encrypted password strings to use with the -p option to useradd

echo "P4sSw0rD" | openssl passwd -1 -stdin

$1$Jxmpx1Da$Y8MzBctIyDW8/7pFPbNWD1

The -1 says to generate a MD5 password hash. The salt is automatically generated.

You can then use

useradd -d /home/dummy -g idiots -m -p $(echo "P4sSw0rD" | openssl passwd -1 -stdin) dummy

to add the user. To do this interactively hiding the password

useradd -d /home/dummy -g idiots -m -p $(read -sp Password: pw ; echo $pw | openssl passwd -1 -stdin) dummy