TIL how to use local SSH keys on a remote host

⋅ 3 minute read ⋅ 432 words

I am currently working on some Rust code that requires access to an NVIDIA GPU to run. Since I am working on a MacBook and don’t own a GPU, I paid for a Pod on runpod.io   .

The Pod is configured with an NVIDIA GeForce RTX 4090 and runs a Docker image with my development environment that I prepared locally.

Problem

I baked all the necessary libraries and frameworks into the Docker image, but I also need the container to access my private GitHub repository. At the same time, I am not very keen on putting my private SSH keys into the container.

Solution

It turns out that there is a neat solution for this: SSH agent forwarding. This allows the remote machine to forward any signature request back to my local machine, e.g. to sign an SSH authentication challenge from GitHub. The remote machine basically relays the request, which also means that the private key never leaves the local machine.

I set this up in the following way:

  1. On my machine I create a new SSH key for GitHub with scoped (read-only) permissions and call it github_runpod.

  2. I enable forwarding with ForwardAgent for SSH connections to runpod in my ~/.ssh/config:

    Host runpod
    HostName <THE-IP-OF-THE-POD>
    Port <THE-PORT-OF-THE-POD>
    User dev 
    IdentityFile ~/.ssh/runpod
    ForwardAgent yes
  3. I load the GitHub key into my local agent:

    ssh-add --apple-use-keychain -c  ~/.ssh/github_runpod 

    The -c option requires the user to confirm before it is used in authentication. This is the command for MacOS. On Linux drop the --apple-use-keychain. Also for -c to work on MacOS you need to install ssh-askpass   . 1

  4. I SSH into the remote runpod to test that it works:

    ssh runpod

    and

    ssh-add -l

    should show the forwarded key. Now git clone uses the local github_runpod key when cloning a private repository:

    git clone git@github.com:PrivateRepoAccount/Repo.git

Security Risks

If an attacker had access to the runpod, they can’t steal your private key. However, they can ask your local agent to sign challenges using any of the local keys added to the agent. The risks can be somewhat mitigated by using -c flag, using only a tightly scoped read-only key, and adding destination constraints   using the -h option.

Other options

There are a couple of other ways of granting the runpod access to private Github repositories:

  • Copy a specifically created read-only private key to the runpod. This only works if you also purchase persistent storage from runpod, because otherwise you lose the key file when the pod is paused or terminated.
  • Personal access token configured in GitHub and added as a runpod environment variable.

Footnotes:

  1. This is the command for MacOS. On Linux drop the --apple-use-keychain. Also for -c to work on MacOS you need to install ssh-askpass   . ↩︎

If you have any thoughts, questions, or feedback about this post, I would love to hear it. Please reach out to me via email (public key) .


Tags:
#ssh