Snowball's answer helped a lot. However, I made some modifications to the command and wanted to explain how it works.
Given this situation:
ssh ssh
A -------> B -------> C
^ ^
using A's using B's
ssh key ssh key
Modify your ~/.ssh/config
file and add the host B
through which you want to jump, just how you would normally configure a host:
Host B
User myusername
HostName b.mycompany.com
Then you add the host C
that you want to end up on:
Host C
User myusername
HostName c.intranet.mycompany.com
ProxyCommand ssh -T -q -o 'ForwardAgent yes' B 'ssh-add -t 1 && nc %h %p'
Note the ProxyCommand
, where:
ssh -T -q
indicates that it should not allocate a pseudo-TTY (-T
) and be quiet (-q
);
- once on the jump host
B
, we add the key to the SSH keys of A
through ssh-add
;
- which only works because we forwarded the SSH agent using
-o 'ForwardAgent yes'
.
ssh-add -t 1
indicates that I want the key to be added only for the 1 second needed to authenticate to the final host C;
- and finally,
nc %h %p
initiates a netcat
connection to the final host %h
at port %p
(both which will be filled out by SSH based on the information in the ~/.ssh/config
file).
If you need to specify a custom key on B
to use, you can do that by modifying the ssh-add
part:
Host C
User myusername
HostName c.intranet.mycompany.com
ProxyCommand ssh -T -q -o 'ForwardAgent yes' B 'ssh-add -t 1 ~/.ssh/mykey && nc %h %p'