7

useradd has a -p flag, with which you can specify a password ciphertext encrypted with crypt. But later Linuxes don't have crypt.

How can I in a single line, create a new user in RedHat with a pre-defined password?

Update to prevent more drive-by down-voting without context or engagement: My scripting is used inside an automatic setup environment in Amazon Web Services. The script downloads and installs software prerequisites along with the primary line of business application, configures it, adds configuration files for it etc. One of the other things this application needs to do is create an SFTP service to transfer some ledger files backwards and forwards (that service is not under my control, so I can't meet this requirement with public key auth). One of the steps in the automated setup is to create the user that an external system will log on with to do the SFTP-ing, but the useradd command by default in RedHat creates a locked user with no password, then you interactively set a password using passwd. That's not an option in an automated script. The -p flag exists, but it expects ciphertext encrypted with crypt, which itself uses (I think) whatever cryptographic algorithm the shadow file uses, but it seems like that utility hasn't existed in later Linux and Linux-like operating systems for a while. Is the only way around this to encrypt the password on another, older system, and use the ciphertext in the script? It seems like a lot of effort given any passwords in the script are retrieved using KMS decryption so there are no cleartext secrets viewable in the script.

2
  • 1
    You can make a script like this one: stackoverflow.com/questions/3190955/…
    – DukeLion
    Aug 11, 2017 at 7:49
  • That question is someone who wants to know if they can use Python to create users, and they are directed to use useradd - which has the problem I originally have.
    – Elomis
    Aug 13, 2017 at 23:10

3 Answers 3

20

This solution should do the trick:

sudo useradd -p $(openssl passwd -1 password) username

This command creates a user with the username "username" and the password "password".

Alternative:

useradd test
echo "username:password" | chpasswd

Tested on: Ubuntu 16.04 / Debian 9

5
  • Would it be safer to put quotes around $(openssl...) ?
    – TomOnTime
    Aug 15, 2017 at 0:41
  • Thanks very much, didn't realise openssl could replace the builtin crypt.
    – Elomis
    Aug 15, 2017 at 23:01
  • 2
    This also works on RedHat
    – Ian Lim
    Dec 7, 2018 at 7:13
  • How to quote password if it contains special character like &?
    – Edwin Yip
    May 24, 2021 at 6:55
  • better is use -5 or -6 (uses more modern SHA256 or SHA512 instead of MD5) Mar 8, 2023 at 17:52
2

For RedHat (7.3 Maipo for this version), the following not particularly neat solution works:

useradd your-service-user; echo s3cr3tP4ssW0rd! | passwd your-service-user --stdin

This uses the standard input to feed the passwd to add the password.

2

Here's something not related to the question but important. remember to clean your history because your password will be stored there too.

Use the command "history" to clear the last two lines from range -2 to -1

history -d -2--1

a one line example that does it all.

useradd -G wheel youruser; echo yourpassword | passwd youruser --stdin  ; history -d -1

note , you need a recent version (5.1.x) of bash to use negative index. check with : echo "${BASH_VERSION}"

Thank you!

1
  • 1
    or have HISTCONTROL="...,ignorespace,..." set and any command prefixed with a space won't be saved into history, so no need to clean (see man bash for details) Mar 8, 2023 at 17:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .