Skip to main content.

Reverse SSH tunneling

2014-08-27

Live demo in BSD Now Episode 052. | Originally written by TJ for bsdnow.tv | Last updated: 2014/08/27

NOTE: the author/maintainer of the tutorial(s) is no longer with the show, so the information below may be outdated or incorrect.

We've done a number of SSH tutorials in the past, but most of them rely on the fact that you have a certain level of control on the network. In some cases, you need to be able to access a system that's behind a firewall. This guide will show you how to do just that - reversing the connection and accessing an internal system from the outside. The only requirement in this case is that the firewall allows outbound SSH traffic. You'll have to have access to the machine behind the firewall at some point for this to work. The -R switch will play a key role in this (very short) tutorial. Since we'll be reversing the connection, be sure your client system has a publicly-accessible sshd running. On the system behind the firewall, run the following:

$ ssh -fN -R 9000:localhost:22 user@clientip

Replace "clientip" with the IP of your system and "22" with the port on which you run sshd. It's recommended to run that command in tmux so it doesn't get lost. You might want to consider running sshd on port 443, so it looks similar to normal SSL traffic. See our stunnel tutorial for more ideas there. Now move back to the client system, and we'll make the reverse connection like so:

$ ssh -p 9000 user@127.0.0.1

While it may appear to be connecting on the loopback device, it's actually using the already-established connection made by the internal machine. You'll need to use the username and password/key that you normally would for the internal system. Some recommended settings to have in the client's /etc/ssh/sshd_config:

ClientAliveInterval 300
TCPKeepAlive yes

With these, the internal system will send a packet to the client every five minutes to keep the connection from dying due to inactivity. One problem with this setup is, of course, if the first connection dies. Another possible issue is if your client's IP changes. While not much can be done about the first one (aside from maybe a cron job to try and re-establish the connection), there is a good way to handle the second situation. If you use something like SSH chaining, you can leave the internal system connected to a dedicated server whose IP doesn't ever change. From there, connect to the server, then to localhost.

Latest News

New announcement

2017-05-25

Hi, Mr. Dexter. Also, we understand that Brad Davis thinks there should be more real news....

Two Year Anniversary

2015-08-08

We're quickly approaching our two-year anniversary, which will be on episode 105. To celebrate, we've created a unique t-shirt design, available for purchase until the end of August. Shirts will be shipped out around September 1st. Most of the proceeds will support the show, and specifically allow us to buy...

New discussion segment

2015-01-17

We're thinking about adding a new segment to the show where we discuss a topic that the listeners suggest. It's meant to be informative like a tutorial, but more of a "free discussion" format. If you have any subjects you want us to explore, or even just a good name...

How did you get into BSD?

2014-11-26

We've got a fun idea for the holidays this year: just like we ask during the interviews, we want to hear how all the viewers and listeners first got into BSD. Email us your story, either written or a video version, and we'll read and play some of them for...


Episode 232: FOSDEM 2018

2018-02-07

Direct Download:HD VideoMP3 AudioTorrent> This episode was brought to you by Headlines [FOSDEM Trip report] Your BSDNow hosts were both at FOSDEM in Brussels, Belgium over the weekend. On the friday before FOSDEM, we held a FreeBSD devsummit (3rd consecutive year), sponsored by the FreeBSD Foundation and organized by Benedict (with the help from Kristof...

Episode 231: Unix Architecture Evolution

2018-01-24

Direct Download:HD VideoMP3 AudioTorrent This episode was brought to you by Headlines Unix Architecture Evolution from the 1970 PDP-7 to the 2017 FreeBSD Q: Could you briefly introduce yourself? I’m a professor of software engineering, a programmer at heart, and a technology author. Currently I’m also the editor in chief of the IEEE...

Episode 230: Your questions, Part III

2018-01-24

Direct Download:HD VideoMP3 AudioTorrent> This episode was brought to you by Headlines KPTI patch lands in FreeBSD -current After a heroic effort by Konstantin Belousov kib@FreeBSD.org, the first meltdown patch has landed in FreeBSD This creates separate page tables for the Kernel and userland, and switches between them when executions enters the kernel, and when...

Episode 229: The Meltdown of Spectre

2018-01-17

Direct Download:HD VideoMP3 AudioTorrent> This episode was brought to you by Headlines More Meltdown Much has been happened this week, but before we get into a status update of the various mitigations on the other BSDs, some important updates: Intel has recalled the microcode update they issued on January 8th. It turns out this update...