-
-
Save ITler/9675f0640729da30d7564879636523b2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#! /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
Requirements
Execution
Call script like: