Skip to content

Instantly share code, notes, and snippets.

@koiralakiran1
Last active March 26, 2024 13:08
Show Gist options
  • Save koiralakiran1/fd33f2b6162f827a0a90a7c45011df53 to your computer and use it in GitHub Desktop.
Save koiralakiran1/fd33f2b6162f827a0a90a7c45011df53 to your computer and use it in GitHub Desktop.
Helper script to setup github ssh as mentioned in github documentation
#!/usr/bin/env bash
# Script for "Connecting to github with ssh"
# Ref: https://docs.github.com/en/authentication/connecting-to-github-with-ssh
# Author: Kiran Koirala
# Github: koiralakiran1
#
# Asks for <filename> and <email>.
# Doesn't check for existing ssh keys.
# Uses "ed25519" key type (hardcoded).
# Key saved to ~/.ssh/<filename>.
# <filename> is "id_ed25519" by default.
#
# To run directly from gist raw url
# Link to gist raw file might change
# $ sh <( curl -s gist_raw_link_to___setup_github_ssh_keys.sh )
set -e
DEFAULT_KEY_TYPE="ed25519"
DEFAULT_FILENAME="id_$DEFAULT_KEY_TYPE"
# Ask for file name (~/.ssh/<filename>)
read -p "Enter filename ~/.ssh/(default = $DEFAULT_FILENAME): " filename
# Ask for email
read -p "Enter email: " email
if [[ -z $filename ]]; then
filename=$DEFAULT_FILENAME
fi
if [[ -z $email ]]; then
echo "Email is required."
exit 1
fi
# Step: Generate new key and add to ssh-sgent
# Ref: https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
ssh-keygen -t $DEFAULT_KEY_TYPE -C $email -f ~/.ssh/$filename
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/$filename
# Step: Add ssh key to github account
# Ref: https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account
echo -e "Here's your public ssh key. Copy these and add it to github: \n"
cat ~/.ssh/$filename.pub
echo -e "Press any key when done: \n"
read
# Step: Testing your ssh connection
# Ref: https://docs.github.com/en/authentication/connecting-to-github-with-ssh/testing-your-ssh-connection
echo "Connecting to github with new ssh key "
ssh -T git@github.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment