SSH Tunneling Port Forwarding
SSH Tunneling: Sichere Verbindungen
SSH Tunnels verschlüsseln Verbindungen zu entfernten Services. Lernen Sie die verschiedenen Forwarding-Arten und ihre Anwendungen.
Forwarding-Typen
| Typ | Flag | Beschreibung |
|---|---|---|
| Local | -L | Lokaler Port → Remote Service |
| Remote | -R | Remote Port → Lokaler Service |
| Dynamic | -D | SOCKS Proxy |
Local Port Forwarding (-L)
# Syntax ssh -L [local_addr:]local_port:remote_addr:remote_port user@ssh_server # Beispiel: Remote Datenbank lokal erreichbar machen ssh -L 5432:localhost:5432 user@server.example.com # Jetzt erreichbar: localhost:5432 → server:5432 psql -h localhost -p 5432 -U postgres # Remote Service in privatem Netz ssh -L 8080:internal-server:80 user@bastion.example.com # bastion hat Zugriff auf internal-server
┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ Ihr Laptop │ │ SSH Server │ │ DB Server │ │ │ SSH │ (Jump) │ TCP │ │ │ :5432 ◄─────┼────────┼─────────────┼───────►│ :5432 │ │ │ │ │ │ │ └─────────────┘ └─────────────┘ └─────────────┘
Remote Port Forwarding (-R)
# Syntax ssh -R [remote_addr:]remote_port:local_addr:local_port user@ssh_server # Lokalen Webserver von außen erreichbar machen ssh -R 8080:localhost:3000 user@server.example.com # server.example.com:8080 → Ihr localhost:3000 # Webhook-Testing (localhost über Internet erreichbar) ssh -R 80:localhost:3000 user@public-server.com
┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ Ihr Laptop │ SSH │ SSH Server │ │ Internet │ │ │───────►│ │◄───────│ │ │ :3000 ◄─────┼────────┼── :8080 ◄───┼────────│ User │ │ │ │ │ │ │ └─────────────┘ └─────────────┘ └─────────────┘
Dynamic Port Forwarding (-D)
# SOCKS5 Proxy ssh -D 1080 user@server.example.com # Jetzt: localhost:1080 ist SOCKS5 Proxy # Firefox/Browser konfigurieren: # Proxy: SOCKS5 localhost:1080 # curl über Proxy curl --socks5 localhost:1080 https://example.com # Git über Proxy git config --global http.proxy socks5://localhost:1080
Praktische Optionen
# Hintergrund (ohne Terminal)
ssh -f -N -L 5432:localhost:5432 user@server
# -f : In Hintergrund
# -N : Kein Kommando ausführen
# Keep-Alive (Verbindung halten)
ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=3 ...
# Kompression (langsame Verbindungen)
ssh -C -L 5432:localhost:5432 user@server
# Mehrere Tunnels
ssh -L 5432:localhost:5432 \
-L 6379:localhost:6379 \
-L 8080:internal:80 \
user@server
SSH Config
# ~/.ssh/config
# Datenbank-Tunnel
Host db-tunnel
HostName bastion.example.com
User admin
LocalForward 5432 db-server:5432
LocalForward 6379 redis-server:6379
# Jump Host
Host internal-server
HostName 10.0.0.50
User admin
ProxyJump bastion.example.com
# SOCKS Proxy
Host proxy
HostName server.example.com
User admin
DynamicForward 1080
# Verwendung:
ssh db-tunnel # Startet Tunnel
ssh internal-server # Springt über Bastion
ssh proxy # Startet SOCKS Proxy
Jump Host / ProxyJump
# Über Bastion auf internen Server
ssh -J bastion.example.com internal-server
# Mehrere Jumps
ssh -J jump1.com,jump2.com target-server
# In ~/.ssh/config
Host internal-*
ProxyJump bastion.example.com
Host internal-db
HostName 10.0.0.100
User dbadmin
# Dann einfach:
ssh internal-db
Tunnel als Service (autossh)
# autossh hält Verbindung aufrecht sudo apt install autossh # Tunnel starten (reconnects automatisch) autossh -M 0 -f -N -L 5432:localhost:5432 user@server # Als Systemd Service # /etc/systemd/system/db-tunnel.service [Unit] Description=SSH Tunnel to Database After=network.target [Service] User=tunnel ExecStart=/usr/bin/autossh -M 0 -N -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -L 5432:localhost:5432 user@server Restart=always RestartSec=10 [Install] WantedBy=multi-user.target # Aktivieren sudo systemctl enable db-tunnel sudo systemctl start db-tunnel
Sicherheit
# Auf SSH Server: Remote Forwarding einschränken
# /etc/ssh/sshd_config
AllowTcpForwarding local # Nur local forwarding
GatewayPorts no # Keine Remote-Bindings
PermitTunnel no # Kein VPN-Tunnel
# Für Remote Forwarding an alle Interfaces:
GatewayPorts yes # Vorsicht!
# Oder:
GatewayPorts clientspecified # Client entscheidet
# Benutzer-spezifisch
Match User tunnel-only
AllowTcpForwarding yes
PermitOpen db-server:5432
ForceCommand /bin/false
💡 Tipp:
SSH Tunnels sind ideal um sichere Verbindungen zu Datenbanken und internen Services aufzubauen, ohne VPN einrichten zu müssen.