SSH tunnel config .ssh/config

SSH tunnels can be pre-configured in the client-side SSH configuration file (~/.ssh/config),
allowing automated and seamless port forwarding without manually entering commands.

1. Configuring Local Port Forwarding (-L)

Local port forwarding allows a local port to securely connect to a remote service.

Example: Forwarding Local Port 8080 to a Remote MySQL Server


Host remote-mysql
    HostName remote-server
    User user
    LocalForward 8080 localhost:3306
2. Configuring Remote Port Forwarding (-R)

Remote port forwarding allows a remote machine to connect back to a local service.

Example: Allowing the Remote Server to Access a Local SSH Service


Host remote-ssh-access
    HostName remote-server
    User user
    RemoteForward 9090 localhost:22

3. Configuring Dynamic Port Forwarding (-D)

Dynamic port forwarding creates a SOCKS5 proxy, allowing multiple applications to route traffic securely through the SSH connection.

Example: Creating a SOCKS5 Proxy on Local Port 1080


Host socks-proxy
    HostName remote-server
    User user
    DynamicForward 1080

4. Additional Configuration Options

✔ Keeping the Connection Alive


    ServerAliveInterval 60
    ServerAliveCountMax 2

✔ Automatically Reconnect on Failure

ExitOnForwardFailure no

✔ Simplifying Hostnames for Easier Use


    HostName remote-server.example.com
    User user
    IdentityFile ~/.ssh/id_rsa

Summary

🚀 Now, SSH tunnels are automatically created when connecting to the configured hosts! 🔐