The intersection of technology and leadership

Using different SSH keys for different hosts

When I’m setting up a new computer, one of the tasks I need to do is set up new SSH keys to access different servers. It’s good practice not to use the same key for different services. Keys are useful so you don’t need to type your credentials in all the time when working on a trusted PC.

Instead of typing something like: ssh thekua@github.com I can just simply type ssh github without being prompted for credentials. Less typing. Win!

After you generate several different keys, you can either add them to the command line when using ssh, but it’s easier just to use the config file (typically found at ~/.ssh/config).

Here’s an example config file you might have assuming you have three different projects:

Host github
    HostName github.com
    User git
    AddKeysToAgent yes
    UseKeychain yes
    IdentityFile ~/.ssh/github_rsa
Host gitlab
    HostName gitlab.com
    User git
    AddKeysToAgent yes
    UseKeychain yes
    IdentityFile ~/.ssh/gitlab_rsa
Host ossproject
    Hostname myossproject.someserver.com
    User thekua
    AddKeysToAgent yes
    UseKeychain yes
    IdentityFile ~/.ssh/myossprojectcreds_rsa

If you have properly installed all of your public keys on the appropriate server, then you should now be able to use the following commands:

ssh github
ssh gitlab
ssh ossproject

Each of these will use different credentials and not know about each other – w00t!

1 Comment

  1. Jens Stahl

    nice eplanation ,thank you!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2024 patkua@work

Theme by Anders NorenUp ↑