How to change DNS ip address in RHEL

I am a new RHEL 5.x/6.x/7.x sysadmin. How do I change DNS IP address in RHEL using the CLI? What is the procedure to change the DNS IP from 8.8.8.8 to 192.168.2.254 in Red Hat Enterprise Linux desktop or server?

Introduction – DNS is an acronym for Domain Name Server. It is possible that you set up wrong DNS ip address in RHEL. This page shows how to change your RHEL server’s DNS IP address settings from the CLI.

Procedure to change DNS ip address in RHEL

  1. Edit the /etc/resolv.conf file with an editor, such as nano or vim in RHEL: sudo vim /etc/resolv.conf
  2. Set the name servers (DNS IP) that you want to use on RHEL : nameserver 192.168.2.254
  3. Save and close the file in RHEL
  4. Test new settings

How to see the current DNS settings in RHEL

Type the following cat command:
cat /etc/resolv.conf

RHEL change DNS ip address

Type the following vim command:
$ sudo vim /etc/resolv.conf
OR
# vim /etc/resolv.conf
Edit/update nameserver entry as follows (you can set maximum 3 nameserver IP address):

## Change DNS settings on RHEL (Red Hat Enterprise Linux) ##
nameserver 192.168.2.254
nameserver 192.168.2.18

Test it using the ping command or host command/dig command:
ping -c 4 www.nixcraft.com
host www.nixcraft.com
dig sxi.io

Changed DNS settings on RHEL? Test new DNS settings with ping, host and dig commands

How to change DNS settings on RHEL using Network Manager

Simply run the following nmcli command to connection names:
$ sudo nmcli connection
To set up DNS IP address to 192.168.2.254 and 192.168.2.18, run:
sudo nmcli con mod {connectionNameHere} ipv4.dns "192.168.2.254 192.168.2.18"
In this example just set up DNS IP to 192.168.2.254 from the CLI on RHEL:
sudo nmcli con mod eth0 ipv4.dns "192.168.2.254"
Reload new DNS settings by running any one of the following command:
sudo systemctl restart NetworkManager.service
OR
sudo nmcli connection reload
Verify it by using the grep command and cat command and host command:
cat /etc/resolv.conf
grep DNS /etc/sysconfig/network-scripts/ifcfg-Ethernet_connection_1
host sxi.io

Conclusion

This page described how to change your RHEL server’s DNS IP settings using the CLI.

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.