DEV Community

Cover image for Single ssh tunnel command with a jump
Irving Gabriel Ocampo
Irving Gabriel Ocampo

Posted on

Single ssh tunnel command with a jump

If you are looking for how connect via ssh to a private server; that's commonly behind a other server or trough a firewall, this is a post I hope will help you.

The method most common used to access a remotely server is via a ssh connection, this command allow us create a channel, a secure channel, point to point type.

Even though I don't talk how set up your ssh in depth (but if you want i made a post talking about it, let me know), it is importan you know the basics about ssh use for implement this commands to pull up a tunnel with middle jumps.

The commands below show how pull up these tunnels with a single intermediary server and other one with two middle hosts. If you understand what is used to made it, you can use this theory in some software you know like as putty services.

variables

  • [middle-server] : Name or IP address from remote server you need cross.
  • [reach-host] : Name or IP address from remote hidden host you need reach.

Using the tools that Openssh provides to us, we pull up a single tunnel:

ssh -fN username@[middle-server] -L 1030:[reach-host]:22
Enter fullscreen mode Exit fullscreen mode

Notice: This IP from reach host has two numbers to side; to the left we has the port we use to make the local connection through localhost ports. The other one, to right side, is the port protocol we decide use for this connection, the obviously answer is the port 22 of ssh protocol.

You must be asking what are these flags, well the first two (fN) is for send to background the process of this tunnel and keep up the channel and avoid execute some extra command on end side when the connection is established. You can see the complete documentation if you run the command 'man ssh' in you terminal.

Now, we well make a tunnel with two middle host cross to us end server we want to reach:

ssh -fN username@[middle-server] -L 1030:[second-middle-server]:22
Enter fullscreen mode Exit fullscreen mode

This first tunnel above we define the connection with the first middle host jump.

ssh -fN -p 1030 username@localhost -L 1031:[reach-host]:22 -L 1032:[other-reach-host]:22
Enter fullscreen mode Exit fullscreen mode

In this other one above, we connect the previous tunnel to use and reach the end host, in the flag '-p' we indicate we want connect through local port 1030 and make another tunnel in other local port (1031) for the end host we want to see and with the same scheme we can add another end host if we want.

And is done, just need to do a ssh connection via localhost in the port defined for the reach host we was set.

Some command like this:

ssh username@localhost -p 1031
Enter fullscreen mode Exit fullscreen mode

if you have do made a connection before, you may know this is the usual ssh connection command.

Top comments (0)