# SSH Tunnel - Local and Remote Port Forwarding Explained With Examples

DevFeed: [SSH Tunnel - Local and Remote Port Forwarding Explained With Examples](<https://devfeed.tech/articles/ssh-tunnel-local-and-remote-port-forwarding-explained-with-examples-20372.md>)

Original publisher: [Read original article](<https://blog.sensible.io/2014/05/17/ssh-tunnel-local-and-remote-port-forwarding-explained-with-examples.html>)

Author: sensible.io team

Published: 2014-05-17T17:57:00Z

Content type: tutorial

Language: en

Sources: [Sensible](<https://devfeed.tech/sources/sensible.md>)

Topics: [ssh](<https://devfeed.tech/topics/ssh.md>), [OpenSSH](<https://devfeed.tech/topics/openssh.md>), [Database](<https://devfeed.tech/topics/database.md>), [PostgreSQL](<https://devfeed.tech/topics/postgresql.md>), [Firewall](<https://devfeed.tech/topics/firewall.md>), [Rails](<https://devfeed.tech/topics/rails.md>), [SockTz](<https://devfeed.tech/topics/socktz.md>)

Tags: [browser](<https://devfeed.tech/tags/browser.md>), [database](<https://devfeed.tech/tags/database.md>), [examples](<https://devfeed.tech/tags/examples.md>), [firewall](<https://devfeed.tech/tags/firewall.md>), [local](<https://devfeed.tech/tags/local.md>), [nat](<https://devfeed.tech/tags/nat.md>), [network](<https://devfeed.tech/tags/network.md>), [port](<https://devfeed.tech/tags/port.md>), [postgresql](<https://devfeed.tech/tags/postgresql.md>), [rails](<https://devfeed.tech/tags/rails.md>), [remote](<https://devfeed.tech/tags/remote.md>), [security](<https://devfeed.tech/tags/security.md>), [server](<https://devfeed.tech/tags/server.md>), [ssh](<https://devfeed.tech/tags/ssh.md>), [syntax](<https://devfeed.tech/tags/syntax.md>), [tutorial](<https://devfeed.tech/tags/tutorial.md>)

## AI overview

A tutorial explaining SSH local and remote port forwarding through examples, including access to blocked web resources, databases behind firewalls, and locally hosted applications without public IP addresses.

## Source excerpt

There are two ways to create an SSH tunnel, local and remote port forwarding (there's also dynamic forwarding, but we won't cover that here). The best way to understand these is by an example, let's start with local port forwarding. Imagine you're on a private network which doesn't allow connections to a specific server. Let's say you're at work and imgur.com is being blocked. To get around this we can create a tunnel through a server which isn't on our network and thus can access Imgur. $ ssh -L 9000:imgur.com:80 user@example.com The key here is -L which says we're doing local port forwarding. Then it says we're forwarding our local port 9000 to imgur.com:80, which is the default port for HTTP. Now open your browser and go to http://localhost:9000. The awesome thing about SSH tunnels is that they are encrypted. Nobody is going to see what sites you're visiting, they'll only see an SSH connection to your server. Connecting to a database behind a firewall Another good example is if you need to access a port on your server which can only be accessed from localhost and not remotely. An example here is when you need to connect to a database console, which only allows local connection for security reasons. Let's say you're running PostgreSQL on your server, which by default listens on the port 5432. $ ssh -L 9000:localhost:5432 user@example.com The part that changed here is the localhost:5432, which says to forward connections from your local port 9000 to localhost:5432 on your server. Now we can simply connect to our database. $ psql -h localhost -p 9000 Now let's stop here for a little bit an explain what is actually going on. In the first example the 9000:imgur.com:80 is actually saying forward my local port 9000 to imgur.com at port 80. You can imagine SSH on your server actually making a connection (a tunnel) between those two ports, one on your local machine, and one on the target destination. If we however say something like 9000:localhost:5432, it means localhost