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 Oct 3, 2020 via email

@marke17
Copy link

marke17 commented Oct 4, 2020

Thank you for the reply, but what I am trying to do is to set one, global property that would be applied to every WIFI connection I will make in future. I'm using a laptop to connect to numerous different WIFI networks, so I would like to skip configuring dns-search and dns-servers for every connection.
Sorry that I did not explain this clearly enough.
I found a config property for NetworkManager that should work, but setting it did not change my resolv.conf.
[https://developer.gnome.org/NetworkManager/stable/NetworkManager.conf.html] documents section "global-dns" with property "searches" that should do exactly what I need, but it doesn't so far.

@mariusse
Copy link

mariusse commented Jan 22, 2021

Editing /etc/dhcp/dhclient.conf doesn't work anymore in Ubuntu 20.04 (I'm actually using LinuxMint based on it, but I don't think it matters).
I used to add 'append domain-search "mycompany.com"' so that I can always connect to hosts in "mycompany.com" domain without entering FQDN.
Is there another way to do this now, because ubuntu 20.04 does not use dhclient anymore - I'm connected to wifi right now, and there is no process with 'dhc' in its name.
I suppose there should be a way to tell NetworkManager to append "mycompany.com" domain to its "ipv4.dns-search" property GLOBALLY instead of adding it to every connection, but I could not find it?

/etc/systemd/resolved.conf
Remove comment so it says:
Domains=
and reboot (you can probably restart something, but systemd-resolvd.service didn't do it) then you should get the domain set in your router (or DHCP server?) and can just ping host to actually ping host.mydomain. Or if your router is not setting domain, set one yourself.
Have a look at the auto generated /etc/resolv.conf after reboot to see search has been set correctly

@lovefore
Copy link

lovefore commented Apr 3, 2021

Editing /etc/dhcp/dhclient.conf doesn't work anymore in Ubuntu 20.04 (I'm actually using LinuxMint based on it, but I don't think it matters).
I used to add 'append domain-search "mycompany.com"' so that I can always connect to hosts in "mycompany.com" domain without entering FQDN.
Is there another way to do this now, because ubuntu 20.04 does not use dhclient anymore - I'm connected to wifi right now, and there is no process with 'dhc' in its name.
I suppose there should be a way to tell NetworkManager to append "mycompany.com" domain to its "ipv4.dns-search" property GLOBALLY instead of adding it to every connection, but I could not find it?

Yes, ubuntu 20.04 does not use /etc/dhcp/dhclient.conf to control /etc/resolv.conf any more!!! What should I do to add an additional domain-name to it? What is /etc/dhcp/dhclient.conf used for? Just to take my precious disk? Or make me confused and wasting my time? What is ubuntu thinking about! Keep changing things in a weird way~

@plembo
Copy link
Author

plembo commented Apr 4, 2021

@lovefore In answer to your question about adding a search domain in Ubuntu 20.04, see my comment above (https://gist.github.com/plembo/26027128bc7cbdbb0b967a2fb275da50#gistcomment-3476223). Ubuntu 20.04 Desktop uses NetworkManager to manage DHCP connections (just like Red Hat, Fedora and a number of other distros). Ubuntu 20.04 Server uses netplan on top of systemd-networkd (which no one else uses). I think Canonical's inconsistency in this and other areas are why they've had difficulty breaking into the enterprise space over the years. It's the also the reason I think a lot of shops could wind up on Debian after the dust clears from IBM's recent missteps with Red Hat (first version of that statement was made with a lot more certainty, because when dealing with enterprise IT you always have to take the effect of sheer momentum into account).

@lovefore
Copy link

lovefore commented Apr 5, 2021

@plembo

@lovefore In answer to your question about adding a search domain in Ubuntu 20.04, see my comment above (https://gist.github.com/plembo/26027128bc7cbdbb0b967a2fb275da50#gistcomment-3476223). Ubuntu 20.04 Desktop uses NetworkManager to manage DHCP connections (just like Red Hat, Fedora and a number of other distros). Ubuntu 20.04 Server uses netplan on top of systemd-networkd (which no one else uses). I think Canonical's inconsistency in this and other areas are why they've had difficulty breaking into the enterprise space over the years. It's the also the reason I think a lot of shops could wind up on Debian after the dust clears from IBM's recent missteps with Red Hat (first version of that statement was made with a lot more certainty, because when dealing with enterprise IT you always have to take the effect of sheer momentum into account).

Yes, I agree! And thanks for your information about my problem. Though Ubuntu users amount seems growing, most of them never could make profit for Ubuntu. Only when it break into enterprise market, could it be something real and make money. Still a lot of work to do. And hope it won't become another stream like centos8~ Anyway, thanks for all the efforts so far.

@lovefore
Copy link

lovefore commented Apr 5, 2021

@plembo
By the way, there is a little problem with your commands. I think everyone should tell it easily and make it work. Anyway, I listed the corrected ones.Thanks, it solved my problem. Though I do not like this way~
$ sudo nmcli con mod 'Wired connection 1' ipv4.dns-search "example.com"
$ sudo nmcli con show 'Wired connection 1' | grep dns-search

@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