Created
July 5, 2021 10:53
-
-
Save JonathanWillitts/f799c690bc9b7ec09367034e75de0ae9 to your computer and use it in GitHub Desktop.
Creates a new user and configures for SSH tunnel only DB access
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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