> For the complete documentation index, see [llms.txt](https://docs.mithril.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.mithril.ai/compute-and-storage/managing-open-ports.md).

# Managing open ports

## Manually opening and closing ports

You can use the following to expose ports on your instances, replacing `<port_number>` with the port number you want to forward to your instance's public IP:

```sh
sudo nohup foundrypf <port_number> >/dev/null 2>&1 &
```

This will start a process that maintains the open port. You can omit `nohup` to run it in the foreground.

You can close the port with the following command, replacing `<port_number>` with the port number you want to close:&#x20;

```sh
sudo foundrypf -d <port_number>
```

## Creating a systemd service to expose ports

If you want to ensure the port forwarding is kept running in the background persistently even after a system reboot or in the case that the process fails in the background due to network blips, you can create a systemd service for it.

Open a new service file using a text editor (in this case, nano):

```bash
sudo nano /etc/systemd/system/examplefoundry.service
```

Add the following content to the service file:

```
[Unit]  
Description=Foundry Port Forwarding Service  
After=network.target
Wants=network-online.target

[Service]  
Type=simple  
User=root  
ExecStart=/usr/local/bin/foundrypf <port_number>  
Restart=always  
RestartSec=3

[Install]  
WantedBy=multi-user.target
```

Again, replace `<port_number>` with the port you want to keep forwarded persistently.

```bash
sudo systemctl daemon-reload  
sudo systemctl enable examplefoundry  
sudo systemctl start examplefoundry
```

Now, the `foundrypf` script will run as a service, and the specified port will be forwarded persistently. Adjust the `<port_number>` and service file name if you wish to set up multiple such services for different ports.

Note that lower-number ports are currently not supported on `foundrypf`. We recommend running applications on higher-number ports.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.mithril.ai/compute-and-storage/managing-open-ports.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
