SSH tunnel via another host

Problem

Admins at example.com only allow ssh connections from whitelisted IP addresses. I can get my IP whitelisted, but my IP changes. A lot. And I also use several ISPs. So that’s pretty much a no-go for me.

Solution

SSH tunneling, of course!

We’ll need another host that has fix IP, let’s call it myHost.com. We also need an open port on myHost.com to connect to from localhost. Let that be port 9999

After googling and reading the man pages, here is what I came up with. It requires one terminal window to keep the bridge up, but that is intentional, so that I can simply close the bridge when ssh is no longer required.

This opens the bridge:

localhost$ ssh -t myhostuser@myHost.com "ssh -N -L $(echo $SSH_CLIENT | awk '{ print $1}'):9999:example.com:22 exampleuser@example.com"

And this opens an SSH session via that bridge:

localhost$ ssh exampleuser@myHost.com -p 9999

Note the username, it is for example.com, not for myHost.com.

Details

Todo, sorry 🙂