Showing posts with label systemd-resolved. Show all posts
Showing posts with label systemd-resolved. Show all posts

Wednesday, January 11, 2023

Change DNS server using resolvectl

For systemd based system, the configuration of dns resolution is controlled by a service called systemd-resolved.service.

In order to change the dns server that the machine refers to for any DNS request, we can use a command called resolvectl.

To set the current dns server in interface wlp4s0 to 1.1.1.1, use this command
sudo resolvectl dns wlp4s0 1.1.1.1

To set the dns server in interface wlp4s0 to 1.1.1.1 and 8.8.8.8, use this command
sudo resolvectl dns wlp4s0 1.1.1.1 8.8.8.8

We can specify as many dns servers as we like. Just separate them with spaces.

To check our currently set dns server for interface wlps4s0, use this command
resolvectl dns wlp4s0

We can see dns settings for all interfaces in the system by running
resolvectl status

We can also query (get the ip address from dns) record using resolvectl, just like below
resolvectl query www.linuxwave.info

To query different type of dns record, we can add the -t flag. This is an example if we want to query an MX record for gmail.com
resolvectl query -t MX gmail.com

We can also do reverse query, by specifying ip address instead of domain name

resolvectl query 142.250.191.51 

Saturday, June 26, 2021

Adding custom nameserver in systemd-resolve

The old /etc/resolve is now being managed by systemd-resolve service, which is part of systemd. In order to add new nameserver, please follow below steps


1. Create a directory named /etc/systemd/resolved.conf.d/

# mkdir /etc/systemd/resolved.conf.d


2. Add a new configuration file for your new dns server. Let's say we want to add google's dns ip address, which are 8.8.8.8 and 8.8.4.4 

# cat >> /etc/systemd/resolved.conf.d/mynameserver.conf <<EOF

[Resolve]

DNS=8.8.8.8 8.8.4.4

EOF


3. Restart the service

# systemctl restart systemd-resolved


4. Verify that your dns is now being used by the system

# systemd-resolve --status

Global

       LLMNR setting: no                  

MulticastDNS setting: no                  

  DNSOverTLS setting: no                  

      DNSSEC setting: no                  

    DNSSEC supported: no                  

         DNS Servers: 8.8.8.8             

                                8.8.4.4             

...


For more information about what option can be included in the configuration file, please refer to resolved.conf man page.