Introduction
In some network environments, a local machine may not have direct access to a remote server, while a jumpbox has access to both the local network and the remote server.
SSH local port forwarding can be used in this situation. It creates a secure tunnel from a port on the local machine through the jumpbox to a specific port on the remote server.
This allows you to connect to the remote service using the local machine’s loopback address, such as 127.0.0.1.
Prerequisites
Before setting up SSH port forwarding, make sure:
- SSH access is available to the jumpbox.
- The jumpbox can connect to the destination server.
- The required destination port is accessible from the jumpbox.
- You have valid SSH credentials for the jumpbox.
- The selected local port is not already being used.
Implementation
Step 1: Understand the SSH Port Forwarding Command
Use the following SSH command:
ssh -L <SOURCEPORT>:<IPADDRESS>:<DESTINATIONPORT> userid@<JUMPBOXIPADDRESS>
Where:
- SOURCEPORT – Port opened on your local machine.
- IPADDRESS – IP address of the remote destination server.
- DESTINATIONPORT – Port of the service running on the remote server.
- JUMPBOXIPADDRESS – IP address of the jumpbox that can access the remote server.
- userid – SSH username used to connect to the jumpbox.
Once the SSH session is established, connections to the local source port are forwarded through the SSH tunnel to the destination server.
Step 2: Configure RDP Port Forwarding
For example, suppose you need to access the RDP service on a remote Windows server, but your local machine cannot directly reach the server.
RDP normally uses port 3389.
Run the following command from your local machine:
ssh -L 3389:<IPADDRESS>:3389 root@<JUMPBOXIP>
This creates the following connection path:
Local Machine
127.0.0.1:3389
|
| SSH Tunnel
v
Jumpbox
|
| Port 3389
v
Remote Server
<IPADDRESS>:3389
Step 3: Access the Remote RDP Service
After the SSH tunnel is established, open your RDP client and connect to:
127.0.0.1
Port:
3389
The RDP connection from your local machine will be forwarded through the jumpbox to the remote server.
Step 4: Keep the SSH Tunnel Active
The SSH session must remain active while the port-forwarding tunnel is being used.
If you want SSH to establish the tunnel without opening an interactive shell, you can use:
ssh -N -L 3389:<IPADDRESS>:3389 root@<JUMPBOXIP>
The -N option tells SSH not to execute a remote command and is useful when SSH is being used only for port forwarding.
Security Note: Use SSH keys or other secure authentication methods where possible, and avoid using privileged accounts such as
rootfor routine access when a restricted account is sufficient.
Conclusion
SSH local port forwarding is useful when your local machine cannot directly access a remote service but a jumpbox can reach it.
By creating an SSH tunnel with the -L option, you can expose the remote service through a local port and access it using 127.0.0.1. This approach can be useful for RDP, database services, web applications, and other TCP-based services that are reachable from the jumpbox.
FAQs
1. What is SSH local port forwarding?
SSH local port forwarding creates a tunnel from a port on your local machine through an SSH server to a destination server and port.
2. Why use a jumpbox for port forwarding?
A jumpbox can act as an intermediary when your local machine cannot directly reach the destination server but the jumpbox has network access to it.
3. Can I use SSH port forwarding for RDP?
Yes. RDP traffic can be forwarded through SSH using port 3389, provided the jumpbox can reach the remote Windows server.
4. Why do I connect to 127.0.0.1?
The local port is opened by the SSH client on your machine. Connecting to 127.0.0.1 sends the traffic through the SSH tunnel to the configured destination.
5. Does the SSH connection need to remain active?
Yes. The SSH tunnel normally remains available only while the SSH connection providing the forwarding is active.
Related Articles
- How to Reset SSH Port to Default Port Through WHM – Learn how to restore the default SSH port configuration through WHM when managing a server.
Read the article
Talk to our experts
Looking for the right technology solution for your business? Our team of experts can help you with development, cloud, DevOps, design, and a wide range of other technology needs. Get in touch with our team here.