DEV Community

Cover image for Setup GitHub ssh keys on Linux servers in 59 seconds
Matthew Wang
Matthew Wang

Posted on

Setup GitHub ssh keys on Linux servers in 59 seconds

TLDR

ssh-keygen -t ed25519 -C "email@example.com" 

eval "$(ssh-agent -s)"

ssh-add ~/.ssh/id_ed25519

cat ~/.ssh/id_ed25519.pub 

# Copy public key

# Create & Paste into GitHub deploy keys

git clone git@github.com:USER/foo.git
Enter fullscreen mode Exit fullscreen mode

I often forget how to set up GitHub ssh keys for deployment. Here is a quick guide on how to set it up.

All of these commands can be found on GitHub's guides but spread around many pages. This guide consolidates them

1. Create your unique ssh key pair.

It's recommended to use the ed25519 algorithm instead of rsa nowadays.

Note: the email doesn't do anything. It's just for labeling.

ssh-keygen -t ed25519 -C "myemail@example.com" 
Enter fullscreen mode Exit fullscreen mode

If you already have a pair of ed25519 keys you may skip this step.

This will prompt you to save the key to a location. Using the default location is fine.

Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/me/.ssh/id_ed25519)
Enter fullscreen mode Exit fullscreen mode

Just press enter.

Create a passphrase if needed. Otherwise, just press enter.

Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter the same passphrase again: [Type passphrase again]
Enter fullscreen mode Exit fullscreen mode

2. Start ssh-agent

eval "$(ssh-agent -s)"
Enter fullscreen mode Exit fullscreen mode

Why use eval? ssh-agent -s generates the bash code to run. Then, we use eval to run that code.

3. Add the key to your ssh-agent

ssh-add ~/.ssh/id_ed25519
Enter fullscreen mode Exit fullscreen mode

4. Copy the public key

cat ~/.ssh/id_ed25519.pub 
Enter fullscreen mode Exit fullscreen mode

Copy output public key

5. Create Github deploy key

Your Repo > Settings > Deploy Keys

Paste your public key into your new deploy key for your repo.

Github deploy key page

6. Pull from your GitHub Repo

Code Dropdown > SSH Tab > Copy URL

Copy github ssh URL screenshot

git clone git@github.com:USER/foo.git
Enter fullscreen mode Exit fullscreen mode

And you're done!

References

Github: Managing deploy keys

Github: Generating a new SSH key and adding it to the ssh-agent

Github: Adding a new SSH key to your GitHub account

Top comments (0)