By default, for an individual, when he connects to the Internet, the DNS server addresses provided in DHCP are those of his Internet service provider. But it is possible to choose others. Here is how to change DNS server in Ubuntu (18.04 and 20.04).
Prerequisites
Have Ubuntu 16.04 or higher
Introduction to DNS
What is a DNS server?
DNS is the abbreviation for Domain Name System. A DNS server is a system whose purpose is to convert an Internet address (www.domainname.ext) into an IP address (physical address of machines connected to a network). This is called domain name resolution.
So, for example when you type in your browser www.wikipedia.fr, the DNS server converts the path to the IP address: 78.109.84.114.
You can find out this IP address by pinging the domain. For example :
ping wikipedia.fr
and under Linux you will have an answer of this type
PING wikipedia.fr (78.109.84.114) 56 (84) bytes of data.
64 bytes from wikimedia2.typhon.net (78.109.84.114): icmp_seq = 1 ttl = 51 time = 23.7 ms
64 bytes from wikimedia2.typhon.net (78.109.84.114): icmp_seq = 2 ttl = 51 time = 23.7 ms
64 bytes from wikimedia2.typhon.net (78.109.84.114): icmp_seq = 3 ttl = 51 time = 23.4 ms
64 bytes from wikimedia2.typhon.net (78.109.84.114): icmp_seq = 4 ttl = 51 time = 23.7 ms
64 bytes from wikimedia2.typhon.net (78.109.84.114): icmp_seq = 5 ttl = 51 time = 23.5 ms
We also speak of primary DNS server and secondary DNS server when two DNS server addresses are entered in your network configuration. The second is used to take over from the first when the latter does not respond.
Why change DNS?
By default, using DHCP, a computer that is connected to the network automatically receives a network configuration that includes an IP address, netmask, gateway, and DNS server address.
For an individual, the DNS server addresses retrieved are generally those of the DNS servers of their Internet service provider (ISP). However, your ISP may have (court decision) or decide to filter (censor) certain addresses, be down or not be very efficient in responding to your name resolution requests. Additionally, some unscrupulous ISPs might decide to spy on you by tracking and logging all of your DNS queries.
But, there are alternatives to ISP DNS: public DNS servers.
For example there are those from Google. Certainly renowned for their speed, but with what guarantee for your privacy?
IPv4: 8.8.8.8 and 8.8.4.4
IPv6: 2001: 4860: 4860 :: 8888 and 2001: 4860: 4860 :: 8844
or those offered by Cloudflare, launched in April 2018, which also promise speed and respect for privacy.
IPv4: 1.1.1.1 and 1.0.0.1
IPv6: 2606: 4700: 4700 :: 1111 and 2606: 4700: 4700 :: 1001
There are also those of OpenDNS and many others, some of which are managed by non-profit associations. For example FDN (French) or quad9 (easy to remember: 9.9.9.9).
On the other hand, I advise you to be careful with your choice of DNS because some may redirect you to malicious sites ...
Finally, it is possible to remain in DHCP for its IP configuration while using custom DNS servers.
Change DNS in Ubuntu Desktop and its variants
Ubuntu Desktop and its variants (Xubuntu, Kubuntu, Lubuntu,…) use NetworkManager by default to manage network connections. NetworkManager can be configured directly from the graphical interface or in a terminal.
For Ubuntu Server (> = 18.04), the configuration is done with Netplan and a YAML description file (not mentioned in this tutorial which is for the desktop versions).
Change DNS in Ubuntu through the GUI
For Ubuntu
Open the network settings in Ubuntu by going to Settings> Network. Then click on the Change connection icon (1) of the network card you want to configure.
Go to the IPv4 and / or IPv6 tab.
Disable automatic DNS management (1) then add the IP addresses of the DNS servers (2). If you want to enter more than one, separate them with a comma. And apply the changes (3).
So disconnect and reconnect your network interface to apply the settings.
For Xubuntu, Lubuntu, Kubuntu ...
Open the network settings in Ubuntu variants by clicking on the network or wifi icon in the status bar.
Select the network card to configure (1) and click Modify connection (2).
Go to the IPv4 and / or IPv6 (1) tab. Select Automatic (Addresses only) (2) to keep your IP address automatically assigned by DHCP, then enter the IP address of the DNS servers (3). If you want more than one, separate them with a comma. And Save the modifications (4).
Finally, disconnect and reconnect your network interface to apply the settings.
Change DNS in Ubuntu from command line
Open a terminal (Ctrl + Alt + T), then do:
nmtui
Choose edit a network connection.
Select your network connection and press Enter to change it.
Display the IP configuration (v4 and / or v6).
Add the IP address of the DNS servers (1). You can add more than one. And check Ignore automatically obtained DNS settings (2).
Go down to validate.
Finally to exit, do ESC a few times until you return to the command prompt.
And to apply your settings, restart your network connection by disabling it:
nmcli con down id "connection name"
and reactivating it:
nmcli con up id "connection name"
to find out the name of the connection, do:
nmcli con show
Also no need to type it entirely, just start typing its beginning and press Tab ↹ to complete it automatically
Otherwise, instead but more drastically, you can restart the NetworkManager service:
sudo service network-manager restart
Test your DNS
You can do a test on the Internet using the DNSLeak site which tests for DNS leaks and returns the name of the DNS provider to you.

Commentaires
Enregistrer un commentaire