Using GitHub Deploy Keys like a Pro

As a devop I want to access multiple private Git repositories from multiple hosts. For security reasons I want to use dedicated deploy keys per repository (and host).

We solve this by creating artificial host entries in the server’s .ssh/config. The fake hostnames makes it easy to map an ssh key to a repository.

Host github-as-sandbox
  HostName github.com
  User git
  IdentityFile /home/devop/.ssh/sandbox_id_rsa
  IdentitiesOnly yes

Let’s test the configuration…

$ ssh github-as-sandbox
PTY allocation request failed on channel 0
Hi datenkollektiv/sandbox! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

With this snippet in place you can clone/and pull the repo regardless of the current user and without using your private Git credentials…

$ git clone git@github-as-sandbox:datenkollektiv/sandbox.git

Double check the remote:

$ git remote -v
origin  git@github-as-sandbox:datenkollektiv/sandbox.git (fetch)

Git it done! ;-)