> This page is for Portal.

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.emnify.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.emnify.com/_mcp/server.

> Control which destinations your devices can reach by writing IP firewall rules and assigning them to service policies in the emnify Portal

> **Note**
>
> Your role needs [permission](/portal/roles/custom) to manage IP firewall profiles and to assign them to service policies.

The IP firewall controls which destinations your devices can reach.
You write rules that allow or deny traffic by IP address, protocol, and port, and the emnify network enforces them before the traffic leaves the network.
Use it to restrict devices to the backends they need, so a compromised or misconfigured device can't reach anything else.

The IP firewall filters outbound traffic only, which is traffic that your devices send.

## Prerequisites

The IP firewall is a subscription for your organization.
To request access, contact [emnify support](https://support.emnify.com/hc/en-us/requests/new).

If the subscription ends, your IP firewall profiles stay in your organization, but the network stops enforcing them until you subscribe again.

## How the IP firewall works

You configure the IP firewall with two objects:

* An **IP firewall profile** is a named list of rules.
  You can reuse one profile on several service policies.
* A **service policy** applies IP firewall profiles to its devices.
  You can assign up to 10 profiles to one service policy, and every device with that service policy uses them.

The IP firewall applies to a device only when its service policy has at least one IP firewall profile assigned and the IP firewall is enabled on that service policy.
Otherwise, the device's traffic isn't filtered.

### How rules are evaluated

The network checks the rules in a profile from top to bottom.
The first rule that matches the traffic decides whether it's allowed or denied, and the rules after it are ignored.

When a service policy has several IP firewall profiles, the network checks each profile on its own:

* If any profile denies the traffic, it's denied, even if another profile allows it.
* If no profile denies the traffic and at least one allows it, it's allowed.
* If no rule in any profile matches, the traffic is denied.

The network drops denied traffic without sending a response to the device.
Applications on the device see a connection timeout, not a rejected connection.

> **Warning**
>
> Traffic that no rule matches is denied.
> Before you enable the IP firewall, make sure your rules allow everything your devices need, including DNS.
> Otherwise, your devices can't resolve hostnames.

## Write the rules

An IP firewall profile is a plain text file with one rule per line.
Each rule has this format:

```text
ACTION PROTOCOL DIRECTION ADDRESS... [PORTS:...] [# COMMENT]
```

| Field     | Required | Values                                                                                                                                                                                          |
| :-------- | :------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| ACTION    | Yes      | `ALLOW` or `DENY`                                                                                                                                                                               |
| PROTOCOL  | Yes      | `TCP`, `UDP`, `ICMP`, or `ANY`                                                                                                                                                                  |
| DIRECTION | Yes      | `OUT`                                                                                                                                                                                           |
| ADDRESS   | Yes      | The destination, as one or more of `IP:<address>` for a single IPv4 address or `CIDR:<address>/<prefix>` for a range, or `ANY` for every destination. Separate several destinations with spaces |
| PORTS     | No       | `PORTS:443` for one port, `PORTS:80,443` for several, `PORTS:10000-10100` for a range, or `PORTS:ANY`. Applies to `TCP` and `UDP` only. Without it, the rule matches every port                 |
| COMMENT   | No       | Free text after `#`, at the end of a rule or on its own line                                                                                                                                    |

Keywords aren't case-sensitive, and empty lines are ignored.
A profile can have up to 100 rules, an organization can have up to 100 IP firewall profiles, and a service policy can have up to 10 of them assigned.

This example allows DNS, MQTT to one broker, and HTTPS to a range of cloud APIs.
It needs no final deny rule, because the IP firewall denies traffic that no rule matches:

```text
ALLOW UDP OUT IP:8.8.8.8 IP:8.8.4.4 PORTS:53     # DNS
ALLOW TCP OUT IP:203.0.113.10 PORTS:8883         # MQTT to the broker
ALLOW TCP OUT CIDR:52.95.0.0/16 PORTS:443        # HTTPS to cloud APIs
```

When you write rules, keep in mind:

* **Put specific rules before broad ones.**
  A broad rule such as `DENY TCP OUT ANY PORTS:443` placed first matches all HTTPS traffic, so a later rule that allows one HTTPS backend never applies.
* **Allow your DNS servers.**
  By default, emnify routes DNS queries to Google's public DNS at `8.8.8.8` and `8.8.4.4`.
  If you [use your own DNS servers](/portal/how-tos/configure-dns-settings), allow those addresses instead.
* **Allow your Cloud Connect ranges.**
  The IP firewall also filters traffic to private addresses.
  If your devices reach your backend through [Cloud Connect](/services/cloud-connect), add a rule such as `ALLOW TCP OUT CIDR:<your-cloud-connect-range> PORTS:ANY`.
* **Allow device-to-device traffic.**
  The IP firewall also filters traffic between your devices.
  If you [enable device-to-device communication](/portal/how-tos/enable-device-to-device-communication), add a rule that allows your devices' IP address range.
* **Avoid `ALLOW ANY OUT ANY`.**
  It allows all outbound traffic, so the profile doesn't restrict anything.
* **Don't add a catch-all `DENY ANY OUT ANY`.**
  A deny in any profile is final, so a catch-all deny in one profile blocks everything that the other profiles on the same service policy allow.
  Traffic that no rule matches is already denied.

## Create an IP firewall profile

In [**Device Policies**](https://portal.emnify.com/device-policies), find **IP Firewall profiles** in the **Security Policies** section.

Click **Actions > Download example profile**.
The file explains the rule format and contains example rules as comments.

Edit the file and add your rules.
Every line in the example profile is a comment, so the file has no rules.
If you upload it unchanged, the profile denies all outbound traffic after you enable the IP firewall.

Click **Actions > New IP Firewall Profile**.
The **Add IP firewall profile** dialog opens.

Replace the default name with a name for the profile.
Optionally, add a description.

In **Rules file**, upload your file: drag it into the box, or click the link in the box and select it.
The file must be a `.txt` file.

Click **Save**.
The Portal validates the rules and shows any problems before it saves the profile.

## Change an IP firewall profile

Changes to a profile apply to every service policy it's assigned to.

In **IP Firewall profiles**, open the options menu in the profile's row and select **Profile details**.
The **IP Firewall profile details** dialog opens.

To change the name or description, select the edit icon next to it and enter the new text.

To change the rules, select the download icon next to the rules file to get the current rules, and edit the file.

Select the replace icon next to the rules file, and select your updated file.

Click **Save**.

## Assign profiles and enable the IP firewall

In [**Service Policies**](https://portal.emnify.com/device-policies#service-policies), choose the policy you want to update and expand the policy details.

In **Security**, select the edit icon in the **Enable IP Firewall** row.
If no profile is assigned yet, you can also click **Attach profiles**.
The **IP firewall profiles** dialog opens.

In **Choose profile**, select an IP firewall profile, and select the check mark to add it.
To add more profiles, repeat this step.

Click **Close**.

Turn on the **Enable IP Firewall** switch.
The network starts to enforce the rules for every device with this service policy.

You can't turn on the IP firewall on a service policy without an assigned profile.
While the IP firewall is on, you can't remove the last assigned profile.

## Disable the IP firewall

In [**Service Policies**](https://portal.emnify.com/device-policies#service-policies), choose the policy you want to update and expand the policy details.

In **Security**, turn off the **Enable IP Firewall** switch.
The assigned profiles stay on the service policy, but the network stops enforcing them.

## Track configuration changes

Assigning or removing an IP firewall profile and enabling or disabling the IP firewall each create a [Policy management](/system-events/event-types/policy-management) event.
Use these events to audit when the configuration changed.

## Manage the IP firewall with the emnify REST API

You can also manage IP firewall profiles and their assignment with the emnify REST API.
Use the `dry_run` parameter to validate rules without saving them.

For details, see [Create an IP firewall profile](/developers/api/ip-firewall-profile/ip-firewall-profile-create) and [Replace IP firewall profiles on a service profile](/developers/api/service-profiles/service-profile-ip-firewall-rule-set-put) in the API reference.