Last active
March 4, 2024 05:25
-
-
Save gene1wood/0ec4bb6055b1011642fd to your computer and use it in GitHub Desktop.
A tool to create a user and populate it's ssh authorized_keys file with their github public keys
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 | |
if [ "$#" -lt "1" ]; then | |
echo "usage" | |
echo "$0 GITHUB_USERNAME UNIX_USERNAME" | |
echo "$0 GITHUB_AND_UNIX_USERNAME" | |
echo "$0 GITHUB_USERNAME -G wheel UNIX_USERNAME" | |
echo "$0 GITHUB_USERNAME --home /opt/foo -G wheel UNIX_USERNAME" | |
exit 1 | |
fi | |
github_username="$1" | |
shift | |
if [ "$*" = "" ]; then | |
unix_username="$github_username" | |
useradd_args="$unix_username" | |
else | |
eval unix_username="\$$#" | |
useradd_args="$*" | |
fi | |
if ! useradd $useradd_args; then | |
echo "Useradd failed" | |
exit 1 | |
fi | |
users_home="`getent passwd "$unix_username" | cut -d: -f6`" | |
if [ ! -d "$users_home/.ssh" ]; then | |
install --owner "$unix_username" --group `id --group "$unix_username"` --mode 700 --verbose --directory "$users_home/.ssh" | |
fi | |
if [ ! -e "$users_home/.ssh/authorized_keys" ]; then | |
install --owner "$unix_username" --group `id --group "$unix_username"` --mode 600 --verbose /dev/null "$users_home/.ssh/authorized_keys" | |
fi | |
if pubkeys="`curl --fail https://github.com/$github_username.keys`"; then | |
echo "$pubkeys" >>"$users_home/.ssh/authorized_keys" | |
echo "User created and public keys loaded" | |
else | |
echo "Failed to fetch keys at https://github.com/$unix_username.keys" | |
echo "User $unix_username created but public keys were not loaded" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment