# Firewalls

A firewall is a set of inbound and outbound rules that controls which traffic reaches your servers and where they can connect. Rules only allow traffic. Traffic that no rule matches is dropped, inbound and outbound.

Firewalls are stateful. Replies to a connection you allowed pass in both directions, so you don't need a rule for return traffic.

## Create a firewall

1. Go to **Networks** → **Firewalls** → [Create firewall](https://cloud.betterstack.com/team/t0/firewalls/new ";_blank")
2. Enter a **Name**, for example `web-public`.
3. Under **Inbound rules**, click the shortcut for each service you run, for example **HTTP(s)**.
4. Click **Create firewall**.

The form starts with an inbound rule for SSH on TCP port 22 from any address, and an outbound rule that allows all traffic. A firewall created with the API, the CLI, Terraform, Pulumi or MCP gets only the outbound rule, so add an inbound SSH rule before you attach it.

![Create firewall form with inbound rules and the shortcut buttons](https://imagedelivery.net/xZXo0QFi-1_4Zimer-T0XQ/86bb0d19-0f05-4933-2a69-1b422b888400/md2x =2100x1200)

Each rule has:

- **Sources** for inbound rules or **Destinations** for outbound rules: **Any IPv4**, **Any IPv6**, **Cloudflare IPv4**, **Cloudflare IPv6**, or any CIDR you type, like `203.0.113.0/24`.
- **Protocol**: **TCP**, **UDP**, **ICMP**, **GRE**, **ESP** or **Any**.
- **Port from** and an optional **Port to** for a range. ICMP, GRE, ESP and Any carry no port.

The shortcuts add common inbound rules open to any address: **SSH**, **HTTP(s)**, **PostgreSQL**, **MySQL**, **Redis** and **Node Exporter**. **Allow Cloudflare only** limits ports 80 and 443 to Cloudflare's published IP ranges. Use it for servers behind the [Cloudflare](https://betterstack.com/docs/cloud/integrations/cloudflare/) proxy.

[warning]
#### Keep at least one inbound rule
Without inbound rules, all inbound traffic is dropped and you can't reach your servers over SSH. Locked out? Add the SSH rule back and save. The change reaches your servers within seconds.
[/warning]

## Limit outbound traffic

The outbound rule every new firewall starts with allows all outbound traffic. To limit where your servers connect, remove that rule and add outbound rules for everything your servers depend on, like DNS and package updates. Outbound traffic that no rule matches is dropped.

## Apply a firewall to a server

When you create a server, select the firewall in the **Firewalls** section. A server carries one firewall.

To change the rules later:

1. Open the firewall in **Networks** → [Firewalls](https://cloud.betterstack.com/team/t0/firewalls ";_blank")
2. Add, edit or remove rules.
3. Click **Save changes**.

The change reaches every server that uses the firewall within seconds.

A [Kubernetes cluster](https://betterstack.com/docs/cloud/kubernetes/getting-started/#networking) can carry a firewall too, for its workers.

## What a firewall filters

A firewall filters every public address of a server, both the addresses it booted with and every [reserved IP](https://betterstack.com/docs/cloud/networks/reserved-ips/) you assign to it. One set of rules applies to all of them.

Traffic over a [private network](https://betterstack.com/docs/cloud/networks/private-networks/) isn't filtered. A server on the same private network reaches every port your server listens on, whatever your rules say. Control who is on a private network by choosing which servers you attach to it.

## Sending e-mail

Outbound TCP port 25 is blocked on every server, with or without a firewall, to keep our addresses off spam blocklists. No rule can allow it. Send e-mail through a transactional e-mail provider on port 587 or 465.

## Long-lived connections

An idle TCP connection is forgotten after 24 hours. If the client then sends first and your inbound rules don't allow it, the packet is dropped and the client must reconnect. Use TCP keepalives shorter than 24 hours on connections that stay idle for long.

## Need help?

Please let us know at hello@betterstack.com.  
We're happy to help! 🙏
