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. |