هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
Step by step
- **Read the
-Lvalue:**LOCALPORT:TARGET:TARGETPORT. The target is resolved on the SSH server's side —db1.internalmust be reachable frombastion, not from your laptop. If the database runs on the SSH server itself, the target is127.0.0.1. - 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).
- Several databases at once:
ssh -N -L 15432:db1.internal:5432 -L 13306:mysql1.internal:3306 -L 16379:redis1.internal:6379 bastion- Permanent tunnel for a tool — put it in the SSH config:
Host db-tunnel
HostName bastion.example.com
User alice
LocalForward 15432 db1.internal:5432
ExitOnForwardFailure yesssh -N db-tunnel- 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- Security on the SSH server: forwarding is on by default. Limit what users may forward, per group:
Match Group dbadmins
AllowTcpForwarding local
PermitOpen db1.internal:5432 mysql1.internal:3306The 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).
Check that it worked
Common problems
| Problem | Cause | Fix |
|---|---|---|
bind [127.0.0.1]:15432: Address already in use | Local port taken (old tunnel or local DB). | Another local port, or stop the old tunnel. |
channel 2: open failed: connect failed: Connection refused | The 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 failed | The server forbids forwarding or that target. | AllowTcpForwarding/PermitOpen on the server (ask the admin). |
| Tunnel dies after idle time | NAT/firewall timeout. | ServerAliveInterval 60; autossh for long-running tunnels. |
| Database refuses the login | pg_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 network | Bound to 0.0.0.0. | Use the default 127.0.0.1 bind. |
Go further
- Command pages: ssh, psql, ss.
- Next how-tos: Use a jump host with SSH, Port-forward to a Kubernetes service, Connect to EC2 without SSH keys.
- Learn it: TLCL — SSH tunnels. Full guide: OpenSSH.
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.