Accessing a Private Server via SSH Tunneling
How to access a private server behind a firewall using SSH tunneling.
Create an SSH Tunnel from Your Machine
To access a private server (private-server) inside a remote network, establish an SSH tunnel through a publicly accessible server (gateway-server).
ssh -L 2222:192.168.1.100:22 user@gateway-serverssh -L 2222:192.168.1.100:22 user@gateway-server-L 2222:192.168.1.100:22→ Forwards local port2222toprivate-server:22.user@gateway-server→ The intermediate server with public access.
Note: Replace 192.168.1.100 with the actual private server IP inside the remote network.
Open a New Terminal and Connect to the Private Server
ssh -p 2222 user@localhostssh -p 2222 user@localhostNow, you're securely connected to private-server!
Run the Tunnel in the Background
To keep the tunnel running in the background, use:
ssh -f -N -L 2222:192.168.1.100:22 user@gateway-serverssh -f -N -L 2222:192.168.1.100:22 user@gateway-server-f→ Run in background.-N→ Do not execute remote commands, only forward ports.
Automate the Tunnel with SSH Config
Add the following to ~/.ssh/config on your local machine for quick access:
Host private-tunnel
HostName gateway-server
LocalForward 2222 192.168.1.100:22Host private-tunnel
HostName gateway-server
LocalForward 2222 192.168.1.100:22Now, simply run:
ssh private-tunnel
ssh -p 2222 user@localhostssh private-tunnel
ssh -p 2222 user@localhostThis setup allows seamless access to a private server behind a firewall using SSH tunneling!
