Connect to GitHub via SSH on Linux
If you’re tired of entering a username and password each time you push to a GitHub repository on the command line, a secure method for bypassig this is to set-up a connection using a set of SSH keys.
The SSH protocol allows a user to connect and authenticate to remote servers and services. With SSH keys, you can connect to GitHub without supplying your username and personal access token at each visit.
Part 1: Setting up the SSH keys
To set-up SSH keys on the local machine, enter the following command:
$ ssh-keygen -t ed25519-sk -C "firstname.lastname@example.org"
Note: If the command fails and you receive the error
invalid format or feature not supported, you may be using a hardware security key that does not support the Ed25519 algorithm. Enter the following command instead.
$ ssh-keygen -t ecdsa-sk -C "email@example.com"
When you are prompted to “Enter a file in which to save the key,” press Enter to accept the default file location.
When you are prompted to type a passphrase, press Enter (you needn’t enter a value here). Press Enter a second time when asked to confirm the passphrase.
If successful, you should see output similar to this:
Your identification has been saved in /home/username/.ssh/id_ed25519. Your public key has been saved in /home/username/.ssh/id_ed25519.pub. The key fingerprint is: SHA256:8XBlePQHxbcGEGeRV9KD1WK7tRf3nIc40k/5jRgbTZDpTo firstname.lastname@example.org The key's randomart image is: +--[ED25519 256]--+ | .. o.o.. .| | o oo+ B.+| | B . o.=+| | S + o.+.+| | o.o o.| | .. o. . o *.O o| | ... . o * * | | +. o + o| | .o+ . | +----[SHA256]-----+
This means that your SSH key has been added to the ssh-agent.
Part 2: Save the SSH keys to GitHub
The administrator of the repository needs to unstall the SSH key. In GitHub, go to Settings > SSH & GPG Keys and click on “New SSH Key“.
On the command line, enter the following command to view your public key:
Copy the key and then return to the GitHub settings page.
Give the key a name and then paste the code into the “Key” text box. Press “Add SSH key”
Part 3: Update the origin URI
Assuming your repository already has an origin URI using HTTP, remove the origin URL with the following command:
git remote remove origin
Next, add an SSH origin URL via the following command:
git remote add origin email@example.com:Repository_Owner_Account/Repository_Name.git
You should now be able to push updates to GitHub repositories without having to enter a username/password combination on each occasion. To maintain account security, you can regularly review your SSH keys list in GitHub and revoke any keys that are invalid or have been compromised.
GutHub has additional information about SSH keys on their website.