Skip to content

Latest commit

 

History

History
114 lines (77 loc) · 2.48 KB

ssh.md

File metadata and controls

114 lines (77 loc) · 2.48 KB

Multiple SSH Keys settings for different github account

create different public key

create ssh key according the article Set-Up Git

$ ssh-keygen -t ed25519 -C "[email protected]"
$ ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/id_ed25519_work

*** -f filename ***

Please refer to github ssh issues for common problems.

for example, 2 keys created at:

~/.ssh/id_ed25519
~/.ssh/id_ed25519_work

then, add these two keys as following

$ ssh-add ~/.ssh/id_ed25519
$ ssh-add ~/.ssh/id_ed25519_work

you can delete all cached keys before

$ ssh-add -D

finally, you can check your saved keys

$ ssh-add -l

Add ssh to Github

Reference: FYI Adding a new SSH key to your GitHub account

Modify the ssh config

$ cd ~/.ssh/
$ touch config
$ vi config
  • create config if not existed

Then added

# Personal account
Host github.com
        HostName github.com
        User git
        PreferredAuthentications publickey
        IdentitiesOnly yes
        IdentityFile ~/.ssh/id_rsa
# Work account
Host work.github.com
        HostName github.com
        User git
        PreferredAuthentications publickey
        IdentitiesOnly yes
        IdentityFile ~/.ssh/id_rsa_work

Testing SSH both

Clone you repo and modify your Git config

Uses standard SSH key git clone [email protected]:rails/rails.git

Uses work SSH key git clone [email protected]:rails/rails.git

cd rails and modify git config(for display author with work email)

$ git config user.name "work"
$ git config user.email "[email protected]" 

then use normal flow to push your code

$ git add .
$ git commit -m "your comments"
$ git push

Bonus

Setting ./gitconfig

vi ~/.gitconfig

Add this block to bottom line

[includeIf "gitdir:~/work/"]
  [user]
    name = "work"
    email = [email protected]"
  [core]
    sshCommand = "ssh -i ~/.ssh/id_rsa_work"

To select git work account we have clone repo under ~/work directory.

That's it! 🎉