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 

No comments:

Post a Comment