Search code examples
sshssh-keys

How to pass local private key to ssh command on intermediate jump server?


I have an intermediate server between my local and my target server.

With password authentication I can ssh to the target like this:

ssh me@intermediate-server ssh me@target-server

But now both server works with key authentication.

How can I do something like this?

ssh me@intermediate-server ssh me@target-server -i /path/to/my-local-prv-key

The above command won't work of course because it looks for the key file on the intermediate-server.

So how to pass my private key to the final command safely?


Solution

  • You cannot, not this way.

    But there's better way anyway, use the -J (jump) switch, instead of nesting the ssh.

    ssh -J me@intermediate-server -i /path/to/my-local-prv-key me@target-server
    

    The -i applies to both servers. But it should do no harm, if the key cannot be used on the target-server. If you need to use a different key on each server, you can use two -i switches (ssh would then try both keys on both servers).

    See also Does OpenSSH support multihop login?