Change DNS Server given during Ubuntu 18.04 installation

12,805

Solution 1

Edit your netplan configuration file and remove the old dns server names and add new ones. Edit the file with sudo nano /etc/netplan/01-netcfg.yaml and yours should be similar to the example below:

network:
    version:2 
    renderer: networkd
    ethernets:
         enp0s3:
             dhcp4: true
             nameservers:
                  search: [mydomain, otherdomain]
                  addresses: [10.10.10.1, 1.1.1.1]

The line of interest is the one that says addresses under the settings nameserver. It might also be written like so:

nameservers:
     search: 
         - mydomain
         - otherdomain
     addresses:
         - "10.10.10.1"
         - "1.1.1.1"

Change the address there to the one you desire. Make sure to observe the indentations as ther are. Now after thatv save the file and aplly the changes:

sudo netplan --debug apply

Solution 2

This is a script based on an answer showed on datawookie.

#!/bin/bash        
# Installing resolvconf package
    sudo apt install resolvconf
# Detecting a magic sentence to applies changes... or not
    grep "Make edits to /etc/resolvconf/resolv.conf.d/head." /etc/resolvconf/resolv.conf.d/head &> /dev/null
    if [ ! $? -eq 0 ]
    then
     echo '# Make edits to /etc/resolvconf/resolv.conf.d/head.' | sudo tee --append /etc/resolvconf/resolv.conf.d/head &> /dev/null
     echo 'nameserver 8.8.8.8' | sudo tee --append /etc/resolvconf/resolv.conf.d/head &> /dev/null
     echo 'nameserver 8.8.4.4' | sudo tee --append /etc/resolvconf/resolv.conf.d/head &> /dev/null
    fi
# restarting daemon...
    sudo service resolvconf restart
# Flushing former DNS caches (by security)
    sudo systemd-resolve --flush-caches
    sudo systemctl restart systemd-resolved.service
# and check if DNS changes was done... or not ;)
    nslookup askubuntu.com | grep "Server:"
    read $r
Share:
12,805

Related videos on Youtube

M. Geiger
Author by

M. Geiger

Updated on September 18, 2022

Comments

  • M. Geiger
    M. Geiger over 1 year

    During Ubuntu Server 18.04 installation, I provided the wrong DNS server, say 192.168.0.1. I now want to change it to a different server, say 8.8.8.8, but cannot find the correct spot to do this.

    I know that I can configure the global DNS settings in /etc/systemd/resolved.conf. Although this fixes the problem, I am still seeing the original wrong server entry in systemd-resolve --status:

    Global
         DNS Servers: 8.8.8.8
          DNSSEC NTA: 10.in-addr.arpa
                      16.172.in-addr.arpa
                      168.192.in-addr.arpa
                      17.172.in-addr.arpa
                      18.172.in-addr.arpa
                      19.172.in-addr.arpa
                      20.172.in-addr.arpa
                      21.172.in-addr.arpa
                      22.172.in-addr.arpa
                      23.172.in-addr.arpa
                      24.172.in-addr.arpa
                      25.172.in-addr.arpa
                      26.172.in-addr.arpa
                      27.172.in-addr.arpa
                      28.172.in-addr.arpa
                      29.172.in-addr.arpa
                      30.172.in-addr.arpa
                      31.172.in-addr.arpa
                      corp
                      d.f.ip6.arpa
                      home
                      internal
                      intranet
                      lan
                      local
                      private
                      test
    
    Link 2 (ens3)
       Current Scopes: DNS
       LLMNR setting: yes
       MulticastDNS setting: no
       DNSSEC setting: no
       DNSSEC supported: no
       DNS Servers: 192.168.0.1
       DNS Domain: xyz.com
    

    If I try to use sudo systemd-resolve --interface ens3 --set-dns 8.8.8.8 to change it, I get an error message:

    The specified interface ens3 is managed by systemd-networkd. Operation refused.
    Please configure DNS settings for systemd-networkd managed interfaces directly in their .network files.
    

    Unfortunately, the /etc/systemd/network/ directory is empty. There are no *.network files there.

    However, I found a config file in /run/systemd/network/10-netplan-ens3.network where I can change the entry, but it gets reset to the wrong value after I do

    sudo systemctl daemon-reload
    sudo systemctl restart systemd-networkd
    sudo systemctl restart systemd-resolved
    

    Now, how do I change the link specific setting permanently?

    Edit: Thanks for the input!

    I changed the DNS server in /etc/netplan/01-netcfg.yaml

    Output cat /etc/netplan/01-netcfg.yaml:

    # This file describes the network interfaces available on your system
    # For more information, see netplan(5).
    network:
      version: 2
      renderer: networkd
      ethernets:
        ens3:
          addresses: [ 192.168.0.2/24 ]
          gateway4: 192.168.0.1
          nameservers:
            search: [ xyz.com ]
            addresses:
                - "8.8.8.8"
    

    (Maybe the spaces are not entirely correct because of pasting...)

    Edit 2: I double checked the spaces

    Edit 3: The answer of @George Udosen fixed the problem, thank you very much.

    Output of sudo netplan --debug apply:

    ** (generate:10422): DEBUG: 00:54:03.168: Processing input file //etc/netplan/01-netcfg.yaml..
    ** (generate:10422): DEBUG: 00:54:03.168: starting new processing pass
    ** (generate:10422): DEBUG: 00:54:03.169: ens3: setting default backend to 1
    ** (generate:10422): DEBUG: 00:54:03.169: Generating output files..
    ** (generate:10422): DEBUG: 00:54:03.169: NetworkManager: definition ens3 is not for us (backend 1)
    DEBUG:netplan generated networkd configuration exists, restarting networkd
    DEBUG:no netplan generated NM configuration exists
    DEBUG:device lo operstate is unknown, not replugging
    DEBUG:netplan triggering .link rules for lo
    DEBUG:device ens3 operstate is up, not replugging
    DEBUG:netplan triggering .link rules for ens3
    

    systemd-resolve --status now lists the correct DNS server:

    ...
    Link 2 (ens3)
      Current Scopes: DNS
       LLMNR setting: yes
       MulticastDNS setting: no
       DNSSEC setting: no
       DNSSEC supported: no
       DNS Servers: 8.8.8.8
       DNS Domain: xyz.com
    
    • George Udosen
      George Udosen over 5 years
      edit the file /etc/netplan/01-netcfg.yaml and add it there. Now do cat /etc/netplan/01-netcfg.yaml and add it to your post so I can guide you!
    • M. Geiger
      M. Geiger over 5 years
      Thank you @george-udosen! I edited my post accordingly.
    • George Udosen
      George Udosen over 5 years
      Now apply the changes with sudo netplan apply or sudo netplan --debug apply to debug!
  • M. Geiger
    M. Geiger over 5 years
    This answers my question perfectly, thank you. I upvoted your answer but do not have enough reputation that it shows.
  • George Udosen
    George Udosen over 5 years
    Don't worry check back in a few minutes then you can upvote it!
  • Andrew
    Andrew almost 5 years
    I believe it should begin "network:" not "networks:"
  • willemijns
    willemijns over 4 years
    done.hope this help.