Created
March 23, 2024 14:03
-
-
Save fahrradflucht/bf9adb78c5b699c8cb99e470da096bf0 to your computer and use it in GitHub Desktop.
Download ssh keys from GitHub and add them as authorized keys.
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
#!/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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
curl -sL https://gist.githubusercontent.com/fahrradflucht/bf9adb78c5b699c8cb99e470da096bf0/raw/a92f6d56f9400f708b5248397d76dac45774b561/add_github_user_keys.sh | bash -s -- GITHUB_USERNAME_HERE