Talk to the NOC, IST hours — +91 75994 50220 support@bigdomainhost.com
Guide

SSH tunnels explained: what an SSH tunnel is and how to create one

An SSH tunnel lets you reach a service on your server, such as a database bound to 127.0.0.1, without opening a single extra firewall port. This guide covers local, remote and dynamic forwarding with exact commands, plus the server settings that keep forwarding safe.

Short answer: An SSH tunnel is an encrypted channel inside an SSH connection that carries other traffic. With local forwarding, ssh -N -L 13306:127.0.0.1:3306 user@server lets your laptop reach a database that listens only on the server. Remote forwarding (-R) exposes a local port to the server, and dynamic forwarding (-D) gives you a SOCKS proxy.

What is an SSH tunnel?

An SSH tunnel is an encrypted path for other network traffic, carried inside an ordinary SSH connection. You log in to a server as usual, and the SSH client also listens on a port you choose. Anything sent to that port travels through the encrypted session and comes out on the other side, where it is delivered to a host and port you named. The official term is SSH port forwarding, and OpenSSH offers three kinds: local (-L), remote (-R) and dynamic (-D).

The classic use is a database on a VPS that listens only on 127.0.0.1. It is not reachable from the internet, which is how it should be, yet with one command you can open it from your laptop as if it were running locally. No firewall port is opened and nothing new is exposed. Use tunnels only on servers you own or are authorised to manage.

SSH port forwarding: quick checklist

  1. Confirm you can log in normally: ssh deploy@203.0.113.10.
  2. Pick the direction: -L to reach something on the server, -R to expose something on your machine to the server, -D for a SOCKS proxy.
  3. Use a free local port above 1024, such as 13306, so you do not need admin rights or clash with a local service.
  4. Add -N so no remote shell is started, and -f if you want it in the background.
  5. Test with the real client (for example mysql -h 127.0.0.1 -P 13306), not just by checking that the port is open.
  6. Move tunnels you use often into ~/.ssh/config with LocalForward and ServerAliveInterval.
  7. On the server, keep GatewayPorts at its default of no and restrict AllowTcpForwarding for accounts that do not need it.

How to create an SSH tunnel with local port forwarding (-L)

SSH local port forwarding is the one you will use most. The syntax is -L [bind_address:]port:host:hostport. The first port is on your computer; host:hostport is the destination, resolved from the server's point of view. So 127.0.0.1 on the right-hand side means the server itself, not your laptop.

Reach MySQL or MariaDB bound to 127.0.0.1

ssh -N -L 13306:127.0.0.1:3306 deploy@203.0.113.10

Leave that running, and in a second terminal connect to the local end:

mysql -h 127.0.0.1 -P 13306 -u appuser -p

Use 127.0.0.1 rather than localhost here: the MySQL client treats localhost as a request for the local Unix socket and ignores the port. To MySQL on the server, the connection arrives from 127.0.0.1, so the database user must be allowed from localhost or 127.0.0.1, not from your home IP.

Reach PostgreSQL the same way

ssh -N -L 15432:127.0.0.1:5432 deploy@203.0.113.10
psql -h 127.0.0.1 -p 15432 -U appuser appdb

PostgreSQL sees a TCP connection from 127.0.0.1, so the matching host line in pg_hba.conf applies, not the local (socket) line.

Run it in the background with -N and -f

-N tells ssh not to run a remote command, which is what you want for a tunnel-only session. -f sends ssh to the background after you have authenticated. Add -o ExitOnForwardFailure=yes so ssh quits with an error if the port cannot be bound, rather than going to the background with no working tunnel:

ssh -f -N -o ExitOnForwardFailure=yes -L 13306:127.0.0.1:3306 deploy@203.0.113.10

To stop it, find the process with pgrep -af "ssh -f -N" and end it with kill.

The local port is bound to loopback by default, so only your own computer can use it. The client option -g (or GatewayPorts yes in ssh_config) would let other machines on your network connect to it too; leave it off unless you mean to share the tunnel.

Remote forwarding: the SSH reverse tunnel (-R)

An SSH reverse tunnel works the other way round. The server listens on a port, and connections to it are carried back to your machine and delivered to a destination you can reach from there. A typical case is showing a web app running on your laptop to the server, or reaching a machine behind NAT that can make outbound connections but accept none:

ssh -N -R 8080:127.0.0.1:3000 deploy@203.0.113.10

Now curl http://127.0.0.1:8080 run on the server reaches port 3000 on your laptop. By default sshd binds remote forwards to the server's loopback address only, so the internet cannot see port 8080.

Dynamic forwarding: a SOCKS proxy (-D)

With -D, ssh runs a SOCKS 4 and SOCKS 5 server on your machine. Each program that uses it can ask for any destination, and the connection is opened from the server:

ssh -N -D 1080 deploy@203.0.113.10

Set a browser's SOCKS5 proxy to 127.0.0.1 port 1080 and its traffic leaves through the server. Only TCP is carried, not UDP. Respect the terms of the networks you connect through.

Save tunnels in ~/.ssh/config

Put tunnels you use often in your client config:

Host db-tunnel
    HostName 203.0.113.10
    User deploy
    LocalForward 13306 127.0.0.1:3306
    LocalForward 15432 127.0.0.1:5432
    ServerAliveInterval 30
    ServerAliveCountMax 3
    ExitOnForwardFailure yes

Then ssh -N db-tunnel opens both forwards. RemoteForward and DynamicForward work the same way. ServerAliveInterval 30 makes the client send a check through the encrypted channel after 30 idle seconds; after three unanswered checks it disconnects, rather than leaving a silently dead tunnel.

Keep a tunnel up with autossh

For a tunnel that should restart itself after a network drop, autossh (in the Ubuntu and Debian repositories) wraps ssh and relaunches it. A common pattern is autossh -M 0 -N db-tunnel, where -M 0 turns off autossh's own monitoring port and relies on the ServerAlive settings to detect a dead link.

Server side: AllowTcpForwarding and GatewayPorts

Forwarding is controlled in /etc/ssh/sshd_config, or better a drop-in under /etc/ssh/sshd_config.d/. The defaults in OpenSSH are permissive for forwarding but safe for exposure:

SettingDefaultWhat it controls
AllowTcpForwardingyesWhether users may forward TCP at all; also takes no, local or remote
GatewayPortsnoWhether remote forwards (-R) can listen on public addresses, not only loopback; also yes or clientspecified
PermitOpenanyWhich destinations -L forwards may reach
PermitListenanyWhich ports -R forwards may listen on
DisableForwardingnoTurns off all forwarding: TCP, agent, X11 and Unix sockets

The security point is simple. Anyone who can log in can, by default, forward ports, so an account meant only for SFTP or backups can also be used to reach services on 127.0.0.1 or your private network. Turn forwarding off for those accounts with a Match block. Put it at the very end of /etc/ssh/sshd_config, because every line after a Match line belongs to that block until the next Match or the end of the file:

Match User sftpuser
    AllowTcpForwarding no
    PermitTTY no
    ForceCommand internal-sftp

The OpenSSH manual notes that turning off TCP forwarding does not improve security on its own if the user still has a shell, because they can run their own forwarding tools. That is why the example also forces SFTP only; do not add ForceCommand internal-sftp to an account that needs rsync or a shell.

Setting GatewayPorts yes publishes every reverse tunnel on the server's public IP, often by accident. Leave it at no; if you truly need one public forward, clientspecified with PermitListen limits the damage. Run sudo sshd -t before restarting SSH, and keep your current session open, as the Ubuntu VPS initial setup guide explains.

SSH tunnels on Windows

Windows 10 (version 1809 and later) and Windows 11 include the OpenSSH client as an optional feature that is usually already installed; if ssh is not found, add it under Settings, Optional features. Once it is installed, the same ssh -N -L ... commands work in PowerShell or Windows Terminal, and the config file lives at %USERPROFILE%\.ssh\config. In PuTTY, open Connection > SSH > Tunnels, enter the source port (for example 13306) and destination (127.0.0.1:3306), choose Local, Remote or Dynamic, click Add, then save the session before you open it. Leave "Local ports accept connections from other hosts" unticked.

SSH tunnel, open firewall port or VPN?

OptionBest forTrade-off
SSH tunnelOne person reaching a database or admin page now and thenPer-user and per-port; ends when the session ends
Open firewall portPublic services such as HTTPSExposes the service to scans; avoid for databases
VPN (WireGuard, OpenVPN)Teams or many services, always onMore setup; carries UDP and whole subnets

If a database must be opened to the internet, restrict it to known IPs and require TLS. For occasional admin access, a tunnel is simpler and exposes nothing new. If the tunnel itself will not connect, work through SSH connection refused: causes and fixes first, since every forward depends on a working SSH login.

Using SSH tunnels on a Bigdomainhost VPS

A Bigdomainhost KVM VPS comes with full root access and Ubuntu, Debian, AlmaLinux and other Linux images, in Mumbai (BOM-1) or Delhi NCR (DEL-1). The per-instance firewall described in the networking and firewall docs denies inbound traffic except SSH on port 22 by default, so an SSH tunnel to a database on 127.0.0.1 works without opening any other port. Each server also has a private interface, and private traffic between your own servers in the same region stays on the internal network, so you can keep databases off public addresses. If an SSH change locks you out, the out-of-band console in the control panel still gets you in. For help, call +91 75994 50220 during IST hours, WhatsApp the same number at any time, or email support@bigdomainhost.com.

Sources (checked )

Answers

Frequently asked questions

Still unsure? Call +91 75994 50220 or write to support@bigdomainhost.com — a human replies in IST hours.

What is an SSH tunnel used for?

An SSH tunnel carries other traffic through an encrypted SSH session. The most common use is reaching a service that listens only on the server, such as MySQL or PostgreSQL on 127.0.0.1, from your own computer. It is also used to expose a local development server to a remote machine, or to browse through a server with a SOCKS proxy.

How do I create an SSH tunnel to MySQL?

Run ssh -N -L 13306:127.0.0.1:3306 user@server and leave it open. Then connect your client to host 127.0.0.1 on port 13306, not localhost, because the MySQL client treats localhost as the Unix socket. The database sees the connection coming from 127.0.0.1 on the server, so the MySQL user must be allowed from localhost or 127.0.0.1, not from your home IP.

What is an SSH reverse tunnel?

An SSH reverse tunnel uses remote forwarding, the -R option. The server listens on a port and sends connections back through the SSH session to a host and port reachable from your machine. By default the server binds that port to loopback only, so it is not public unless GatewayPorts is changed in sshd_config.

Is an SSH tunnel the same as a VPN?

No. An SSH tunnel forwards individual TCP ports, or acts as a SOCKS proxy, for one SSH session, and it carries no UDP. A VPN such as WireGuard connects whole networks, carries any IP traffic and usually stays up permanently. A tunnel suits occasional admin access; a VPN suits teams or many services.

Why does my SSH tunnel keep disconnecting?

Idle connections are often dropped by home routers or mobile networks. Add ServerAliveInterval 30 and ServerAliveCountMax 3 to the host entry in ~/.ssh/config so the client sends regular checks and closes a dead session cleanly. For a tunnel that must restart by itself, run it under autossh with -M 0 and those ServerAlive settings.

Support that picks up the phone.

In IST hours, in Hindi or English, and WhatsApp replies fastest at any time. Sales, migration and emergencies all reach the same engineers. No offshore queue, no 48-hour first reply.

Need cPanel or WHM with the ports already sorted?

Web & cPanel hosting comes with cPanel ready to log in to, reseller hosting adds white-label WHM, and a KVM VPS can have cPanel & WHM pre-installed. Call +91 75994 50220 or email support@bigdomainhost.com.