Web Servers

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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:

1
2
3
ssh github
ssh gitlab
ssh ossproject

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

Published on System Code Geeks with permission by Patrick Kua, partner at our SCG program. See the original article here: Using different SSH keys for different hosts

Opinions expressed by System Code Geeks contributors are their own.

Patrick Kua

Patrick Kua is an author, speaker and consultant who still finds time to code. He works as an active, generalizing specialist at ThoughtWorks and dislikes being put into a box. He is often found leading technical teams, coaching people and organisations in lean and agile methods, sometimes facilitating situations beyond adversity. Patrick is fascinated by elements of learning and continuous improvement, always helping others to develop an enthusiasm for the same.
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button