Skip to content

Instantly share code, notes, and snippets.

@JonathanWillitts
Created July 5, 2021 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonathanWillitts/f799c690bc9b7ec09367034e75de0ae9 to your computer and use it in GitHub Desktop.
Save JonathanWillitts/f799c690bc9b7ec09367034e75de0ae9 to your computer and use it in GitHub Desktop.
Creates a new user and configures for SSH tunnel only DB access
#!/bin/bash
################################################################################
# Creates a new user and configures for SSH tunnel only DB access.
#
# Usage: create_ssh_tunnel_user.sh <username_to_create>
#
################################################################################
set -e # on error, exit early
if [[ -z $1 ]]
then
echo "No username supplied! Exiting ..."
exit 1
fi
new_user=$1
echo "- creating new user: ${new_user} ..."
adduser "${new_user}"
echo "- adding '${new_user}' to group: db-ssh-tunnel-only ..."
adduser "${new_user}" db-ssh-tunnel-only
ssh_dir=/home/${new_user}/.ssh
echo "- creating and configuring .ssh dir: ${ssh_dir} ..."
mkdir --mode=700 --verbose "${ssh_dir}"
chown ${new_user}:${new_user} "${ssh_dir}"
authorized_keys_file=$ssh_dir/authorized_keys
echo -e "- creating and configuring authorized_keys file: ${authorized_keys_file} ..."
touch "${authorized_keys_file}"
chown ${new_user}:${new_user} "${authorized_keys_file}"
chmod 600 "${authorized_keys_file}"
echo "- verifying..."
echo -n " User: " && getent passwd "${new_user}"
echo -n " Groups: " && groups "${new_user}"
echo -n " .ssh dir: " && ls -l --directory "${ssh_dir}"
echo -n " keys file: " && ls -l "${authorized_keys_file}"
echo "- finished creating new user: ${new_user}."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment