How to use local Github ssh key when issuing remote git commands on another machine via ssh

Viewed 425

I have a local machine with an ssh keypair with the public one on github. As we all know, this allows me to interface with Github without the need to enter a username/password each time.

I also have a remote machine, which I can ssh into using the same public key. Visual Studio Code, allows me to interface with this machine via a workspace. And I noticed that when I run git commands via its terminal interface, I don't get a password prompt.

When I try to run the same git commands via an ssh session I started via my terminal, I'm prompted for a password.

How is Visual Studio Code achieving this? How can I achieve this with an ssh session started via my regular terminal? Note that I do not want to put my key pair on this remote machine.

I'm using Ubuntu 20.04


EDIT

I just want to note that this question might become more interesting when github removes the option of password authentication later next year.

1 Answers

The canonical way to do this on the terminal is using agent forwarding.

The principle is that you have an "Agent" running on your local machine that has access to your private keys. Typically the agent will have your private keys loaded and decrypted in memory. When you ssh into a remote machine you specify "Agent Forwarding" to give the remote machine access to your local agent via your SSH connection.

This lets your user (and 'root') on the remote machine request your local Agent to sign ssh connection requests.


Assuming a *nix host or Mac then you are most likely using OpenSSH for your client. To setup your agent, and add your private key to it:

ssh-add

Or if you have a special name for your private key then type:

ssh-add path/to/private/key

Then when you ssh into your remote machine just add the -A switch to forward the agent:

ssh -A remote-machine

This will let your user session (and root) use your local SSH key instead of anything stored on the remote host. Git will implicitly pick use the agent, you don't need to give it any special instruction.


If you are using Windows and Putty then it's worth mentioning pageant which works with Putty. This is the Windows equivalent of using ssh-add. It has its own GUI which is fairly self explanatory.

Related