Skip to content

Instantly share code, notes, and snippets.

@danielef
Created December 3, 2018 16:52
Show Gist options
  • Save danielef/e3390da0c05de931ead7cef06511169d to your computer and use it in GitHub Desktop.
Save danielef/e3390da0c05de931ead7cef06511169d to your computer and use it in GitHub Desktop.
Initializes users and its public keys in localhost (Centos 7.5)
#!/bin/bash
# Initial config for users access
function validate_root {
if [ "$USER" -ne "root" ]; then
printf "WARN: this script is for root user\n"
fi
}
function add_user {
printf "\t Adding user: [$1]\n" && \
useradd $1 && \
usermod -aG wheel $1 && \
usermod -aG docker $1 && \
printf "\t Pushing authorized_keys ...\n" && \
mkdir /home/$1/.ssh && \
touch /home/$1/.ssh/authorized_keys && \
cat $1.pub >> /home/$1/.ssh/authorized_keys && \
chown -R $1 /home/$1/.ssh && \
chmod 700 /home/$1/.ssh && \
chmod 600 /home/$1/.ssh/authorized_keys && \
printf "\t OK for: [$1]\n"
}
function scan_for_pubkeys {
printf "Scanning this directory for *.pub files ...\n"
for i in $( ls *.pub ); do
suffix_idx=$(( ${#i} - 4 ))
user=${i:0:$suffix_idx}
add_user "$user"
done
}
function disable_root_login {
printf "Disabling SSH Root Login\n" && \
printf "\n#Added by initial-config.sh\nPermitRootLogin no\n" >> /etc/ssh/sshd_config && \
printf "Restarting SSH Daemon\n" && \
systemctl restart sshd
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment