Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Configure local and remote port forwarding with an SSH tunnel


    Learn how to configure SSH tunnels with local and remote port forwarding. This allows you to bypass intranet firewalls and setup persistent connections between your local host machine and a remote server.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Instructor: Let's say you are on a network connection which is blocking access to a specific website. You can easily get around this with an SSH tunnel and local port forwarding. The -L flag is what tells SSH to open a local tunnel.

    The value of this flag is the local port you wish to use for the tunnel, followed by a colon, followed by the website host you wish to access, followed by a colon, and finally, the value of the port you wish forwarded.

    In this example, you will forward the local port 8000 over to on port 80 of the remote host. Lastly, specify the SSH server you wish to tunnel the connection through. This is the server where the final request will be passed through.

    When you try to access on your local machine, what happens here is that the request on port 8000 under local machine is being passed to the remote host, and then to on port 80 of the remote host. The result is then forwarded back to our local port and host.

    You can use this method to get around intranet firewalls, as well as connecting to blocked remote services.

    Note that the SSH tunnel is keeping the terminal window open. If you specify a -f flag, it tells SSH to run in the background, and a -N does not open a shell window. Since the SSH tunnel runs in the background, you can look up the process by using ps aux, piped to grep, followed by SSH or the port or the tunnel.

    To kill the tunnel, use the kill command, followed by the ID of the running process.

    Another way to use SSH tunnels is to forward remote ports. This will forward connections on a remote host back to our local host machine. This is useful if a user wants to access your local host machine but, for one reason or another, is unable to connect directly to it.

    Note that remote port forwarding is disabled on SSH servers by default, so you need to edit the SSH t config file. It is typically located at /etc/ssh/ssht_config. Once open, set the gateway ports configuration value to yes, then restart the SSH server by running service ssh restart.

    The command to establish an SSH tunnel is similar to before, but you use a -R flag rather than a -L flag. The first value defined is the port you wish to listen to on the remote host. Port 8000, followed by localhost, followed by the local port that requests should be forwarded to, in this case, 3000.

    Then, of course, specify the SSH connection string of the remote host to use for the connection. Now if you load that URL on the remote host or IP on port 8000, it will tunnel requests to port 3000 on a local machine.