GitHub: Authenticated Access via SSH

This article describes how to set up authenticated access to a (private or public) GitHub repository from Linux.

Create & Set Up an SSH Key

SSH Key Creation

Create a new SSH key:

# 1. Adjust the email
# 2. Set a strong passphrase when asked
ssh-keygen -t ed25519 -C "you@example.com"

Rename the new key to whatever descriptive name you favor (I’m simply using github):

mv ~/.ssh/id_ed25519 ~/.ssh/github
mv ~/.ssh/id_ed25519.pub ~/.ssh/github.pub

SSH Key Setup: Local

Specify that the new key is to be used for github.com by adding the following to ~/.ssh/config:

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/github
  IdentitiesOnly yes

SSH Key Setup: GitHub

Add the public key to your GitHub account at Settings > SSH and GPG keys > New SSH key.

Enable Automatic Authentication

Start an SSH agent for your terminal session and add the key to be used. Supply the key’s passphrase when asked:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/github

With the above in place, the git command won’t ask you for the passphrase to you private SSH key anymore - in the current terminal session.

Test Authentication

ssh -T git@github.com -v

More verbose output:

ssh -vvT git@github.com -v

Repo: Check Out via SSH (not HTTPS)

If the repository was checked out via HTTPS, switch to SSH:

git remote set-url origin "$(git remote get-url origin | sed -E 's#https://github.com/(.+)#git@github.com:\1#')"

Comments

Related Posts

Latest Posts

Scripted WordPress to Hugo Migration

Scripted WordPress to Hugo Migration
After having published in WordPress for almost 20 years, it was time for a change. This site is now rendered by Hugo, a static website generator built for Markdown content hosted in a Git repository. The migration from WordPress (HTML) to Hugo (Markdown) was far from trivial. Since I couldn’t find any tool for the job, I developed my own set of migration scripts that fully automate the migration process. You can find them on GitHub along with extensive documentation.
Website