When managing remote servers, we often need to access multiple admin interfaces (MongoDB, RabbitMQ, etc.) that are bound to localhost on the remote machine. Standard port forwarding requires separate tunnels for each service, which becomes cumbersome.
Instead of creating multiple -L
forwards, use SSH's dynamic port forwarding feature:
ssh user@remote.server -D 4321
This creates a SOCKS proxy on your local machine that routes all traffic through the SSH connection.
Configure your browser to use the SOCKS proxy:
- Firefox: Preferences > Network Settings > Manual proxy configuration
- Chrome: Launch with
--proxy-server="socks5://localhost:4321"
With the proxy active, simply access services as if you were on the remote server:
http://localhost:28017
for MongoDBhttp://localhost:15672
for RabbitMQ
For persistent connections, consider these options:
ssh -N -f -D 4321 user@remote.server
Where:
-N
: Don't execute remote commands-f
: Fork into background
Remember that this makes all browser traffic go through the remote server. For better security:
ssh -D localhost:4321 -C user@remote.server
The -C
flag enables compression, while binding to localhost prevents external access to your proxy.
If you specifically need named access, you can edit your /etc/hosts
file:
127.0.0.1 my.tunnel.name
Then combine with port forwarding:
ssh -L 28017:localhost:28017 -L 15672:localhost:15672 user@remote.server
Now you can access services via http://my.tunnel.name:28017
.
For regular use, add this to your ~/.ssh/config
:
Host remote-admin
HostName remote.server
User user
DynamicForward 4321
ServerAliveInterval 60
Then simply connect with ssh remote-admin
.
When working with remote servers, we often encounter services (like MongoDB, RabbitMQ, or admin panels) that only listen on localhost. The standard SSH port forwarding approach requires specifying each port individually:
ssh user@remote.server -L 8080:localhost:80 -L 8081:localhost:8080
This becomes impractical when managing multiple services or when ports aren't known in advance.
The -D
flag in SSH creates a SOCKS proxy that handles all port routing dynamically:
ssh -N -D 1080 user@remote.server
Key advantages:
- No need to specify individual ports upfront
- All localhost-bound services become accessible
- Works with any application supporting SOCKS proxies
For web-based admin interfaces, configure your browser to use the SOCKS proxy:
Firefox: Preferences > Network Settings > Manual proxy configuration
SOCKS Host: localhost
Port: 1080
Check "Proxy DNS when using SOCKS v5"
Now any URL like http://localhost:28017
in your browser will automatically route through the SSH tunnel.
For production environments, consider these enhancements:
# Persistent connection with autossh
autossh -M 0 -f -N -D 1080 user@remote.server
# Multi-hop tunneling
ssh -At user@jump.server ssh -At -D 1080 user@target.server
- Use SSH key authentication instead of passwords
- Consider binding the SOCKS proxy to 127.0.0.1 only
- For sensitive services, add
-C
for compression and encryption
If connections fail:
# Verify SOCKS proxy with curl
curl --socks5 localhost:1080 http://localhost:28017
# Check SSH debug output
ssh -vvv -D 1080 user@remote.server