Skip to content

Instantly share code, notes, and snippets.

@jondb
Created June 3, 2016 21:58
Show Gist options
  • Save jondb/50533c70a191b4859bf54574991767e3 to your computer and use it in GitHub Desktop.
Save jondb/50533c70a191b4859bf54574991767e3 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
USAGE="$0 <username> <ssh_pubkey> <group,groups>"
if [ -z ${1+x} ]; then echo $USAGE; exit 1; fi
if [ -z ${2+x} ]; then echo $USAGE; exit 1; fi
USERNAME=$1
USERPUBKEY=$2
GROUPS=$3
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
# Create the user
/usr/sbin/useradd -m $USERNAME -s /bin/bash
passwd -d $USERNAME
chage -d 0 $USERNAME
cd /home/$USERNAME
# make the .ssh dir
mkdir .ssh
# Put the public key into the special file in the .ssh dir
echo $USERPUBKEY > .ssh/authorized_keys
# Set permissions
chmod 700 .ssh
chmod 600 .ssh/authorized_keys
chown -R ${USERNAME}:${USERNAME} .ssh
#
# Optionally set user as root (admin)
/usr/sbin/usermod -a -G $3 $USERNAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment