How to configure LXC to run Pi-hole

The issue I am facing:

Hi all,
I would like to run Pi-hole inside of LXC container (not lxd). But I don't know how to properly configure LXC containers to run in same LAN subnet range like host (and other devices).
Could you please help me configure it properly?
thank you

Details about my system:

Raspberry Pi 4 8GB
Raspberry Pi OS 64
Linux raspberrypi 5.15.61-v8+
lxc v.4.0.6

What I have changed since installing Pi-hole:

OK, so i found one way myself on page: LXC containers on host's lan - Stack Overflow

The key in this setup is not to bridge eth0 and don't use lxc-net. On the host, /etc/network/interfaces is standard:

auto eth0
iface eth0 inet static
  address 192.168.0.8
  netmask 255.255.255.0
  gateway 192.168.0.1

A bridge is not needed (no lxc-net) but set the container config to create a virtual interface thusly:

lxc.net.0.type = veth
lxc.net.0.veth.pair = veth0
lxc.net.0.flags = up
lxc.net.0.hwaddr = 00:16:3e:xx:xx:xx
lxc.net.0.ipv4.address = 192.168.0.64/32
lxc.net.0.ipv4.gateway = 192.168.0.8
lxc.net.0.script.up = /var/lib/lxc/netup.sh 192.168.0.64
lxc.net.0.script.down = /var/lib/lxc/netdown.sh 192.168.0.64

Some notes on this config: (1) there is no lxc.net.0.link since we don't want a bridge, (2) the lxc.net.0.ipv4.gateway address is the host's IP address, (3) note the netmask is /32, (4) the scripts are explained below.

The netup.sh script routes incoming IP traffic to the container and creates an ARP entry so that eth0 will accept traffic for it:

#!/bin/sh
ip route add ${1}/32 dev veth0
arp -i eth0 -Ds ${1} eth0 pub

The netdown.sh script simply removes the ARP entry (the IP route will go away automatically when veth0 is destroyed).

#!/bin/sh
arp -d -i eth0 ${1} pub

On the guest, /etc/network/interfaces can be empty, since in this case the setup was done in the container config file.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.