Setup ssh keys for multiple github account

Many people have more than one Github accout, tipically one for work and the other is for personal projects. Things get more complecated since there are more ssh keys now. While It's better to use docker containers for each development environment, inside each container you can setup ssh like before -- generate id_rsa using all default options, but put all ssh keys in host should work just fine, with a bit more works as this guide shows.

  1. generate two ssh keys or copy it from host in case of using container

    ssh-keygen -t rsa -b 4096 -C "your_personal_email@example.com"
    ssh-keygen -t rsa -b 4096 -C "your_work_email@example.com"

    copy ssh key(s) to each github account respectively

  2. edit ./ssh/config

    Host github_work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_work
    Host github-private-quantum
    HostName github.com
    User git
    IdentityFile ~/.ssh/quantumabacus_rsa
  3. test ssh connection

    ssh -T git@github_work
    ssh -T git@github-private-quantum

    This will tell you some account information:

    Hi! You've successfully authenticated, but GitHub does not provide shell access.
  4. Cloning repository using a specific ssh key
    For this example I will clone this repository: https://github.com/xyzluo/test_n. You should use your own test repository.

  5. Important: remote url should use git as user name and Host defined in ~.ssh/config.

    git clone git@github-private-quantum:xyzluo/test_n.git
  6. Once it's done, run git remote -v you'll see this:

    origin  git@github-private-quantum:xyzluo/test_n.git (fetch)
    origin  git@github-private-quantum:xyzluo/test_n.git (push)
Categories: tips

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *