Skip to content

Instantly share code, notes, and snippets.

@fahrradflucht
Created March 23, 2024 14:03
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 fahrradflucht/bf9adb78c5b699c8cb99e470da096bf0 to your computer and use it in GitHub Desktop.
Save fahrradflucht/bf9adb78c5b699c8cb99e470da096bf0 to your computer and use it in GitHub Desktop.
Download ssh keys from GitHub and add them as authorized keys.
#!/bin/bash
# Check if a GitHub username has been provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <github-username>"
exit 1
fi
# Make sure we have curl installed
if ! command -v curl &> /dev/null
then
echo "curl could not be found, please install curl."
exit
fi
GITHUB_USER="$1"
# Define the directory where the SSH authorized_keys file is located
AUTHORIZED_KEYS_DIR="$HOME/.ssh"
AUTHORIZED_KEYS_FILE="$AUTHORIZED_KEYS_DIR/authorized_keys"
# Create the .ssh directory if it doesn't exist
mkdir -p "$AUTHORIZED_KEYS_DIR"
# Ensure the authorized_keys file exists
touch "$AUTHORIZED_KEYS_FILE"
# Fetch the user's public keys from GitHub and add them to the authorized_keys
echo "Fetching public keys for GitHub user: $GITHUB_USER"
curl -s "https://github.com/$GITHUB_USER.keys" >> "$AUTHORIZED_KEYS_FILE"
# Ensure the permissions are correct
chmod 700 "$AUTHORIZED_KEYS_DIR"
chmod 600 "$AUTHORIZED_KEYS_FILE"
echo "Public keys for $GITHUB_USER have been added to $AUTHORIZED_KEYS_FILE"
@fahrradflucht
Copy link
Author

Usage: curl -sL https://gist.githubusercontent.com/fahrradflucht/bf9adb78c5b699c8cb99e470da096bf0/raw/a92f6d56f9400f708b5248397d76dac45774b561/add_github_user_keys.sh | bash -s -- GITHUB_USERNAME_HERE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment