Skip to content

Instantly share code, notes, and snippets.

@plembo
Last active November 18, 2024 08:51
Show Gist options
  • Save plembo/26027128bc7cbdbb0b967a2fb275da50 to your computer and use it in GitHub Desktop.
Save plembo/26027128bc7cbdbb0b967a2fb275da50 to your computer and use it in GitHub Desktop.
Add search domain under Ubuntu 18.04 LTS and later NetworkManager

Add a search domain under NetworkManager on Ubuntu Desktop 18.04 LTS and later (ipv4)

In older versions of the NetworkManager gui there was a field for the search domain to be written into /etc/resolv.conf, but it was dropped by the time Ubuntu 18.04 LTS shipped. You can add it by editing the system-connections file directly, but running the appropriate nmcli commands is the right way to do it. Note that Ubuntu Server 18.04, which use netplan and systemd-networkd by default, is completely different. Ubuntu 20.04 involves even more variation (see comments below). It's enough to drive someone to Debian proper...

Basic wired networking

First, get the name of the interface you want to set a search domain on:

$ nmcli con show

Assuming that the target interface will be "Wired connection 1" (note, interface names are case-sensitive):

$ sudo nmcli con mod 'Wired connection 1' ipv4.dns-search "example.com"
$ nmcli con show 'Wired connection 1' | grep dns-search

DHCP networking

If you're using DHCP ("method=auto") in Ubuntu 18.04 and later, the above fix will be ignored.

If you really want to force a domain name into resolv.conf, you need to edit /etc/dhcp/dhclient.conf to add this directive:

supersede domain-name "example.com";

Server side solution

In general I always try to resolve this issue at the DHCP server by making sure I pass the search domain value to clients. Most routers with a DHCP service will provide some way to specify this configuration detail. This is what that would look like in a standard ISC DHCPD configuration (/etc/dhcp/dhcpd.conf):

subnet 10.1.0.0 netmask 255.255.255.0 {
  option routers 10.1.0.1;
  option subnet-mask 255.255.255.0;
  option domain-name "example.com";
  option domain-search "example.com";

The "domain-name" option is ignored by Linux clients, but needed by Windows machines. The "domain-search" option is what Linux machines use, while Windows machines will ignore it.

NOTES

  1. Ubuntu 18.04 LTS Desktop and later default to using NetworkManager to control networking, with systemd-resolved handling name resolution. But in my experience the default Ubuntu config for systemd-resolved links the wrong file to /etc/resolv.conf: /run/systemd/resolve/stub-resolv.conf.

On a properly configured system the other file in that systemd/resolve directory, named resolv.conf, should look like this:

# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients directly to
# all known uplink DNS servers. This file lists all configured search domains.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver 10.0.1.1
nameserver 8.8.8.8
nameserver 8.8.4.4
search example.com

So if you change the link like this:

$ sudo rm /etc/resolv.conf
$ sudo ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf

then things should work the way they're supposed to.

@plembo
Copy link
Author

plembo commented Apr 5, 2021

@lovefore, I like your method better. It's more precise. With your permission, I'm going to refactor this to use it.

@lonbluster
Copy link

lonbluster commented Feb 10, 2022

incredible ubuntu, ifconfig to ip to resolve.conf to netplan, to nmcli, and still need reboot? WHAT a MESS

@plembo
Copy link
Author

plembo commented Feb 10, 2022

Enough to drive someone to Debian...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment