Any developer has at least one SSH key, mostly used for login through SSH to remote machines or working with GIT repositories.
When we try to connect through SSH to another machine or pulling from a GIT repository, our system is going to use the first SSH key we created by default.
So, what happens if we want to use different SSH Keys for same server or same repository? How we deal with this trouble?.
We have to make a config
file in our SSH folder and specify to the system which key has to use given host.
Let’s see an example where we have two different repositories hosted in Github.com and we want to use different SSH keys for each repository.
First we have to create our ~/.ssh/config file:
Host project_1
HostName github.com
IdentityFile ~/ssh/dev_key
Host project_2
HostName github.com
IdentityFile ~/ssh/deploy_key
For example, it is common having multiple SSH keys in our production server, where each SSH key have access to specific repositories.
Now in our production server, if we want to pull changes for our project_1 we have to do run this command:
git pull git@project_1:user/project.git
For project_2, will be the same command with the host in the repository url modified:
git pull git@project_2:user/project.git
Analyzing the config file, Host it’s the alias for our hostname, in our case github.com, and in the IdentityFile we link to our SSH key path.
That’s all, easy and powerful.
A young developer from Madrid who loves programming and computing. Constantly testing with new technologies and thinking in new projects and challenges.