I used to utilize ssh-agent as well as ssh-add to connect to a server via an intermediate server. There are two articles to explain the principle and the usage.

But this kind of directly forwarding is not secure for your private key REF, I recommend to use the ProxyCommand to release the aim.

Here is an example:

1
2
3
4
5
6
7
8
9
Host hosta
User userfoo
Hostname 123.123.123.123

Host hostb
User userbar
Hostname 192.168.1.1
Port 2222
ProxyCommand ssh -q -W %h:%p hosta

This config gives a solution to connect hostb via hosta, which it will firstly connect hosta and use ProxyCommand to automatically connect hostb.