INTRO:
I'm sure quite a few people have run into problems with DNS caching. There are plenty of hints and tips for flushing DNS cache in Ubuntu, and the more rare info about disabling it altogether. But most of these answers don't get to the core of the problem: Do you NEED DNS caching at all when you're a normal user who runs your own internal DNS server? (Kind of a joke, but not.)
The problem I have is that I have my own domain internally, and run my own DNS server (BIND) which serves out both the internal hosts on my network (about 75 hosts), and also is a caching resolver for internet hosts (that way I can browse the web slightly faster, although that is less true today than it was 20 years ago).
The purpose of DNS caching is to take load off of DNS servers. I suppose if you're Joe Average who doesn't run your own DNS server it might be seen as a way of going easy on your ISP's DNS servers. But for people like me who need to have all of my hosts internally accessible by name, it's imperative to be able to resolve the internal domain as well.
I user autofs with nfs to automagically mount different volumes for music, videos, photos, and so on. Sure, I could do all of that by IP, but hostnames look and feel better. And who has time to manage /etc/hosts on 75 different machines? As such, I want DNS queries to ONLY hit my DNS server, and Ubuntu 22.04 seems to have special issues with this.
THE PROBLEM:
I've had many instances now where I try to open my automount NFS volume and it's not there. I do an nslookup for my server and... there is no record in the cache that is running locally. I've tried multiple solutions and none have worked. There was one solution that was supposed to make sure that my DNS server running on 192.168.20.10 would be the server that the cache would rely on. Didn't work at all.
Another solution (kind of messy) was to manually set up /etc/resolv.conf and then make it immutable. That didn't work because /etc/resolv.conf is a symlink. What I did for a while was just manually correct it to point to my DNS and it would inevitably return to the one set up by NetworkManager. I finally found a solution.
SOLUTION (But...)
#!/bin/bash
[[ $(id -u) -ne 0 ]] && echo "This must run as root" && exit
systemctl disable systemd-resolved.service
systemctl stop systemd-resolved
grep "dns=none" /etc/NetworkManager/NetworkManager.conf || sed -i '/\[main\]/a dns=none' /etc/NetworkManager/NetworkManager.conf
rm /etc/resolv.conf
printf "nameserver 192.168.20.10\nsearch mydomain.prv\n" > /etc/resolv.conf
chattr +i /etc/resolv.conf
This actually worked. But, it's not the right answer. The correct solution would be to find out WHY the DNS cache will work with my DNS for a while and then give up and start only serving out internet hosts bypassing my DNS server. And that leads me to...
THE QUESTION:
Does anyone know how the default DNS Cache in Ubuntu 22.04 selects where it gets DNS answers from? I tried adding the DNS servers to the netplan and that did not work. I can't even find any definitive answer on what DNS cache Ubuntu uses. I've seen references to dnscache mostly, but a few to dnsmasq. My system does not have a dnscache command, but it does have a dnsmask command. I upgraded from Ubuntu 18.04 to 22.04, so maybe these issues are due to cruft? I'd have no problem with using the cache as long as it always uses my DNS server, but I've not found any reliable instructions to fix the real problem. So, for now, I'll still stick with the script above since it's "working". But I would love to have things working RIGHT.