Blog
How to connect to the same host with different SSH keys
I keep a personal and a work GitHub account on the same laptop, and this SSH config is how they stay apart. It builds on my note on generating an SSH key pair.
Set up your SSH configuration file
To connect to a remote server over SSH, you would normally type the user name, host name and port every time:
$ ssh [email protected] -p 4321
Add these lines to your ~/.ssh/config, and ssh avengers does the same thing:
Host avengers
HostName avengers.com
User tony
Port 4321
You can also choose which key to use for each server with IdentityFile:
Host avengers
HostName avengers.com
User tony
Port 4321
IdentityFile ~/.ssh/id_rsa_avengers
Host marvel
HostName marvel.com
User tony
Port 8080
IdentityFile ~/.ssh/id_rsa_marvel
IdentityFile is most useful when you connect to the same host with different keys. A common case is having two GitHub accounts, one personal and one for work:
Host github_personal
HostName github.com
IdentityFile ~/.ssh/github_personal
Host github_work
HostName github.com
IdentityFile ~/.ssh/github_work
# Default key for every other host
Host *
IdentityFile ~/.ssh/id_rsa
Using the configuration file above, you can clone repositories from your personal GitHub account with:
$ git clone git@github_personal:tonystark/ultron.git
Notice that instead of @github.com, the host is @github_personal, the name defined in the configuration file.
Updating the configuration file
- Open your favorite terminal
- Create or open the SSH configuration file
$ nano ~/.ssh/config