Search code examples
gitsshkeyroaming

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


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?


Solution

  • 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