SSH Tunneling Explained: The Hidden Superpower Behind Secure Connections

Local, remote and dynamic SSH tunnels: what each one does, the command for it, real DevOps use cases, and fixes for the errors you’ll hit.

Dhananjay AggarwalDhananjay Aggarwal, · 9 min read
Share
Summarize with AI
A glowing SSH padlock above the title SSH Tunneling Explained: The Hidden Superpower Behind Secure Connections. From a post by Dhananjay Aggarwal.

SSH is already powerful for remote login, but its real magic lies in a feature many beginners overlook: SSH tunneling.

If you work with Linux, DevOps, cloud or backend systems, SSH tunneling is one of those skills that instantly separates a beginner from a real engineer. It lets you securely reach internal services, get around restrictive networks, forward ports, and create encrypted pathways that protect your data from eavesdroppers.

SSH tunneling sends network traffic through an encrypted SSH connection between your machine and a remote server. There are three kinds: local forwarding (ssh -L) reaches a remote service from your laptop, remote forwarding (ssh -R) exposes a local service to a remote server, and dynamic forwarding (ssh -D) turns the connection into a SOCKS5 proxy.

This article breaks tunneling down with analogies, real-world examples, diagrams and practical commands you can use immediately.

What Is SSH Tunneling?#

A laptop labelled SSH Tunneling connected to an SSH server through a glowing encrypted tunnel with a padlock. From a post by Dhananjay Aggarwal.

SSH tunneling is a technique that creates a secure, encrypted pathway (a tunnel) between your local machine and a remote machine. You can route any kind of network traffic through this tunnel.

You can use SSH tunnels to:

  • Access internal servers from outside the network
  • Get past firewalls
  • Protect sensitive data
  • Connect to remote databases securely
  • Expose local development servers to remote machines
  • Debug services running inside private networks

Simple Analogy to Understand SSH Tunneling#

Two buildings separated by a street full of people, connected underground by a private tunnel carrying a package between two keys. From a post by Dhananjay Aggarwal.

SSH tunneling works like a private underground tunnel between two buildings: whatever you send through it can't be seen by anyone on the busy street above.

Imagine two buildings separated by a busy street full of people. Normally, if you send a package across, anyone can intercept it.

SSH tunneling acts like:

  • A private underground tunnel
  • That only you and your friend have keys to
  • Where anything you send is invisible to everyone outside

This lets you move information safely, even through dangerous or untrusted areas.

What are the types of SSH tunneling?#

Three types of tunnels between a local and a remote computer: local (-L) over a local port, remote (-R) over a remote port, and dynamic (-D) over a local port. From a post by Dhananjay Aggarwal.

SSH supports three types of tunnels:

  1. Local Port Forwarding (ssh -L)
  2. Remote Port Forwarding (ssh -R)
  3. Dynamic Port Forwarding (ssh -D)

Each one suits different real-world scenarios.

What is SSH local port forwarding (-L)?#

Local port forwarding (ssh -L) routes a port on your local machine to a port on a remote server.

Command

Bash
ssh -L 8080:localhost:3000 user@server

Meaning

  • You open localhost:8080 on your laptop
  • It tunnels to the remote server
  • The remote server connects to localhost:3000 on its side

Real life example

You need to reach a database that is only available inside the cloud VPC.

Bash
ssh -L 5432:localhost:5432 ubuntu@aws-server

Now your local machine can connect to the remote PostgreSQL database with:

Bash
psql -h localhost -p 5432

What is SSH remote port forwarding (-R)?#

Remote port forwarding (ssh -R) exposes a port on your local machine to a remote server.

Command

Bash
ssh -R 9000:localhost:8080 user@server

Meaning

  • The server opens localhost:9000
  • Anything hitting that port is forwarded to port 8080 on your local system

Real life example

Your backend team wants to see your local development server.

You run:

Bash
ssh -R 9000:localhost:3000 dev@company-server

Now the team can open:

code
http://server:9000

and view your local React or Node app.

What is SSH dynamic port forwarding (-D)?#

Dynamic port forwarding (ssh -D) turns your SSH connection into a SOCKS5 proxy, so your browser or apps send their traffic through the encrypted tunnel.

This is the coolest one. It turns your SSH connection into a mini VPN for your browser or apps.

Command

Bash
ssh -D 9090 user@server

Meaning

Your system creates a SOCKS5 proxy on port 9090. All traffic passes through the SSH tunnel.

Real life example

You are on public Wi-Fi and need safe browsing or access to restricted websites.

Configure the browser proxy: SOCKS5 → localhost:9090

Now all browsing traffic moves through the encrypted SSH tunnel.

When Do You Use Each Tunnel?#

TunnelUse it for
Local (-L)Reaching remote internal services
Remote (-R)Letting remote systems reach your local service
Dynamic (-D)Browsing securely through an encrypted proxy

Real World DevOps Use Cases#

DevOps teams use SSH tunnels to reach private databases, debug Kubernetes pods, go through bastion hosts, open internal dashboards and protect traffic on public Wi-Fi.

1. Accessing a private database on AWS#

DevOps teams regularly tunnel into RDS, MongoDB, Redis or Elasticsearch nodes.

2. Debugging Kubernetes pods#

Developers forward ports from pods to their laptops.

3. Going through bastion hosts#

SSH tunneling is combined with jump hosts for multi-layer access.

4. Securely reaching internal dashboards#

Grafana, Prometheus, Jenkins and Kibana often expose only private ports.

5. Protecting your traffic on public Wi-Fi#

Dynamic tunnels are widely used as lightweight VPNs.

Common Problems and Solutions#

The SSH tunnel errors you're most likely to hit are a port that's already in use, a refused connection, and a server that doesn't allow remote forwarding.

  • Problem: “Bind: Address already in use”. Cause: the port is already taken by another process. Fix: use a different port or free the one in use.
  • Problem: “Connection refused”. Cause: the remote service is not running or not reachable. Fix: start the service or check network and firewall settings.
  • Problem: “Permission denied for remote forwarding”. Cause: the SSH server does not allow remote port forwarding. Fix: set AllowTcpForwarding yes on the server or ask the admin.

Tunneling Architecture Diagram#

Tunneling architecture: a laptop connects to a server through one tunnel that provides encryption, port forwarding and a SOCKS proxy. From a post by Dhananjay Aggarwal.

Conclusion#

SSH tunneling is one of the most powerful and practical tools in modern engineering. Whether you want to reach private services, debug systems, get around network restrictions or browse securely, SSH tunneling gives you full control with strong encryption and flexibility.

Mastering SSH tunneling is essential if you work with cloud servers, Linux systems, DevOps pipelines or backend infrastructure.

Frequently asked questions#

What is the difference between SSH local and remote port forwarding?

Local forwarding (ssh -L) routes a port on your machine to a port on a remote server, so you can reach remote internal services. Remote forwarding (ssh -R) exposes a port on your machine to a remote server, so remote systems can reach your local service.

How do I connect to a remote database through an SSH tunnel?

Use local port forwarding, for example ssh -L 5432:localhost:5432 ubuntu@aws-server. Your machine can then reach a PostgreSQL database that is only available inside the cloud VPC with psql -h localhost -p 5432.

Can an SSH tunnel work like a VPN?

Yes, for the apps you point at it. ssh -D 9090 user@server creates a SOCKS5 proxy on port 9090, and a browser configured to use localhost:9090 sends all its traffic through the encrypted SSH tunnel.

How do I fix "bind: address already in use" when opening an SSH tunnel?

Another process is already using the local port you asked for. Pick a different port or free the one in use.

Why is SSH remote port forwarding denied?

The SSH server doesn't allow remote port forwarding. Set AllowTcpForwarding yes on the server, or ask its admin to.

Found it useful? Clap for it.
Share
Summarize with AI
Prefer IntervueClub on GoogleShow our posts more often in Top Stories
Dhananjay Aggarwal

Written by Dhananjay Aggarwal

All posts