Tunneling to Production

December 1, 2011 code 1 min read

One of the great features of SSH is its ability to tunnel any port to a remote server. I’ve used this SSH command often to connect to a production database:

$ ssh -N -L 27018:localhost:27017

That SSH command takes all traffic to port 27018 on your local host and tunnels it to 27017 on the remote server.

If you rarely use this command, it’s easy to forget it. So why not create the tunnel directly in the scripts that need it using net-ssh-gateway We’ve been using this on and it works great.

require 'net/ssh/gateway'

gateway =, username)'', 27017, 27018)

db ='', 27018).db('myapp_production')
# …
This content is open source. Suggest Improvements.


avatar of Brandon Keepers I am Brandon Keepers, and I work at GitHub on making Open Source more approachable, effective, and ubiquitous. I tend to think like an engineer, work like an artist, dream like an astronaut, love like a human, and sleep like a baby.