> For the complete documentation index, see [llms.txt](https://hacking-3.gitbook.io/barre/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-3.gitbook.io/barre/apuntes/general/redes/protocolos/ssh/port-forwarding.md).

# Port-forwarding

Port forwarding, also known as port mapping, is a network configuration technique that allows external devices to access services running on a private network.

***

## **Local SSH Tunnel**

This tunnel allows a local port on your machine to listen for connections and redirect those connections to a remote address and port on the server.

### **Code**

```bash
ssh -L 8080:localhost:80 user@host
```

### **Code explanation**

* `-L`: Stands for "local port forwarding".
* `8080`: This is the port on your local machine that will listen for incoming connections.
* `localhost:80`: This is the address and port on the remote server to which those connections will be redirected.

### **What this does**&#x20;

When someone connects to port 8080 on your local machine, the connection will be redirected to port 80 on the remote server (the one you’re SSHing into).

{% hint style="info" %}
Suppose you want to access a web server running on port 80 of a remote machine, but you can only access it locally. With this command, you can connect to port 8080 on your local machine and access the remote server’s port 80 through the SSH tunnel.
{% endhint %}

***

## Reverse SSH tunnel

This tunnel allows a port on the remote server to listen for connections and redirect those connections to a local address and port on your machine.

### Code

```bash
ssh -R 9000:localhost:80 user@host
```

### Code explication

`-R`: Stands for "remote port forwarding".

`9000`: This is the port on the remote server (host) that will listen for incoming connections.

`localhost:80`: This is the address and port on your local machine to which those connections will be redirected.

### What this do

When someone connects to port 9000 on the remote server (host), the connection will be redirected to localhost:80 on your local machine (the one running the command).

{% hint style="info" %}
Suppose your IP is 10.10.10.10 and you can't expose port 80, which is the port your web server is running on, because of a firewall. However, you can expose port 9000. With this command, anyone accessing port 9000 on your machine (where the server is located) will actually be accessing port 80 through the SSH tunnel.
{% endhint %}
