|
| 1 | +This guide was developed using a OPNsense 25.1.1. Following the first method below will have you adding your Pi-hole as a DNS server for all devices on your LAN. Following the second method below will have DNS queries route through your OPNsense and then to your Pi-hole. Either option is valid, depending on your specific requirements, but it is recommended to choose one or the other - not both. |
| 2 | + |
| 3 | +Optionally, there is also the possibility to block DNS traffic to other, non-Pi-hole DNS servers via firewall rules. This can be used to prevent clients like Chromecasts of using their hardcoded DNS servers. |
| 4 | + |
| 5 | +Beware: This tutorial does not explain on how to block DoH or DoT. |
| 6 | + |
| 7 | +### Grab your IPv4 and IPv6 address from your Pi-hole |
| 8 | + |
| 9 | +SSH into your Pi-hole. A quick `ifconfig eth0` (substituting `eth0` for the applicable network interface) will get you the address information: |
| 10 | + |
| 11 | +``` |
| 12 | +eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 |
| 13 | + inet 10.0.51.2 netmask 255.255.255.0 broadcast 192.168.0.255 |
| 14 | + inet6 2001:470:5:3::8ed prefixlen 128 scopeid 0x0<global> |
| 15 | + inet6 2001:470:5:3:8d70:e92e:7030:6eef prefixlen 64 scopeid 0x0<global> |
| 16 | + inet6 fe80::337f:710d:6781:4c85 prefixlen 64 scopeid 0x20<link> |
| 17 | + ether dc:a6:32:0e:31:2d txqueuelen 1000 (Ethernet) |
| 18 | + RX packets 1297459 bytes 1279521260 (1.1 GiB) |
| 19 | + RX errors 0 dropped 0 overruns 0 frame 0 |
| 20 | + TX packets 2315988 bytes 2766370095 (2.5 GiB) |
| 21 | + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 |
| 22 | +``` |
| 23 | + |
| 24 | +Take note of the `inet` address, and the `inet6` address which starts with `fe` (denotes a local link or LAN address). Those are the IPs which you'll use as your DNS servers. For this example, we'll be using the `10.0.51.2` and `fe80::337f:710d:6781:4c85` IPs for the following directions. |
| 25 | + |
| 26 | +### Method 1: Distribute Pi-hole as DNS server via DHCP |
| 27 | + |
| 28 | +Choosing this option will have DNS queries taking the following path: |
| 29 | + |
| 30 | +``` |
| 31 | +(Clients) -> Pi-hole -> Upstream DNS Server |
| 32 | +``` |
| 33 | + |
| 34 | +#### IPv4 |
| 35 | + |
| 36 | +Go to **Services** --> **ISC DHCPv4** and click on the interface that you want to modify. |
| 37 | +Under **DNS servers** enter your Pi-hole IPv4 address into the first line. |
| 38 | +Leave the second line blank unless you have another Pi-hole host running. |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +#### IPv6 |
| 43 | + |
| 44 | + |
| 45 | +Go to **Interfaces** and click on the interface that you want to modify. |
| 46 | +Under **Manual configuration** check "Allow manual adjustment of DHCPv6 and Router Advertisements" |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +Now go to **Services** --> **Router Advertisement** and click on the interface that you want to modify. |
| 51 | +Under **DNS servers** enter your Pi-Holes IPv6 address into the first line. |
| 52 | +Leave the second line blank unless you have another Pi-hole host running. |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +Beware: Link local IPv6 are not reachable from other VLANs. |
| 57 | + |
| 58 | + |
| 59 | +All client devices on your network should now automatically be configured with a single IPv4 and single IPv6 address for DNS resolution. If you don't see your client devices update quickly enough, you *can* toggle Wi-Fi off/on or disconnect the Ethernet cable for a couple of seconds and then reconnect it. |
| 60 | + |
| 61 | +### Method 2: Using your Pi-hole as DNS server for your OPNsense |
| 62 | + |
| 63 | +If you'd rather have your OPNsense forward all DNS traffic to Pi-hole, you can complete this *alternative* step. |
| 64 | +Doing so will have DNS queries taking the following path: |
| 65 | + |
| 66 | +``` |
| 67 | +(Clients) -> OPNsense -> Pi-hole -> Upstream DNS Server |
| 68 | +``` |
| 69 | + |
| 70 | +!!! warning |
| 71 | + Keep in mind that if you are using your OPNsense IP address as an upstream resolver for your Pi-hole together with this configuration, ***this will cause a resolver loop***. Make sure to have another upstream DNS server(s) set in your Pi-hole's configuration, or use Pi-hole as [your own recursive DNS server with Unbound](https://docs.pi-hole.net/guides/dns/unbound/?h=unbound#configure-unbound). |
| 72 | + |
| 73 | +1. In Pi-hole, navigate to `Settings -> DNS` and ensure you have at least one external upstream DNS server enabled. |
| 74 | +2. In OPNsense navigate to `Settings -> General -> Networking`. |
| 75 | +3. Under DNS Servers, enter the IPv4 and or IPv6 address of your Pi-hole host. |
| 76 | +4. Uncheck `Allow DNS server list to the overridden by DHCP/PPP on WAN`. |
| 77 | +5. Click Save. |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +### Block other DNS servers (optional) |
| 82 | + |
| 83 | +Some clients have hardcoded DNS settings. To prevent them from circumventing our Pi-Hole, we can optionally block all DNS request that are not coming from our Pi-Hole. |
| 84 | +!!! warning |
| 85 | + If your device does not have any fallback from the hardcoded DNS settings, this could break its DNS. |
| 86 | + |
| 87 | +We first create an Alias for Pi-holes MAC address. |
| 88 | +Go to **Firewall** --> **Aliases** scroll down and click on the plus button to create a new alias. |
| 89 | +Give it a Name, select the type MAC address and enter the MAC address from Pi-Hole into the "Content" field and then click save. |
| 90 | +The MAC address of your device you can find out by looking at the line `ether` from our `ifconfig eth0` output we got earlier. |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | +Now go to **Firewall** --> **Rules** and click on the interface that you want to modify. |
| 95 | + |
| 96 | +We create one rule to allow Pi-hole to do DNS. |
| 97 | +After that rule, we create a rule to reject ALL DNS. It is important that this reject rule is after the other rule. |
| 98 | +OPNsense does the first rule that matches, if we have our reject rule first, Pi-hole can not reach any DNS servers. |
| 99 | +It should look something like this: |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +We don't need a rule to allow for example device 10.0.51.10 to reach our Pi-hole at 10.0.51.2, since they are both on the same network, DNS request will not be routed through OPNsense. Instead the client 10.0.51.10 will directly contact Pi-hole for DNS requests. |
0 commit comments