Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 114Network · SSH

How to open an SSH tunnel to a database

The database listens only on a private network (as it should), but you need it for a quick query, a migration check or a GUI tool on your laptop. An SSH tunnel (local port forwarding) gives you a private, encrypted path through a server you can already SSH to — no database port opened anywhere. This how-to opens the tunnel, runs it in the background, and covers reverse tunnels and the security settings.

  • 5 Lessen
  • 6 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

114.1

The quick answer

ssh -N -L 15432:db1.internal:5432 bastion
psql -h 127.0.0.1 -p 15432 -U shop shop

-L 15432:db1.internal:5432 = local port 15432 → (through bastion) → db1.internal:5432. -N = no shell, only the tunnel. Stop it with Ctrl+C.

114.2

Step by step

  1. **Read the -L value:** LOCALPORT:TARGET:TARGETPORT. The target is resolved on the SSH server's side — db1.internal must be reachable from bastion, not from your laptop. If the database runs on the SSH server itself, the target is 127.0.0.1.
  2. Run it in the background and stop it cleanly:
ssh -f -N -o ExitOnForwardFailure=yes -L 15432:db1.internal:5432 bastion
ss -ltn | grep 15432
LISTEN 0 128 127.0.0.1:15432 0.0.0.0:*
pkill -f 'L 15432:db1.internal:5432'

ExitOnForwardFailure=yes = fail at once if the port can not be opened (instead of a tunnel that does nothing).

  1. Several databases at once:
ssh -N -L 15432:db1.internal:5432 -L 13306:mysql1.internal:3306 -L 16379:redis1.internal:6379 bastion
  1. Permanent tunnel for a tool — put it in the SSH config:
~/.ssh/config (part)
Host db-tunnel
    HostName bastion.example.com
    User alice
    LocalForward 15432 db1.internal:5432
    ExitOnForwardFailure yes
ssh -N db-tunnel
  1. Reverse tunnel (-R) — let a server reach something on your side (for example a webhook test). Rarely needed; many companies forbid it:
ssh -N -R 18080:127.0.0.1:8080 web1
  1. Security on the SSH server: forwarding is on by default. Limit what users may forward, per group:
/etc/ssh/sshd_config.d/20-forwarding.conf
Match Group dbadmins
    AllowTcpForwarding local
    PermitOpen db1.internal:5432 mysql1.internal:3306

The local end listens on 127.0.0.1 only — keep it that way (do not use GatewayPorts or 0.0.0.0: unless you mean it).

114.3

Check that it worked

ss -ltn | grep 15432
psql -h 127.0.0.1 -p 15432 -U shop -d shop -c 'SELECT 1;'

The local port is listening on 127.0.0.1, and the client reaches the database.

114.4

Common problems

ProblemCauseFix
bind [127.0.0.1]:15432: Address already in useLocal port taken (old tunnel or local DB).Another local port, or stop the old tunnel.
channel 2: open failed: connect failed: Connection refusedThe target does not listen, or wrong host/port from the server's view.Test from the bastion: nc -zv db1.internal 5432.
administratively prohibited: open failedThe server forbids forwarding or that target.AllowTcpForwarding/PermitOpen on the server (ask the admin).
Tunnel dies after idle timeNAT/firewall timeout.ServerAliveInterval 60; autossh for long-running tunnels.
Database refuses the loginpg_hba.conf sees the bastion's IP, not yours.Allow the bastion's IP for that user in the DB.
The tunnel is open to others in your networkBound to 0.0.0.0.Use the default 127.0.0.1 bind.

Commando's in deze les

114.5

Go further

1In ssh -L 15432:db1.internal:5432 bastion, which machine must reach db1.internal?

The SSH server (bastion) — the target is resolved and connected on its side.

2What does ExitOnForwardFailure=yes prevent?

A tunnel that starts without its port forward working.

Alle commando's in dit hoofdstuk