Personal tools
You are here: Home Linux I/O Simple bash script to create a user with SSH key
 

Simple bash script to create a user with SSH key

A bash script that will create a user with 'wheel' group access (to provide sudo access).

Load the script into the environment by cut-n-pasting this into the shell

function createsshuser()
{
  USER="$1"
  shift 
  SSH_PUBLIC_KEY="$*"
  
  PASSWORD=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
 
  # create a user with a random password
  adduser ${USER}
  echo ${USER}:${PASSWORD} | chpasswd

  # add the user to the wheel group so they can sudo
  usermod -a -G wheel ${USER}

  # add the ssh public key
  su - ${USER} -c "umask 022 ; mkdir .ssh ; echo $SSH_PUBLIC_KEY >> .ssh/authorised_keys"
}

 

Create users by typing

# createsshuser bob ssh-rsa AAAA...B3FBdxaQ== bob@example.com
Document Actions