Use ProxyCommand
See man ssh_config
. I recommend making use of ProxyCommand
. Let's take your original scenario:
- Computer A (your computer)
- Computer B (a proxy hostname)
- Computer C (only reachable via SSH from Computer B)
Edit ~/.ssh/config
with the following contents.
Host computerb
HostName <hostname or IP of Computer B>
Host computerc 192.168.35.*
ProxyCommand ssh computerb nc -w 180 %h %p
Now you'll be able to transparently reach Computer C. e.g.
ssh computerc
Advantages of this method
More secure
You only need your private key to be on Computer A (your computer). The nc
command will act as a proxy in which SSH will encrypt traffic through. This includes authentication. It is a very bad idea to distribute your private key to multiple servers (as any compromised server with your private key ultimately compromises your private key).
Matches Multiple destinations
One can match multiple destination computers using Host
. A single computer or any computer within a specific network (e.g. 192.168.35.0/24
in the above example) to proxy through Computer B. It also serves as an alias.
ssh 192.168.35.27
In the above example, it will proxy through Computer B to get to the IP address.
Daisy chain proxies
Using this method you can daisy chain as many automatic proxies as necessary. e.g. you can add a Computer D which is only reachable from Computer C and it will work transparently.
Host computerd
ProxyCommand ssh computerc nc -w 180 %h %p
ssh computerd
will automatically proxy through Computer C and Computer B in the above ssh_config
examples.