profile picture

Automatically add user to Ubuntu Linux and set password

Published on 23 December 2008 - development work

This content is old and may be outdated.

I was wondering how to create a user on my system without requiring a prompt or CLI access. The following script is the result:

#!/bin/bash
UADD=/usr/sbin/useradd
OPENSSL=/usr/bin/openssl
SHELL=/bin/bash

# Generate 12 characters long password and print it.
PASS=`$OPENSSL rand -base64 12`
echo $PASS

# Make the password usable for the useradd utility
PASS=`mkpasswd $PASS`

# Create the user
$UADD -s $SHELL -m $1 -p $PASS