Skip to content

Instantly share code, notes, and snippets.

@peterkappus
Created November 30, 2017 13:48
Show Gist options
  • Save peterkappus/6de7bb4b9298d9d91224ecf6a6a7a7af to your computer and use it in GitHub Desktop.
Save peterkappus/6de7bb4b9298d9d91224ecf6a6a7a7af to your computer and use it in GitHub Desktop.
Easily create a sudoer, SSH-keyed new user on a linux machine
#!/bin/bash -e
#
# Creates user and adds public key
# Must be run as root
#
# Requires:
# 1) Full name
# 2) Username
# 3) Public key
if (( "$#" != 3 ))
then
echo "Creates a user with sudo rights and loads their public key."
echo "Usage: $(basename $0) FULL_NAME USERNAME PUBLIC_KEY"
exit 1
fi
fullname=$1
username=$2
key=$3
useradd -c "$fullname" $username
mkdir -m 700 /home/$username/.ssh
echo "$key" > /home/$username/.ssh/authorized_keys
chmod 600 /home/$username/.ssh/authorized_keys
chown -R $username:$username /home/$username/.ssh
echo -e "$username\t\tALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
echo 'User created'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment