Linux – How to override a vm DNS servers

dnslinuxnetwork-interfacenetworkmanagervirtual machine

I'm running a CentOS 7 Virtual machine and it's picking up the DNS nameservers from my Fedora 27 system via DHCP I believe. I am trying to change the DNS nameserver in /etc/resolv.conf to google name servers. It's different on my fedora 27, when I do that and do systemctl restart network it goes back to the same nameservers on the fedora 27.

Is there a way to override that?

I believe I have to do something in /etc/sysconfig/network-scripts/

I am connected via ethernet enp0s3 on the vm and of course bridged on virbr0

Best Answer

The network interface of the VM is configured to accept DNS from dhcp only. Change the interface settings.

Network interface configuration files are in /etc/sysconfig/network-scripts/.

Say it's a kvm vps and the network interface is eth0. Edit the file /etc/sysconfig/network-scripts/ifconfig-eth0 and add the following for Google's IPv4 nameserver

PEERDNS=no
DNS1=8.8.8.8
DNS2=8.8.4.4

Then restart networking as follows

sudo service network restart

You may use any other nameserver.

The above mentioned nameservers will be added in resolve.conf and PEERDNS=no directive will stop dhcp from altering nameserver.

Another way out is making resolv.conf read only even for root as follows

chattr +i /etc/resolv.conf

If you want to edit the file latter use chattr -i /etc/resolv.conf before editing.

P.S. PEERDNS=no works only with RHEL based system including RHEL, CentOS and Fedora.

Related Question