Use multiple ssh key with git

When hosting multiple git repositories on the same server, you may need to use multiple deploy keys to access them. This article will show you how to set up multiple ssh keys for git.

Generate a new ssh key

First, you need to generate a new ssh key for the new repository. You can do this by running the following command:

ssh-keygen -t ed25519 -C "ci-app-one" -f ~/.ssh/ci_app_one.key

Add the new deploy key to the github repository

Repo > Settings > Deploy keys > Add deploy key

cat ~/.ssh/ci_app_one.key.pub # Don't forget the **.pub**

Edit the ssh config file

Configure the new ssh key for the new repository on the remote machine.
You can do this by adding the following lines to the ~/.ssh/config file:

Host one
Hostname github.com
IdentityFile ~/.ssh/ci_app_one.key
IdentitiesOnly yes
User your-username/app-one # app-one is the repository name

Test the new ssh key

ssh -T git@one

You should see a success message like this:

Hi your-username/app-one! You've successfully authenticated, but GitHub does not provide shell access.

Clone the repository using the new ssh key

git clone git@one:your-username/app-one.git

That's it! You have successfully set up multiple ssh keys for git.

You can do the same for other repositories by following the same steps with different key and repository names.