How to automatically add user account AND password with a Bash script?

The code below worked in Ubuntu 14.04. Try before you use it in other versions/linux variants.

# quietly add a user without password
adduser --quiet --disabled-password --shell /bin/bash --home /home/newuser --gecos "User" newuser

# set password
echo "newuser:newpassword" | chpasswd

You could also use chpasswd:

echo username:new_password | chpasswd

so, you change password for user username to new_password.


You can run the passwd command and send it piped input. So, do something like:

echo thePassword | passwd theUsername --stdin

I was asking myself the same thing, and didn't want to rely on a Python script.

This is the line to add a user with a defined password in one bash line:

useradd -p $(openssl passwd -crypt $PASS) $USER

Tags:

Linux

Bash

Passwd