How to Change DNS Settings on Linux

This post will guide you how to change your Linux server’s DNS settings. How do I change DNS IP Address from the command line in the Linux operating systems.

change dns settings linux1

Changing DNS Settings


If you want to change the DNS IP Address of your current Linux Server, and you need to modify the configuration file of DNS server located in /etc/resolv.conf. And this file should contain at least one nameserver line. And each nameserver line defines a DNS server. You just need to replace the IP address of the current nameserver with another IP address that you want to use. So do the following steps:

#1 open the configuration file called /etc/resolv.conf with your vim text editor, type:

$ sudo vim /etc/resolv.conf

#2 replace or add the following lines into this file.

nameserver dns_ip_address

#3 save and close the file.

devops@devops:~$ cat /etc/resolv.conf
# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "systemd-resolve --status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver 127.0.0.53
options edns0

Then you can verify it by issuing the following command:

$ dig yahoo.com

outputs:

devops@devops:~$ dig yahoo.com

; <<>> DiG 9.11.3-1ubuntu1.8-Ubuntu <<>> yahoo.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 49490
;; flags: qr rd ra; QUERY: 1, ANSWER: 6, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;yahoo.com. IN A

;; ANSWER SECTION:
yahoo.com. 1323 IN A 98.138.219.231
yahoo.com. 1323 IN A 98.137.246.7
yahoo.com. 1323 IN A 98.138.219.232
yahoo.com. 1323 IN A 98.137.246.8
yahoo.com. 1323 IN A 72.30.35.10
yahoo.com. 1323 IN A 72.30.35.9

;; Query time: 22 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Fri Sep 13 07:17:30 EDT 2019
;; MSG SIZE rcvd: 134

Conclusion


You should know that how to change DNS settings from the command line in a CentOS or RHEL or Ubuntu Linux server.

You might also like:

Sidebar



back to top