I have a number of projects on a single server that all have their own git repos on Github. My problem is that I need to create a separate deploy key for each one of these but I have to keep adding and re-adding the ssh-keys when I am doing deployments, ex:
eval "$(ssh-agent -s)"
ssh-add id_site1
I figured that there had to be a way to simplify this using SSH config but ended up with something like this which did not work, it always assumed the first entry in the list.
# Site1
Host www.github.com
HostName github.com
User git
IdentityFile /root/.ssh/id_site1
# Site2
Host www.github.com
HostName github.com
User git
IdentityFile /root/.ssh/id_site2
The way around this was the Host parameter in SSH config, I can make these whatever I like and then make sure my remote connects to them correctly (kind of like editing /etc/hosts on your local machine to connect to staging sites).
For example, my remote for Site1 was currently
$ git remote -v show
origin git@github.com:username/site1.git (fetch)
origin git@github.com:username/site1.git (fetch)
I updated my site entry in my SSH config to use a custom hostname, ex: site1.github.com
# Site1
Host site1.github.com
HostName github.com
User git
IdentityFile /root/.ssh/id_site1
# Site2
Host site2.github.com
HostName github.com
User git
IdentityFile /root/.ssh/id_site2
then I removed and readded my git remote to use ssh with my updated site origin:
git remote rm origin
git remote add origin git@site1.github.com:username/site1.git
If it doesn't recognize your settings try running the following to flush SSH config:
eval "$(ssh-agent -s)"