Ignoring for the moment the port forwarding part of your example, you need to use the ProxyCommand
or ProxyJump
directives / -J
option. From the command line, try this:
ssh -i cert.pem -J user@host1-ip user2@host2-ip
Since -J
and ProxyJump
were added in OpenSSH 7.3 (IIRC), you might need to use the older ProxyCommand
directive in your ~/.ssh/config
file, especially if your ssh
on your Mac is not updated recently. Also, this allows you to specify other options such as IdentityFile
to put your PEM file, as in:
Host host1
User host1user
Hostname host1-ip
IdentityFile ~/.ssh/cert.pem
Host host2
User host2user
Hostname host2-ip
IdentityFile ~/.ssh/cert.pem
# hop through host1
ProxyJump host1
# or if older ssh-en, one of:
#ProxyCommand ssh -A host1 nc %h %p
#ProxyCommand ssh -A host1 -W %h:%p
With that in place, it shoud be as simple as:
ssh host2
To add your port forwarding:
ssh host2 -L 9999:localhost:1234