Skip to content

Instantly share code, notes, and snippets.

@zioalex
Forked from ITler/create_user.sh
Created February 11, 2020 10:19
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 zioalex/b56882fa61335136b521a1ff9d96745e to your computer and use it in GitHub Desktop.
Save zioalex/b56882fa61335136b521a1ff9d96745e to your computer and use it in GitHub Desktop.
#! /usr/bin/env bash
# normally first char of prename + surname
users="${@:-jsmith}"
# normally users
group="users"
# normally adm and/or sudo
groups="adm,sudo"
# optional file containing all known SSH pub keys in authorized_keys format
all_auth_keys_file=$(dirname $0)/authkeys
for user in ${users}; do
auth_keys_file=/home/${user}/.ssh/authorized_keys
echo "Creating access for user: ${user}"
useradd -s /bin/bash -m ${user} -g ${group} -G ${groups} -c "manually created on $(date +%Y%m%d-%H%M%S)"
mkdir -m 0700 /home/${user}/.ssh
[ -f "${auth_keys_file}" ] || install -b -m 0600 /dev/null ${auth_keys_file}
if [[ -r ${all_auth_keys_file} && $(grep ${user} ${all_auth_keys_file}) ]]; then
echo "Found user's SSH key in authorized_keys database ${all_auth_keys_file} file"
cat ${all_auth_keys_file} >>${auth_keys_file}
else
echo "SSH key for user ${user} not found in authorized_keys database ${all_auth_keys_file}, so add key manually"
echo "Now paste user's SSH pub key here, make sure to end with new line and press CTRL+D to exit"
echo ${auth_keys_file}
cat >>${auth_keys_file}
fi
sed -i "/.*${user}.*/!d" ${auth_keys_file}
echo "SSH pub key stored in ${auth_keys_file}"
chown -R ${user}:${group} $(dirname ${auth_keys_file})
echo -e "... User '${user}' done\n"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment