How to Set Up a Reverse SSH Tunnel to Access Your Local Machine from a Remote Machine

How to Set Up a Reverse SSH Tunnel to Access Your Local Machine from a Remote Machine

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

  • Superuser (root or sudo) access on the local machine.
  • SSH enabled and configured on both the local machine and the remote server.
  • Access to a remote server with a public IP address.

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:

  • -R 2222:localhost:22: Specifies the reverse port forwarding. Port 2222 on the remote server will be forwarded to port 22 on your local machine (which is the SSH port). You can replace port 2222 with any other port of your choice.
  • user@remote_host: Replace user with your username on the remote server and remote_host with the IP address or domain name of the remote server.

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:

  • -p 2222: Specifies the port being used on the remote server for the forwarding (in this case, 2222).
  • user: The username on your local machine (not the remote server).
  • localhost: Refers to the remote server itself, as the connection is forwarded through port 2222 to your local machine.

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

  • Working in environments protected by firewalls: If you are in a network that does not allow inbound connections, a reverse SSH tunnel enables an external server to access your machine.
  • Remote support: Technicians can use reverse SSH tunnels to access clients' local machines without needing to configure routing or firewalls on the client side.
  • Development and debugging: In development environments, you can access services running on your local machine from external servers, facilitating network testing.

Security Considerations

While the reverse SSH tunnel is a powerful tool, it’s important to configure it carefully to avoid security risks:

  • SSH key authentication: Use public key authentication instead of passwords to add an extra layer of security.
  • Firewall: Ensure that only authorized IPs can access the forwarded ports on the remote server.
  • Monitoring: Keep logs of connections and monitor suspicious activities on both the remote server and the local machine.

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.


要查看或添加评论,请登录

Fábio Berbert de Paula的更多文章

社区洞察

其他会员也浏览了