carlessanagustin
5/4/2018 - 2:21 PM

SHELL: Create user with key (unstable)

SHELL: Create user with key (unstable)

#!/bin/bash
# Stop on any errors
set -e

NEWUSER=$1
USERPUBKEY=$2

if [ -z "$NEWUSER" ]; then
        echo "Username required"
        exit 1;
fi

if [ -z "$USERPUBKEY" ]; then
        echo "Public key required - Enclose argument in quotes!"
        exit 1;
fi

#1.) Create a new user.
useradd -d /home/$NEWUSER -s /bin/bash -m $NEWUSER

#2.) Create a local public/private key pair as the user.
su - -c "ssh-keygen -q -t rsa -f ~/.ssh/id_rsa -N ''" $NEWUSER

#3.) Create an authorized_keys file with their external public key,
su - -c "echo $USERPUBKEY > .ssh/authorized_keys" $NEWUSER

#4.) Adjust the authorized_keys permissions
su - -c "chmod 600 .ssh/authorized_keys" $NEWUSER

#5.) More steps?