17

I would like to use DHCP to assign an IP address to my server, but specify the DNS servers manually. I tried the following simple configuration:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: yes
      nameservers: [8.8.8.8,8.8.4.4]

After running netplan apply I check the nameservers with systemd-resolve --status and it shows:

DNS Servers: 8.8.8.8
             8.8.4.4
             192.168.1.1

The last is the DNS server provided by DHCP, which I do not want to be in the list.

Does netplan have a way to accomplish this?

timbo
  • 333
alanwj
  • 461

4 Answers4

19

This has been fixed in the netplan repository on github, and will presumably make its way into Ubuntu at some point.

Two new options have been added, dhcp4-overrides and dhcp6-overrides. To ignore DNS servers from DHCP you would do something like:

dhcp4: yes
dhcp4-overrides:
  use-dns: no
alanwj
  • 461
5

There is unfortunately no changelog in the netplan github repo to ascertain when the newer features came in. However there was a big jump in release versions in https://github.com/CanonicalLtd/netplan/releases from 0.40 to 0.90 in November 2018

On Ubuntu 18.10, netplan is presently at version 0.40.2.2

With that netplan version, trying a wireless config to do DHCP but with the Cloudflare anonymous DNS servers:

network:
  version: 2
  renderer: NetworkManager
    ethernets:
      wlp4s0:
        dhcp4: yes
        dhcp4-overrides:
          use-dns: no
        nameservers:
          addresses: [1.1.1.1,1.0.0.1]

produces an Unknown key dhcp4-overrides error:

enter image description here

There are no backports to cosmic according to ubuntu.packages.org

On Ubuntu 19.04 beta, netplan version 0.96 is installed and the dhcp4-overrides can be used:

enter image description here

timbo
  • 333
2

This is a really common setup that's totally borked in Ubuntu 18.04, Netplan needs to support the NetworkManager options when using DHCP:

nmcli connection modify ${ID} ipv4.ignore-auto-dns yes
nmcli connection modify ${ID} ipv4.dns 'xx.xx.xx.10 xx.xx.xx.20'
nmcli connection modify ${ID} ipv4.dns-search 'abc01.domain.com. abc02.domain.com.' 
1

Using dhcp6-overrides with 'use-dns: false' doesn't work to ignore DNS servers provided over DHCP6.

NB: I'm using netplan.io 0.96 and systemd 240 in Ubuntu 19.04 (disco).

# cat /etc/netplan/10-enp3s0-init.yaml
network:
    version: 2
    renderer: networkd
    ethernets:
        enp3s0:
            critical: true
            dhcp4: true
            dhcp4-overrides:
                use-dns: false
            dhcp6: true
            dhcp6-overrides:
                use-dns: false
            ipv6-privacy: true
            nameservers:
                search: [home]
                addresses: [1.0.0.1, 1.1.1.1]

# resolvectl status enp3s0
Link 2 (enp3s0)
      Current Scopes: DNS
DefaultRoute setting: yes
       LLMNR setting: yes
MulticastDNS setting: no
  DNSOverTLS setting: opportunistic
      DNSSEC setting: yes
    DNSSEC supported: yes
  Current DNS Server: 1.0.0.1
         DNS Servers: 1.0.0.1
                      1.1.1.1
                      fd50:a94:67b3:0:26a7:dcff:fe27:a60 <--- DHCP6 provided?
          DNS Domain: home
trcm
  • 11