> For the complete documentation index, see [llms.txt](https://rosemary-2.gitbook.io/rosemary-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://rosemary-2.gitbook.io/rosemary-docs/tutorials/local-port-forwarding.md). Agents can also search and read this documentation through its [MCP server](https://rosemary-2.gitbook.io/rosemary-docs/~gitbook/mcp).

# Local Port Forwarding

***

*Beginner · 10 minutes*

Learn how to expose a localhost-only service on the agent's machine to its entire network.

***

### Overview

In this scenario, the Rosemary agent listens on a port on its network interface and forwards all traffic to a service running on `127.0.0.1` on the agent's own machine. This is useful for exposing services that are intentionally restricted to localhost.

**What you'll learn:**

* Agent listens on network interface
* Traffic forwards to `127.0.0.1` on agent
* Exposing localhost-bound services to the network

***

### Scenario

You have nginx running on the agent machine on port `3000`, bound only to `127.0.0.1`. It is **not accessible** from the network. You will use Rosemary to expose it on port `3001` so other machines on the network can access it.

```
[Target on Agent's Network] ───connects to───> [Agent:3001] ───forwards to───> [127.0.0.1:3000]
     192.168.3.10                                     192.168.3.23                      (nginx on agent)
```

***

### Prerequisites

* Rosemary server running (`sudo rosemary`)
* One agent connected to the server
* nginx running on the agent bound to `127.0.0.1:3000`

***

### Step-by-Step

#### Step 1: Verify nginx is Running on Localhost

SSH into the agent machine and check nginx:

```bash
# On the agent machine
sudo systemctl status nginx
# Or if running manually
ps aux | grep nginx
```

**Verify it's bound to localhost only:**

```bash
sudo netstat -tlnp | grep 3000
```

**Output:**

```
tcp        0      0 127.0.0.1:3000          0.0.0.0:*               LISTEN      1234/nginx
```

**Test local access (from agent machine):**

```bash
curl http://127.0.0.1:3000
# Returns nginx welcome page
```

**Test network access (from another machine):**

```bash
curl http://192.168.3.23:3000
# Fails: Connection refused
```

***

#### Step 2: Verify Agent Connection

In the Rosemary CLI, confirm the agent is connected:

```bash
rosemary> agents
```

**Output:**

```
ID        OS       Hostname      Subnets
agent-1   linux    pivot-host    192.168.3.0/24
```

***

#### Step 3: Create the Local Port Forward

Tell the agent to listen on port `3001` and forward to `127.0.0.1:3000`:

```bash
rosemary> forward add 3001 agent-1 127.0.0.1 3000 tcp
```

**Command breakdown:**

| Part          | Meaning                                        |
| ------------- | ---------------------------------------------- |
| `forward add` | Create a new forward                           |
| `3001`        | Port the agent listens on (network accessible) |
| `agent-1`     | Agent ID                                       |
| `127.0.0.1`   | Target IP (agent's localhost)                  |
| `3000`        | Target port (nginx)                            |
| `tcp`         | Protocol                                       |

**Output:**

```
[+] Forward added: agent agent-1 listening on :3001 -> 127.0.0.1:3000 (tcp)
```

***

#### Step 4: Access nginx from the Network

From any other machine on the internal network:

```bash
curl http://192.168.3.23:3001
```

**Output:**

```html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...
</html>
```

The nginx service that was restricted to localhost is now accessible to the entire network through port `3001`.

***

#### Step 5: List Active Forwards

```bash
rosemary> forwards
```

**Output:**

```
ID  Agent     Listen  Target          Proto
1   agent-1   :3001   127.0.0.1:3000  tcp
```

***

#### Step 6: Clean Up

```bash
rosemary> forward del 1
```

**Output:**

```
[+] Forward 1 removed
```

The nginx service is no longer exposed to the network.

***

### Use Cases

| Scenario              | Description                               |
| --------------------- | ----------------------------------------- |
| Expose local database | MySQL/PostgreSQL bound to localhost       |
| Development servers   | Node.js/Python dev servers on localhost   |
| Docker containers     | Container ports mapped only to 127.0.0.1  |
| Admin panels          | Web interfaces restricted to localhost    |
| Internal APIs         | Microservices listening on localhost only |

***

### Troubleshooting

| Issue                        | Solution                                                            |
| ---------------------------- | ------------------------------------------------------------------- |
| Port already in use on agent | Choose a different listening port (e.g., `3002`)                    |
| Cannot connect to agent IP   | Verify agent's IP on internal network with `ip addr`                |
| Service not responding       | Confirm service is running: `curl http://127.0.0.1:3000` from agent |
| Firewall blocking            | Agent's listening port must be allowed on internal network          |

***
