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
- Confirm you can log in normally:
ssh deploy@203.0.113.10. - Pick the direction:
-Lto reach something on the server,-Rto expose something on your machine to the server,-Dfor a SOCKS proxy. - Use a free local port above 1024, such as
13306, so you do not need admin rights or clash with a local service. - Add
-Nso no remote shell is started, and-fif you want it in the background. - Test with the real client (for example
mysql -h 127.0.0.1 -P 13306), not just by checking that the port is open. - Move tunnels you use often into
~/.ssh/configwithLocalForwardandServerAliveInterval. - On the server, keep
GatewayPortsat its default ofnoand restrictAllowTcpForwardingfor 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:
| Setting | Default | What it controls |
|---|---|---|
AllowTcpForwarding | yes | Whether users may forward TCP at all; also takes no, local or remote |
GatewayPorts | no | Whether remote forwards (-R) can listen on public addresses, not only loopback; also yes or clientspecified |
PermitOpen | any | Which destinations -L forwards may reach |
PermitListen | any | Which ports -R forwards may listen on |
DisableForwarding | no | Turns 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?
| Option | Best for | Trade-off |
|---|---|---|
| SSH tunnel | One person reaching a database or admin page now and then | Per-user and per-port; ends when the session ends |
| Open firewall port | Public services such as HTTPS | Exposes the service to scans; avoid for databases |
| VPN (WireGuard, OpenVPN) | Teams or many services, always on | More 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 )
- OpenBSD manual pages: ssh(1): OpenSSH remote login client
- OpenBSD manual pages: ssh_config(5): OpenSSH client configuration
- OpenBSD manual pages: sshd_config(5): OpenSSH daemon configuration
- PuTTY documentation: The Tunnels panel
- PowerShell team on GitHub: Win32-OpenSSH wiki