How to change DNS ip address in RHEL

last updated in Categories , , ,

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
See current DNS name server IP address on RHEL

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 www.cyberciti.biz

Ensure that your new DNS settings are working on RHEL
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 www.cyberciti.biz

How to change DNS ip address in RHEL server

Conclusion

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

Posted by: Vivek Gite

The author is the creator of nixCraft 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.

Start the discussion at www.nixcraft.com

Historical Comment Archive

2 comment

  1. To change DNS settings on Linux
    vi /etc/resolv.conf

    nameserver 72.14.188.5
    nameserver 2001:4860:4860::8888
  2. How do I change the IP address, DNS IP and hostname on the command line without rebooting the RHEL 6.x web server? Please help!!!

    Still, have a question? Get help on our forum!