1. 程式人生 > >Linux系統resolv.conf配置資訊丟失的解決方法

Linux系統resolv.conf配置資訊丟失的解決方法

我要配置DNS,修改/etc/resolv.conf,修改後重啟服務 service network restart ,修改/etc/resolv.conf的資訊丟失,請大家看看。[code]修改前的配置

# Generated by NetworkManager
search localhost


# No nameservers found; try putting DNS servers into your
# ifcfg files in /etc/sysconfig/network-scripts like so:
#
# DNS1=xxx.xxx.xxx.xxx
# DNS2=xxx.xxx.xxx.xxx
# DOMAIN=lab.foo.com bar.foo.com

方法一:

在/etc/sysconfig/network-scripts/ifcfg-eth0檔案中直接加入DNS1=xxx.xxx.xxx.xxx,再service network restart,例如:

DEVICE="eth0"
BOOTPROTO="static"
HWADDR="00:0C:29:B5:E4:65"
NM_CONTROLLED="yes"
ONBOOT="yes"
IPADDR=192.168.128.133
NETMASK=255.255.255.0
GATEWAY=192.168.128.1
DNS1=222.46.120.6

重啟後再來看看resolv.conf,多了一行 namerserver xxx.xxx.xxx.xx
nameserver 222.46.120.6

方法二:

vim /etc/resolvconf/resolv.conf.d/head 檔案
顯示與resolv.conf相同的內容:
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN)
在最後鍵入nameserver 202.102.152.3
儲存退出,
resolvconf -u
此時就可以正常上網了,重啟後不用在重新設定DNS了。

方法三:

resolv.conf開頭的第一句說明resolv.conf這個檔案是由NetworkManager這個程式生成的(對應的是network-manager服務),那麼解決辦法也就來了:我們關掉network-manager即可,命令為:sudo service network-manager stop或者sudo /etc/init.d/network-manager stop,但是這種方法不是一勞永逸的,因為每次重啟系統後還是會自動啟動這個服務,因此我們需要完全禁止network-manager啟動即可。編輯network manager的配置檔案,使用命令:sudo vi /etc/init/network-manager.conf。註釋掉其中的start on部分即可。內容如下:

# network-manager - network connection manager
#
# The Network Manager daemon manages the system's network connections,
# automatically switching between the best available.

description     "network connection manager"

#start on (local-filesystems
#         and started dbus
#         and static-network-up)
stop on stopping dbus

expect fork
respawn

script
        # set $LANG so that messages appearing on the GUI will be translated. See LP: 875017
        if [ -r /etc/default/locale ]; then
                . /etc/default/locale
                export LANG LANGUAGE LC_MESSAGES LC_ALL
        fi

方法四:

前面提到固定IP的上網方式主要是修改/etc/network/interfaces這個檔案,配置IP、閘道器什麼的,其實這裡面還有個引數可以配置,那就是DNS了,對應的引數名為dns-nameservers,這裡設定的優先順序比resolv.conf高,也就是網路會從這裡讀取DNS配置,如果沒配置才去看resolv.conf裡面的設定,因此在這裡面配置DNS更簡單。在其後追加

dns-nameservers xxx.xxx.xxx.xxx