DEV Community

rounakcodes
rounakcodes

Posted on • Edited on

Using Deploy Keys for Github Repo

Introduction

This writing is intended to serve as a quick reference when faced with Github repository access errors while accessing via a remote ssh url.

Steps

Remember a different deploy key is required for each repo.

Run the following commands in a shell

ssh-keygen -t ed25519
# If the above command fails, use *ssh-keygen -t rsa -b 4096*
eval "$(ssh-agent -s)"
ssh-add <location of the private key file>
cat <location of the public key file>
# usually the location is same as the private key file with a *.pub* extension
# copy the output of the above command
Enter fullscreen mode Exit fullscreen mode

Visit your repo in the Github site.

Click these (for screenshots, see link at the bottom):

Settings (usually the last tab)
Deploy keys (in the left sidebar)
Add deploy key (button on the right)

Give any title and paste the key which you copied earlier
Remember to tick the checkbox if write access is intended

Save

Now, you should be able to access the Github repo remotely. Use the ssh url (not https) to clone: git@github.com:<username>/<repo-name>.git

Basic info

SSH keys can be managed on your servers when automating deployment scripts using SSH agent forwarding, HTTPS with OAuth tokens, deploy keys, or machine users.

A deploy key is an SSH key that grants access to a single repository.
GitHub attaches the public part of the key directly to your repository instead of a personal user account, and the private part of the key remains on your server.

Accessing multiple Github repos from the same machine

Do this for each repo

ssh-keygen -t ed25519 -f ~/.ssh/<repo-name>  -C "<repo-name>"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/repo-name
cat ~/.ssh/repo-name.pub
# Copy the output of `cat` and paste it in the corresponding Github repo
Enter fullscreen mode Exit fullscreen mode

TODO: https://stackoverflow.com/a/7927828/10427010

More

For screenshots, more explanation (and also for a day when the above stops working as intended), refer:

https://docs.github.com/en/developers/overview/managing-deploy-keys#deploy-keys

Always read the official docs yourself if you want to dive deep into any subject.

Top comments (0)