Using SSH To Reach Servers Through Gateways

I use SSH a lot and having to log into all of my machines individually and then run a command gets tedious. In many cases I need to go through a gateway machine before accessing my target machine. Here are a few simple methods for running commands from your machine to a target machine.

Simple two hop

This will put you at the command prompt for serverB.

$ ssh -t user@serverA "ssh user@serverB"

Simple three hop

This command will put you at the prompt three hosts into a network on serverC.

$ ssh -t user@serverA "ssh -t user@serverB 'ssh user@serverC'"

You can replace the ssh user@serverC command with another command such as ping targetserverC and you will get the response ping as if you were sitting at serverB executing the ping.

$ ssh -t user@serverA "ssh -t user@serverB 'ping google.com'"
*******************************************************************
user@serverA's password:
*******************************************************************
user@serverB's password:
*******************************************************************
PING google.com (74.125.45.138) 56(84) bytes of data.
64 bytes from serverB (74.125.45.138): icmp_seq=1 ttl=54 time=10.9 ms
64 bytes from serverB (74.125.45.138): icmp_seq=2 ttl=54 time=2.40 ms
64 bytes from serverB (74.125.45.138): icmp_seq=3 ttl=54 time=2.83 ms
--- google.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 15ms
rtt min/avg/max/mdev = 2.154/3.533/10.952/1.935 ms

Good luck!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>