2

Kubuntu allows to setup DNS for a connection.

Each time I connect to a new connection, I have to set up DNS for it.

Is there a way to setup DNS globally?

1 Answers1

1

You haven't indicated which Kubuntu Version you're using. I'm using Kubuntu 22.04 in the instructions below, but this should also apply to 20.04 and newer.

You have a couple options on how to do this, dhclient or nmcli, and I'll let you decide which one you think is easier. With both options, you'll only have to do this once, as these configuration settings will survive a reboot.


Option 1

Use dhclient to define DNS servers and ignore those pushed to you from your DHCP server.

  1. Open Konsole and edit /etc/dhcp/dhclient.conf.

    • Add the following line; adjust accordingly with your DNS servers.

      supersede domain-name-servers 8.8.8.8, 1.1.1.1;
      
    • Remove domain-name-servers after the request keyword.

    • The full output of my file is:

      $ cat /etc/dhcp/dhclient.conf 
      # Configuration file for /sbin/dhclient.
      #
      # This is a sample configuration file for dhclient. See dhclient.conf's
      #       man page for more information about the syntax of this file
      #       and a more comprehensive list of the parameters understood by
      #       dhclient.
      #
      # Normally, if the DHCP server provides reasonable information and does
      #       not leave anything out (like the domain name, for example), then
      #       few changes must be made to this file, if any.
      #
      

      option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;

      supersede domain-name-servers 8.8.8.8, 1.1.1.1;

      send host-name = gethostname(); request subnet-mask, broadcast-address, time-offset, routers, domain-search, host-name, dhcp6.name-servers, dhcp6.domain-search, dhcp6.fqdn, dhcp6.sntp-servers, netbios-name-servers, netbios-scope, interface-mtu, rfc3442-classless-static-routes, ntp-servers;

      timeout 300;

  2. By default, Network Manager uses its internal DHCP client. Therefore, in order to override this and use dhclient, which will acknowledge the changes in /etc/dhcp/dhclient.conf, create a new file called /etc/NetworkManager/conf.d/dhcp-client.conf and add the following to it:

    [main]
    dhcp=dhclient
    
  3. Restart Network Manager

    sudo systemctl restart NetworkManager
    
  4. Release your current DHCP lease:

    sudo dhclient -r
    
  5. Acquire new DHCP lease:

    sudo dhclient
    
  6. Confirm your DNS servers by looking at output of resolvectl:

    $ resolvectl
    Global
           Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
    resolv.conf mode: stub
    

    Link 2 (enp0s3) Current Scopes: DNS Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported Current DNS Server: 8.8.8.8 DNS Servers: 8.8.8.8 1.1.1.1

  7. Reboot and confirm DNS servers again with resolvectl.

  8. You can confirm that Network Manager is using dhclient by running the following command:

    journalctl -b0 | grep dhclient
    

    Then look for a line such as the following:

    Mar 05 20:44:05 Kubuntu-22 NetworkManager[412]: <info>  [1709700245.8453] dhcp-init: Using DHCP client 'dhclient'
    

Option 2

Use nmcli to define DNS servers and ignore those pushed to you from your DHCP server.

  1. Find the "name" of your connection. Open Konsole and enter nmcli connection:

    $ nmcli connection
    NAME                UUID                                  TYPE      DEVICE 
    Wired connection 1  eb3986c1-a90b-30d2-8f1d-1aedde7d9b16  ethernet  enp0s3
    

    In the output above, the connection is named "Wired connection 1". I'll use that in the examples below. Adjust accordingly for your connection name.

  2. Ignore DNS servers pushed to you from your DHCP server:

    nmcli connection modify 'Wired connection 1' ipv4.ignore-auto-dns yes
    
  3. Add DNS servers to your connection:

    nmcli connection modify 'Wired connection 1' ipv4.dns "8.8.8.8,1.1.1.1"
    
  4. Reload your connection:

    sudo nmcli connection reload
    
  5. Restart Network Manager:

    sudo systemctl restart NetworkManager
    

At this point, your DNS servers are set for your connection. Look at the output of resolvectl. Notice that the DNS servers I've defined, 8.8.8.8 and 1.1.1.1, are set for Link 2 (enp0s3), which is associated with "Wired connection 1".

$ resolvectl
Global
       Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
resolv.conf mode: stub

Link 2 (enp0s3) Current Scopes: DNS Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported Current DNS Server: 8.8.8.8 DNS Servers: 8.8.8.8 1.1.1.1

By implementing these changes, a config file is created in /etc/NetworkManager/system-connections/. Take a look at the file created by the above commands:

$ sudo cat /etc/NetworkManager/system-connections/'Wired connection 1.nmconnection' 
[connection]
id=Wired connection 1
uuid=eb3986c1-a90b-30d2-8f1d-1aedde7d9b16
type=ethernet
autoconnect-priority=-999
interface-name=enp0s3
timestamp=1709695374

[ethernet]

[ipv4] dns=8.8.8.8;1.1.1.1; ignore-auto-dns=true method=auto

[ipv6] addr-gen-mode=stable-privacy method=auto

[proxy]

For more information:


mpboden
  • 1,389