As you probably don’t know GitHub doesn’t allow to use the same deploy key for multiple repositories.
If you want to have multiple GitHub deploy keys on the same server the easiest way would be using ssh config aliases.
I wanted to git clone a second GitHub repository on the same server and I got this error message:
➜ git clone [email protected]:Renatello/secondrepository.git Cloning into 'secondrepository'... fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
GitHub didn’t let me use the same deploy key for a second repository.
Key is already in use
I then came across this solution using
~/.ssh/config. First, generate a second ssh key using
➜ ssh-keygen -t rsa -f ~/.ssh/id_rsa_secondrepository Generating public/private rsa key pair. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa_secondrepository. Your public key has been saved in /root/.ssh/id_rsa_secondrepository.pub.
Hi, I'm Renat 👋
Start your ssh-agent with the following command:
➜ eval "$(ssh-agent -s)" Agent pid 31457
And then add your newly generated private key identity to the authentication agent, ssh-agent.
➜ ssh-add ~/.ssh/id_rsa_secondrepository Identity added: /root/.ssh/id_rsa_secondrepository (/root/.ssh/id_rsa_secondrepository)
cat this public key and add it to your second GitHub repository deploy keys.
➜ cat ~/.ssh/id_rsa_secondrepository.pub
Then, you need to add 2 hosts for GitHub using the key you just created and the key you created previously.
# ~/.ssh/config # First host Host firstrepository github.com HostName github.com IdentityFile ~/.ssh/id_rsa # Second host Host secondrepository github.com HostName github.com IdentityFile ~/.ssh/id_rsa_secondrepository
You should now be able to clone a second GitHub repository.
➜ git clone [email protected]:Renatello/secondrepository.git Cloning into 'secondrepository'...
If you find this post useful, please let me know in the comments below.
Want to share this with your friends?