How to Manage Multiple SSH Keys for GitHub, GitLab, etc.

Short tutorial on how to structure projects and setup multi-user Git SSH workflow.

When using a single machine with multiple git accounts, you might encounter some obstacles with SSH setup. The easiest way to achieve a multi-user setup is to structu…


This content originally appeared on DEV Community and was authored by Karolis Šarapnickis

Short tutorial on how to structure projects and setup multi-user Git SSH workflow.

When using a single machine with multiple git accounts, you might encounter some obstacles with SSH setup. The easiest way to achieve a multi-user setup is to structure git repositories by respective directories, e.g.:

projects
├── work
│   ├── enterprise-fiz-buz
│   └── ...
└── personal
    ├── karolis.sh
    └── ...

This short tutorial will help you set up SHH for such workflow.

Generating a new SSH key

To generate a new SSH key, use the provided template script (or check the docs):

export EMAIL=karolis.sarapnickis@work.com
export SSH_FILE=$HOME/.ssh/id_rsa_work
ssh-keygen -t rsa -b 4096 -C $EMAIL -f $SSH_FILE
ssh-add -K $SSH_FILE
echo "? PUBLIC KEY BELOW ?" && cat $SSH_FILE.pub
pbcopy < $SSH_FILE.pub

What's left is to add the SSH key to your GitHub/GitLab/etc. account.

Multi-user Git SSH setup

The general idea is that you use specific ssh command based on your working directory with the help of git includes.

Update the ~/.gitconfig file:

[user]
  name = Karolis Šarapnickis
  email = pastas.k@gmail.com
[core]
  sshCommand = ssh -i ~/.ssh/id_rsa
[push]
  default = current
[includeIf "gitdir:~/projects/work/"]
  path = ~/.gitconfig.work

And the ~/.gitconfig.work file:

[user]
  email = karolis.sarapnickis@work.com
[core]
  sshCommand = ssh -i ~/.ssh/id_rsa_work

And that's it! Appropriate SSH setup will be used based on directory location, no extra actions are needed.


This content originally appeared on DEV Community and was authored by Karolis Šarapnickis


Print Share Comment Cite Upload Translate Updates
APA

Karolis Šarapnickis | Sciencx (2021-09-12T17:15:56+00:00) How to Manage Multiple SSH Keys for GitHub, GitLab, etc.. Retrieved from https://www.scien.cx/2021/09/12/how-to-manage-multiple-ssh-keys-for-github-gitlab-etc/

MLA
" » How to Manage Multiple SSH Keys for GitHub, GitLab, etc.." Karolis Šarapnickis | Sciencx - Sunday September 12, 2021, https://www.scien.cx/2021/09/12/how-to-manage-multiple-ssh-keys-for-github-gitlab-etc/
HARVARD
Karolis Šarapnickis | Sciencx Sunday September 12, 2021 » How to Manage Multiple SSH Keys for GitHub, GitLab, etc.., viewed ,<https://www.scien.cx/2021/09/12/how-to-manage-multiple-ssh-keys-for-github-gitlab-etc/>
VANCOUVER
Karolis Šarapnickis | Sciencx - » How to Manage Multiple SSH Keys for GitHub, GitLab, etc.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/12/how-to-manage-multiple-ssh-keys-for-github-gitlab-etc/
CHICAGO
" » How to Manage Multiple SSH Keys for GitHub, GitLab, etc.." Karolis Šarapnickis | Sciencx - Accessed . https://www.scien.cx/2021/09/12/how-to-manage-multiple-ssh-keys-for-github-gitlab-etc/
IEEE
" » How to Manage Multiple SSH Keys for GitHub, GitLab, etc.." Karolis Šarapnickis | Sciencx [Online]. Available: https://www.scien.cx/2021/09/12/how-to-manage-multiple-ssh-keys-for-github-gitlab-etc/. [Accessed: ]
rf:citation
» How to Manage Multiple SSH Keys for GitHub, GitLab, etc. | Karolis Šarapnickis | Sciencx | https://www.scien.cx/2021/09/12/how-to-manage-multiple-ssh-keys-for-github-gitlab-etc/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.