How to use local ssh key to access a third computer?

1.4k Views Asked by At

i want to know if there is a way to use my local ssh keys to access a git server from a third remote machine. In example: Imagine we have three computers: PC1, PC2 and GITSERVER.

I have my ssh keys stored on PC1 which are authorized on GITSERVER, from PC1 i ssh to PC2 from where i want to clone a repository stored on GITSERVER.

There is a way to do that without copying my private key or creating a new one?

2

There are 2 best solutions below

2
On BEST ANSWER

As Sajib Khan states, a ssh_config file can help you. However, then you are always taking your private key with you. There may be use cases in which you don't want to use your private key on a remote machine. For this, you can define per host if you want to take your key with you to the remote machine:

$ vim ~/.ssh/config
Host remote-pc2 # local name of the host (You can use this on your local terminal to connect to the remote host)
HostName 127.0.0.1 # enter correct IP here
User francesco # your login name
ForwardAgent yes

Another way to implement this: You can start the ssh-agent and add the key to it, which could be done in your .bashrc:

$ vim ~/.bashrc
eval `ssh-agent -s`
ssh-add ~/.ssh/id_rsa
# check if key is there
$ ssh-add -l

This helps you so that you don't have to enter your password whenever you start a ssh session. You can take your ssh key from your local machine to another machine for one connection with this command:

$ ssh -A username@remote-host
0
On

One way is to Forward SSH agent. First, open ssh_config file and add two lines following:

$ sudo nano /etc/ssh/ssh_config 

Host <PC2-server-ip.com>
ForwardAgent yes

Add ssh key into the agent lists:

$ eval "$(ssh-agent)"
$ ssh-add ~/.ssh/id_rsa          # add ssh key to list of agent 
identites
$ ssh-add -l                     # you can see the agent just added

Now, login to PC2 and clone from GITSERVER:

$ ssh <user>@<pc2-server-ip>
$ git clone ...