Docs / Linux Basics / Setting Up SSH Tunnels and Port Forwarding

Setting Up SSH Tunnels and Port Forwarding

By Admin · Feb 25, 2026 · Updated Apr 23, 2026 · 30 views · 1 min read

Local Port Forwarding

Forward a port on your local machine to a port on the remote server (or a machine accessible from the remote server).

# Access remote MySQL (3306) via local port 3307
ssh -L 3307:localhost:3306 user@server

# Then connect locally
mysql -h 127.0.0.1 -P 3307 -u root

Remote Port Forwarding

Make a local service accessible from the remote server:

# Expose local port 3000 on the server port 8080
ssh -R 8080:localhost:3000 user@server

Dynamic SOCKS Proxy

# Create a SOCKS5 proxy on local port 1080
ssh -D 1080 user@server

# Configure your browser to use SOCKS5 proxy at localhost:1080
# All traffic routes through the server

Persistent Tunnels

# Keep tunnel alive with autossh
sudo apt install -y autossh
autossh -M 0 -f -N -L 3307:localhost:3306 user@server

SSH Config for Tunnels

# ~/.ssh/config
Host db-tunnel
    HostName server-ip
    User deploy
    LocalForward 3307 localhost:3306
    ServerAliveInterval 60

# Then just: ssh -N db-tunnel

Was this article helpful?