8

Is there any option to add a user in single line. it includes the password. Dont prompt anything.

OS=RHEL 5.5

Albin
  • 89
  • 1
  • 1
  • 3

5 Answers5

19

You can use useradd to set everything including the password. The password must be encrypted already, but you can use openssl to make the md5 password if you want to just specify the plain text password:

useradd -u 12345 -g users -d /home/username -s /bin/bash -p $(echo mypasswd | openssl passwd -1 -stdin) username

You may want to exclude this from your history, though, if you are using a plaintext password in the command. You can prepend a space before the command to exclude it from history. If you are running this command on a lot of machines, you may just want to generate the password once and use it in the command directly:

useradd -u 12345 -g users -d /home/username -s /bin/bash -p '$1$NNfXfoym$Eos.OG6sFMGE8U6ImwBqT1' username
Cakemox
  • 25,209
  • 6
  • 44
  • 67
  • 1
    I don't think the `-u` is needed, in this case, unless you want to specifically choose a certain UID. – Alex W Oct 09 '17 at 14:43
4

You should be able to use something like this:

adduser --uid 3434 --password my_password my_login
WhiteFang34
  • 141
  • 2
3

It looks that useradd will encrypt the password with crypt. If you'd rather use other method (MD5, SHA256, SHA512), you can create the user with useradd and set the password with chpasswd, You can run:

useradd <options> && echo username:password | chpasswd --crypt-method=SHA512

Your system-wide default password encryption method is set in /etc/login.defs in variable ENCRYPT_METHOD. && causes chpasswd to run only if user creation with useradd was successful (exit code of 0)

Paweł Brodacki
  • 6,511
  • 20
  • 23
3

A late arrival to the game - RHEL 7.1 - the following thing works. Creates a SUDO user, does not encrypt password, and immediately logs in as the new user:

U=youzerneim; P="pswrd"; adduser $U; echo $P | passwd $U --stdin; usermod -aG wheel $U; su - $U
Lefty G Balogh
  • 413
  • 4
  • 8
1

useradd -u <uid> -G <group> -d /home/<user home dir> -p password -m

-u for UID
-d for home directory
-m for creating the home directory as mentioned in -d option

Well there are many more options depending on the flavour of linux.
You can always do ... man useradd OR man adduser

Mayank
  • 199
  • 1
  • 8