Skip to content

Instantly share code, notes, and snippets.

@gotev
Last active April 28, 2021 23:37
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 gotev/f4d93e69a30e5a66ef97c8d5d0b350db to your computer and use it in GitHub Desktop.
Save gotev/f4d93e69a30e5a66ef97c8d5d0b350db to your computer and use it in GitHub Desktop.
macOS GitHub SSH Certificates Helper
#!/bin/bash -e
echo "macOS Helper to configure SSH Key to use with GitHub"
echo "based off of instructions reported at:"
echo "https://docs.github.com/en/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account"
echo
if [[ "$OSTYPE" != "darwin"* ]]; then
echo "This script is designed for macOS only..."
echo "exiting"
exit 1
fi
echo "Checking existing ed25519 SSH key..."
if [ ! -f "$HOME/.ssh/id_ed25519" ]; then
echo "You don't have it, generating it for you..."
echo
EMAIL=$(git config --global user.email)
echo "Detected email from git config: $EMAIL"
echo
echo "IF IT'S NOT CORRECT, PLEASE HIT CONTROL + C TO EXIT THIS SCRIPT"
echo "AND PROPERLY CONFIGURE YOUR GIT EMAIL WITH THIS COMMAND:"
echo "git config --global user.email \"youremail@provider.com\""
echo
echo "When prompted where to save the certificate, press ENTER"
echo "and then press ENTER two more times when asked to put in a password"
echo
ssh-keygen -t ed25519 -C "$EMAIL" || true
else
echo "Good! You have it!"
fi
echo
echo "Starting SSH Agent in the background"
eval "$(ssh-agent -s)"
if [ ! -f "$HOME/.ssh/config" ]; then
echo
echo "Creating new SSH config"
touch "$HOME/.ssh/config"
else
echo "Using existing SSH config"
fi
if grep -q "IdentityFile ~/.ssh/id_ed25519" "$HOME/.ssh/config"; then
echo "SSH Config already correct!"
else
echo "Adding the configuration to SSH Config"
echo "Host *" >> "$HOME/.ssh/config"
echo " AddKeysToAgent yes" >> "$HOME/.ssh/config"
echo " IdentityFile ~/.ssh/id_ed25519" >> "$HOME/.ssh/config"
echo
echo "Adding private key to SSH Agent"
ssh-add "$HOME/.ssh/id_ed25519"
fi
echo "Copying SSH public key in the clipboard"
pbcopy < "$HOME/.ssh/id_ed25519.pub"
echo
echo "Now navigate to https://github.com/settings/keys"
echo "and press on New SSH Key"
echo
echo "1. Assign a custom title. For example: macOS SSH Key"
echo "2. and then press COMMAND + V in the Key field"
echo "3. Hit Add SSH Key"
echo "4. Confirm with your password and then enable SSO if needed with your organization"
@gotev
Copy link
Author

gotev commented Apr 28, 2021

To run this script, copy, paste and run in your macOS terminal:

curl https://gist.githubusercontent.com/gotev/f4d93e69a30e5a66ef97c8d5d0b350db/raw/eac83fbf8f9b6edcca574871f0631e79b6e80be5/configure-ssh-certs.sh | bash

and then follow instructions on screen.

To clean configuration made by this script:

cd $HOME/.ssh
ssh-add -d "$HOME/.ssh/id_ed25519"
rm -rf config id_ed25519 id_ed25519.pub

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