SSH jump host
An alternative to SSH tunneling to access internal machines through gateway is using jump hosts.
The idea is to use ProxyCommand to automatically execute ssh command on remote host to jump to the next host and forward all traffic through.
Contents
Prerequisites
- SSH access to the gateway machine and the internal one
- Gateway machine has Netcat installed
Dynamic jumphost list
Just concatenate your jumphost(s) and your destination host via the + sign. You don't have to setup a static configuration in your config.
Setup
~/.ssh/configAdd this textHost *+* ProxyCommand ssh $(echo %h | sed 's/+[^+]*$//;s/\([^+%%]*\)%%\([^+]*\)$/\2 -l \1/;s/:/ -p /') exec nc -w1 $(echo %h | sed 's/^.*+//;/:/!s/$/ %p/;s/:/ /')
Usage
user $ssh host1+host2If usernames on machines differ, specify them:
user $ssh user1%host1+host2 -l user2The login name for the last hop has to be given using -l flag.
You can give also port numbers if needed:
user $ssh host1:port1+host2It works with scp command, too:
user $scp filename host1+host2:~/The colon and path at the end is needed so that scp recognizes it as remote.
Multiple jumps
The same syntax can be used to make jumps over multiple machines:
user $ssh host1+host2+host3All but the last jump host need the ProxyCommand set in their SSH configuration.
Static jumphost list
Static jumphost list means, that you know the jumphost or jumphosts you need, to reach a host. Therefore you can create a static jumphost 'routing' in your .ssh/config. The advantage in comparision to the dynamic jumphost option is, that you don't have to provide the .ssh config on jumphosts between your maschine and all the other jumphosts between you and the final host you want to jump to.
Setup
~/.ssh/configAdd this text### First jumphost. Directly reachable Host alphajump HostName jumphost1.example.org ### Host to jump to via jumphost1.example.org Host behindalpha HostName behindalpha.example.org ProxyCommand ssh alphajump netcat -w 120 %h %p
Usage
user $ssh behindalphaIf usernames on machines differ, specify them by modifing the correspondent ProxyCommand line:
~/.ssh/configModify correspondent ProxyCommandProxyCommand ssh -l otheruser behindalpha netcat -w 120 %h %p
It works with scp command, too:
user $scp filename behindalphabeta:~/The colon and path at the end is needed so that scp recognizes it as remote.
Multiple jumps
The same syntax can be used to make jumps over multiple machines:
~/.ssh/configAdd this text### First jumphost. Directly reachable Host alphajump HostName jumphost1.example.org ### Second jumphost. Only reachable via jumphost1.example.org Host betajump HostName jumphost2.example.org ProxyCommand ssh alphajump netcat -w 120 %h %p ### Host only reachable via alphajump and betajump Host behindalphabeta HostName behindalphabeta.example.org ProxyCommand ssh betajump netcat -w 120 %h %p
user $ssh behindalphabetaTips
To ease the connecting even further:
- Set these commands as shell aliases
- To avoid typing passwords use OpenSSH keys