# configuring the firewall

**URL:** <https://forums.balena.io/t/configuring-the-firewall/369928>\
**Category:** Product support\
**Tags:** raspberrypi4\
**Created:** [February 13, 2024, 2:38pm UTC](https://forums.balena.io/t/configuring-the-firewall/369928 "2024-02-13T14:38:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bas](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/bas/32/10208_2.png) [@bas](https://forums.balena.io/u/bas)\
**Post date:** [February 13, 2024, 2:38pm UTC](https://forums.balena.io/t/configuring-the-firewall/369928/1 "2024-02-13T14:38:19Z")

</div>

I’m trying to enable the firewall feature.

I’ve created a an extra container with iptables installed and set the firewall variable to ‘ON’.

```auto
  firewall:
    build:
      context: ./container-firewall
      dockerfile: ./Dockerfile.firewall
    network_mode: host
    restart: always
    cap_add:
      - NET_ADMIN

```

That seems to work, but I need some help with configuring the firewall.

1. First issue. By default, other containers cannot resolve DNS anymore.

I want to lock down access from the LAN.

1. the exposed docker ports to should only be accessable via the balena public url endpoints.
2. I want also would like to lock down ssh access via LAN. It should only work via the balena ssh tunnel. (tunnel through tunnel).

any help would be appreciated

---

<div class="post-metadata">

**Author:** ![mpous](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mpous/32/4742_2.png) [@mpous](https://forums.balena.io/u/mpous)\
**Post date:** [February 13, 2024, 3:05pm UTC](https://forums.balena.io/t/configuring-the-firewall/369928/2 "2024-02-13T15:05:46Z")

</div>

Hello @bas could you please confirm what other services are you running here? Is this an application we have on balenaHub?

---

<div class="post-metadata">

**Author:** ![bas](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/bas/32/10208_2.png) [@bas](https://forums.balena.io/u/bas)\
**Post date:** [February 13, 2024, 5:07pm UTC](https://forums.balena.io/t/configuring-the-firewall/369928/3 "2024-02-13T17:07:56Z")

</div>

Hi Mpous. It’s a custom stack. Various other services. non that use network\_mode: host.

---

<div class="post-metadata">

**Author:** ![bas](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/bas/32/10208_2.png) [@bas](https://forums.balena.io/u/bas)\
**Post date:** [February 14, 2024, 4:29pm UTC](https://forums.balena.io/t/configuring-the-firewall/369928/4 "2024-02-14T16:29:24Z")

</div>

these is the output from the iptables --list and iptables-legacy --list

I’m not sure how to proceed with this. Is there any documentation/recommendation on how to modify these? Kind of stuck at the moment…

```auto
firewall # Warning: iptables-legacy tables present, use iptables-legacy to see them
 firewall Chain INPUT (policy ACCEPT)
 firewall target prot opt source destination         
 firewall  
 firewall Chain FORWARD (policy ACCEPT)
 firewall target prot opt source destination         
 firewall  
 firewall Chain OUTPUT (policy ACCEPT)
 firewall target prot opt source destination         
 firewall Chain INPUT (policy ACCEPT)
 firewall target prot opt source destination         
 firewall BALENA-FIREWALL all -- anywhere anywhere            
 firewall  
 firewall Chain FORWARD (policy DROP)
 firewall target prot opt source destination         
 firewall DOCKER-USER all -- anywhere anywhere            
 firewall DOCKER-ISOLATION-STAGE-1 all -- anywhere anywhere            
 firewall ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
 firewall DOCKER all -- anywhere anywhere            
 firewall ACCEPT all -- anywhere anywhere            
 firewall ACCEPT all -- anywhere anywhere            
 firewall ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
 firewall DOCKER all -- anywhere anywhere            
 firewall ACCEPT all -- anywhere anywhere            
 firewall ACCEPT all -- anywhere anywhere            
 firewall ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
 firewall DOCKER all -- anywhere anywhere            
 firewall ACCEPT all -- anywhere anywhere            
 firewall ACCEPT all -- anywhere anywhere            
 firewall  
 firewall Chain OUTPUT (policy ACCEPT)
 firewall target prot opt source destination         
 firewall  
 firewall Chain BALENA-FIREWALL (1 references)
 firewall target prot opt source destination         
 firewall ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
 firewall ACCEPT all -- anywhere anywhere ADDRTYPE match src-type LOCAL
 firewall ACCEPT tcp -- anywhere anywhere tcp dpt:48484
 firewall ACCEPT tcp -- anywhere anywhere tcp dpt:48484
 firewall ACCEPT tcp -- anywhere anywhere tcp dpt:48484
 firewall ACCEPT tcp -- anywhere anywhere tcp dpt:48484
 firewall ACCEPT tcp -- anywhere anywhere tcp dpt:48484
 firewall REJECT tcp -- anywhere anywhere tcp dpt:48484 reject-with icmp-port-unreachable
 firewall ACCEPT tcp -- anywhere anywhere tcp dpt:22222
 firewall ACCEPT tcp -- anywhere anywhere tcp dpt:2375
 firewall ACCEPT all -- anywhere anywhere ADDRTYPE match dst-type MULTICAST
 firewall ACCEPT icmp -- anywhere anywhere            
 firewall ACCEPT udp -- anywhere anywhere udp dpt:domain
 firewall RETURN all -- anywhere anywhere            
 firewall REJECT all -- anywhere anywhere reject-with icmp-port-unreachable
 firewall  
 firewall Chain DOCKER (3 references)
 firewall target prot opt source destination         
 firewall ACCEPT tcp -- anywhere 172.17.0.2 tcp dpt:9222
 firewall ACCEPT tcp -- anywhere 172.17.0.4 tcp dpt:5051
 firewall  
 firewall Chain DOCKER-ISOLATION-STAGE-1 (1 references)
 firewall target prot opt source destination         
 firewall DOCKER-ISOLATION-STAGE-2 all -- anywhere anywhere            
 firewall DOCKER-ISOLATION-STAGE-2 all -- anywhere anywhere            
 firewall DOCKER-ISOLATION-STAGE-2 all -- anywhere anywhere            
 firewall RETURN all -- anywhere anywhere            
 firewall  
 firewall Chain DOCKER-ISOLATION-STAGE-2 (3 references)

```

---

<div class="post-metadata">

**Author:** ![bas](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/bas/32/10208_2.png) [@bas](https://forums.balena.io/u/bas)\
**Post date:** [February 19, 2024, 10:51am UTC](https://forums.balena.io/t/configuring-the-firewall/369928/5 "2024-02-19T10:51:30Z")

</div>

Seems I’m on my own here. Reading a bit I found that the following it recommnended `RUN update-alternatives --set iptables /usr/sbin/iptables-legacy`.

In addition I discovered I can use ufw to enable and configure the firewall. That seems a lot less complicated 😉 so I’ll try that next.

---

<div class="post-metadata">

**Author:** ![mpous](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mpous/32/4742_2.png) [@mpous](https://forums.balena.io/u/mpous)\
**Post date:** [February 20, 2024, 5:08pm UTC](https://forums.balena.io/t/configuring-the-firewall/369928/6 "2024-02-20T17:08:42Z")

</div>

Apologizes @bas I have been busy!

Did you fix your issue? Happy to read your solution!

Let me ask internally as well!

---

<div class="post-metadata">

**Author:** ![bas](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/bas/32/10208_2.png) [@bas](https://forums.balena.io/u/bas)\
**Post date:** [February 24, 2024, 1:32pm UTC](https://forums.balena.io/t/configuring-the-firewall/369928/7 "2024-02-24T13:32:55Z")

</div>

not really. I think this article/solution shows some issues around docker and iptables but I need to dive in.

> **[GitHub - chaifeng/ufw-docker: To fix the Docker and UFW security flaw without...](https://github.com/chaifeng/ufw-docker)**
>
> To fix the Docker and UFW security flaw without disabling iptables - chaifeng/ufw-docker

It would be really appreciated if the balena org could disclose how to work with the firewall in an advised manner. Especially on how to block _all_ incoming traffic except those coming from balena (public device url and ssh tunneling stuff).

---

<div class="post-metadata">

**Author:** ![bas](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/bas/32/10208_2.png) [@bas](https://forums.balena.io/u/bas)\
**Post date:** [August 29, 2024, 7:48pm UTC](https://forums.balena.io/t/configuring-the-firewall/369928/8 "2024-08-29T19:48:38Z")

</div>

Hi @mpous did you ever get around to asking this internally? I’ve create a script in a container that directly modifies the ip tables but is a bit iffy. It works bit sometimes after an update the tables get in an incorrect state and stuff stops working. A reboot reset the iptables.

I would love to know what Balena does to the tables exactly. Docker is also known to open up ports for services that exposes ports even if you don’t want that.
