Stop DHCP From Overwriting resolv.conf on FreeBSD 15 and 14
Four ways to keep dhclient from replacing your nameservers in /etc/resolv.conf on FreeBSD 15.1 and 14.5, from the cleanest to the bluntest.
On a FreeBSD server that gets its address by DHCP, dhclient rewrites /etc/resolv.conf every time it obtains or renews a lease, so nameservers you add by hand disappear at the next renewal or reboot. This guide shows four ways to keep your own resolvers in place on FreeBSD 15.1 and 14.5, the releases supported at the time of writing. FreeBSD 10 through 13 are end-of-life; the same mechanisms exist there, but you should upgrade rather than patch around them.
How resolv.conf gets written
Three pieces are involved:
dhclient(8)receives the lease and runs/sbin/dhclient-script.dhclient-scriptbuilds a per-interface resolver file from the DHCP optionsdomain-name,domain-searchanddomain-name-servers, then passes it toresolvconf(8).resolvconfmerges the data from all interfaces and writes/etc/resolv.conf, following the rules in/etc/resolvconf.conf.
You can step in at either level: tell dhclient to ignore or replace what the server sends, or tell resolvconf what to do with it. Pick one method, not several.
Before you start
Run the commands as root, or prefix them with sudo or doas if you have one installed. Find the interface that uses DHCP and look at the current resolver file:
sysrc -a | grep ifconfig_
cat /etc/resolv.confThe examples use vtnet0, the usual VirtIO interface name on a VPS. Keep your provider's web console open while you work: a mistake in network configuration can lock you out of SSH. Back up the files you are going to edit:
cp /etc/rc.conf /etc/rc.conf.bak
cp /etc/resolv.conf /etc/resolv.conf.bakMethod 1: override the DHCP option in dhclient.conf
This is the most precise fix. dhclient keeps doing everything else (address, route, lease renewal) but uses your nameservers instead of the ones offered. Edit /etc/dhclient.conf, which is normally empty apart from comments, and add:
interface "vtnet0" {
supersede domain-name-servers 203.0.113.53, 198.51.100.53;
}The modifiers documented in dhclient.conf(5) behave as follows:
| Statement | Effect |
|---|---|
supersede | Always use your value and discard the server's. |
prepend | Put your value first, then the server's. |
append | Use the server's values first, then yours. |
default | Use your value only if the server sends none. |
For a local caching resolver, use supersede domain-name-servers 127.0.0.1;. To fix the search domain as well, add a second line inside the same block:
supersede domain-name "example.com";Apply the change by restarting the client on that interface. The connection may pause for a second while the lease is renewed:
service dhclient restart vtnet0Method 2: configure resolvconf
/etc/resolvconf.conf does not exist by default; create it. It is sourced as a shell script, so values need quotes. This method also covers DNS information that arrives from sources other than DHCP, such as VPN clients that call resolvconf.
Put your resolver first
name_servers="127.0.0.1"name_servers prepends servers to the generated list. When one of them is a local address (127.* or ::1), resolvconf by default writes only that local server to resolv.conf and leaves the others out. Set resolv_conf_local_only="NO" if you want the DHCP-supplied servers listed after it as a fallback.
Write only your own settings
To ignore everything dynamic and write just what the file specifies, for example a third-party resolver:
resolv_conf_passthrough="NULL"
name_servers="203.0.113.53 198.51.100.53"
search_domains="example.com"Switch resolvconf off
If you want to maintain /etc/resolv.conf entirely by hand:
resolvconf="NO"This stops resolvconf from running any of its subscribers, so the file is never regenerated. Older tutorials suggest resolv_conf="/dev/null", which redirects the output instead; it has the same practical result, but resolvconf="NO" is the documented switch. After either change, edit /etc/resolv.conf yourself.
After editing resolvconf.conf, regenerate the resolver file:
resolvconf -uIf you use the base system's Unbound
When you enable the bundled caching resolver, its setup script does this work for you: it points /etc/resolv.conf at 127.0.0.1, writes a resolvconf.conf containing libc="NO" so the file is left alone, and feeds DHCP-supplied servers to Unbound as forwarders instead.
sysrc local_unbound_enable="YES"
service local_unbound startDo not combine this with your own edits to resolvconf.conf; the generated file is marked as not to be edited.
Method 3: use static addressing
If dhclient never runs, nothing rewrites the file. Only do this if your provider assigns the address statically and permits it; some networks require DHCP. Note the current values first:
ifconfig vtnet0 | grep inet
route -n get default | grep gatewayThen replace the DHCP setting, using your real address, netmask and gateway:
sysrc ifconfig_vtnet0="inet 203.0.113.10 netmask 255.255.255.0"
sysrc defaultrouter="203.0.113.1"Write your nameservers to /etc/resolv.conf and reboot from a session where you also have console access:
shutdown -r nowIf the server does not come back, log in through the console and restore /etc/rc.conf.bak.
Method 4: make the file immutable (last resort)
Setting the system-immutable flag blocks every write, including by root:
chflags schg /etc/resolv.conf
ls -lo /etc/resolv.confIt works, but it is a blunt tool: every attempt to regenerate the file at lease renewal now fails, and so does anything else that legitimately needs to update it, including your own editor. Use it only as a stopgap. Remove the flag with:
chflags noschg /etc/resolv.confVerify that the setting survives
Check what resolvconf has collected, what it wrote, and that name resolution works:
resolvconf -l
cat /etc/resolv.conf
getent hosts www.freebsd.orgThen force the situation that used to break it. Renew the lease, and finally reboot, and compare the file each time:
service dhclient restart vtnet0
cat /etc/resolv.confIf the DHCP servers are back, check that the interface name in dhclient.conf matches exactly, that each statement ends with a semicolon, and that resolvconf.conf contains valid shell syntax (sh -n /etc/resolvconf.conf reports errors).
Frequently asked questions
Which method should I choose?
Use dhclient.conf when DHCP is the only thing touching DNS and you simply want different nameservers. Use resolvconf.conf when you run a local resolver or have several sources of DNS data. Static addressing and chflags solve the problem as a side effect and are rarely the right first choice.
Can I add extra lines without replacing the DHCP servers?
Yes. dhclient-script appends the contents of /etc/resolv.conf.tail, if that file exists, to the data it hands to resolvconf. That is a convenient place for an options line. For extra nameservers, append domain-name-servers in dhclient.conf or name_servers_append in resolvconf.conf is cleaner.
Does this also cover IPv6?
dhclient only handles DHCPv4. DNS servers learned from IPv6 router advertisements reach resolvconf through rtsold, so the resolvconf.conf method is the one that controls both address families.
Why does resolv.conf list only three nameservers?
The libc resolver uses at most three nameserver lines. If you prepend your own servers to a long DHCP list, the ones at the end are never queried.