Skip to content
Home » Linux » NameServer Persistency in Linux

NameServer Persistency in Linux

/etc/resolv.conf

When we want to change DNS permanently in Linux, what will you do? I guess you will modify /etc/resolv.conf. But newer versions of Linux appear different. Let's see the following demonstration.

First of all, we shall view /etc/resolv.conf.

[root@localhost ~]# cat /etc/resolv.conf
# Generated by NetworkManager
search example.com
nameserver 168.95.1.1

There is a name server, and we change it into 8.8.8.8.

[root@localhost ~]# vi /etc/resolv.conf
# Generated by NetworkManager
search example.com
nameserver 8.8.8.8

Restart the network service.

[root@localhost ~]# service network restart
Shutting down interface eth0:  Device state: 3 (disconnected)
                                                           [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface eth0:  Active connection state: activating
Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/1
state: activated
Connection activated
                                                           [  OK  ]

Check /etc/resolv.conf

[root@localhost ~]# cat /etc/resolv.conf
# Generated by NetworkManager
search example.com
nameserver 168.95.1.1

The new setting is gone. The configuration is reset to the older one. It seems that manual modification on /etc/resolv.conf will be a temporary effect and not persistent in OS.

Network Adapter

With newer versions of Linux, the resolv.conf will retrieve the static configuration from the primary network interface controller (NIC) as the source after every bounce of network service. Therefore, we should target on the primary NIC configuration.

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
...
DNS1=168.95.1.1
...

You can see there is a name server as same as the name server in /etc/resolv.conf. Next, let's try to modify DNS here instead of /etc/resolv.conf.

[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
...
DNS1=8.8.8.8
DNS2=168.95.1.1
...

Let's restart network service to take it effective.

[root@localhost ~]# service network restart
Shutting down interface eth0:  Device state: 3 (disconnected)
                                                           [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface eth0:  Active connection state: activating
Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/4
state: activated
Connection activated
                                                           [  OK  ]

The content of /etc/resolv.conf is changed and consistent with new DNS in the primary NIC configuration.

[root@localhost ~]# cat /etc/resolv.conf
# Generated by NetworkManager
search example.com
nameserver 8.8.8.8
nameserver 168.95.1.1

Leave a Reply

Your email address will not be published. Required fields are marked *