How to Set Up a Reverse SSH Tunnel to Access Your Local Machine from a Remote Machine
Fábio Berbert de Paula
Full Stack Developer, focused on React, React Native, JavaScript, Python. Specialist in GNU/Linux and Amazon AWS! System integration with Artificial Intelligence. IG @alexabolada
SSH (Secure Shell) enables secure connections between machines on local and remote networks. One of its lesser-known but extremely useful features is the reverse SSH tunnel, which allows a remote machine to access your local machine, even if it is behind a firewall or NAT router.
What is a Reverse SSH Tunnel?
Normally, SSH is used to connect to a remote server from a local machine. A reverse SSH tunnel inverts this logic: you connect to a remote server and create a tunnel back to your local machine, making it accessible through a specified port on the remote server.
This method is particularly useful in scenarios where the local machine is protected by a firewall or NAT, making it inaccessible directly via a public IP address.
Prerequisites
In my case, I have a remote server set up on Amazon Web Services (AWS) with a public IP address. My local computer is not directly accessible over the internet, as it is protected by my ISP's firewall. Whenever I travel or am away from home and need to access my local computer, I use a reverse SSH tunnel to connect to it from the remote server.
Every time I leave, I open the reverse SSH tunnel to ensure that I can access my local machine remotely.
Step 1: Enable Port Forwarding in SSH
The first step is to enable port forwarding on your local machine. This allows the reverse SSH tunnel to create a connection that will be opened on the remote server but will redirect traffic to your local machine.
On the Local Machine:
1. Open the SSH configuration file:
To configure SSH and allow reverse tunneling, edit the SSH server configuration file on your local machine:
sudo vim /etc/ssh/sshd_config
2. Enable port forwarding:
Add or uncomment the following line in the file:
GatewayPorts yes
This allows SSH to accept external connections on the forwarded ports.
3. Restart the SSH service:
After modifying the configuration file, you need to restart the SSH service for the changes to take effect:
sudo service ssh restart
Step 2: Open the Reverse SSH Tunnel
Now, we need to open the reverse SSH tunnel by connecting to the remote server and specifying the port that will be used to forward traffic back to your local machine.
On the Local Machine:
1. Connect to the remote server with port forwarding:
Run the following command from your local machine to initiate the reverse SSH session:
领英推荐
ssh -R 2222:localhost:22 user@remote_host
In this command:
You have now opened a reverse SSH session with the remote server. Keep this session open to maintain the tunnel.
Now, any connection made to the remote server on port 2222 will be forwarded to port 22 on your local machine.
Step 3: Access the Local Machine from the Remote Machine
After setting up the reverse SSH tunnel, anyone with access to the remote server can access your local machine using the port forwarding you configured.
On the Remote Machine:
1. Open a terminal on the remote machine (the remote server where you set up the tunnel).
2. Connect to your local machine via SSH:
Run the following command on the remote machine:
ssh -p 2222 user@localhost
Here:
You are now connected to your local machine from the remote server.
How Does Reverse SSH Work?
The reverse SSH tunnel redirects traffic between two machines, allowing you to connect to a local machine from a remote machine. In the example above, when you connect to the remote server with port forwarding configured, the remote server creates a "bridge" that forwards all connections made on port 2222 to port 22 (SSH) on your local machine.
Use Cases
Security Considerations
While the reverse SSH tunnel is a powerful tool, it’s important to configure it carefully to avoid security risks:
Conclusion
Creating a reverse SSH tunnel is an effective way to access a local machine that is behind firewalls or NAT routers. With some adjustments to the SSH configuration, you can make your local machine accessible from a remote server and enable secure access for those who need it. Always ensure that you follow best security practices when using these techniques, especially when exposing your machine to external networks.
Now that you understand how reverse SSH works, you can use it in various situations to facilitate remote access and solve connectivity issues.