本机环境:
系统:Centos 网络:NAT
虚拟机之前一直都可以连外网,但最近不能连了,现状如下:
[root@vhost03 ~]# ping baidu.com
ping: unknown host baidu.com
查看了下本机的网卡配置:
[root@vhost03 ~]# more /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
BOOTPROTO="static"
HWADDR="00:0C:29:D7:D2:49"
IPV6INIT="yes"
NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE="Ethernet"
UUID="f08dcf57-d670-46b3-8298-240cd0b76bc2"
IPADDR=192.168.196.129
BROADCAST=192.168.196.255
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
新装的虚拟机默认都是dhcp方式获取IP。为了保证IP固定,自己更新了网卡信息,使用了static模式。其中最后四行是按照dhcp模式时,使用ifconfig命令获取的信息。
查看DNS信息:
[root@vhost03 ~]# more /etc/resolv.conf
# Generated by NetworkManager
# 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
上述结果表示并没有配置DNS。
正确的解决方式:
[root@vhost03 ~]# cd /etc/sysconfig/network-scripts/ [root@vhost03 network-scripts]# cp ifcfg-eth0 ifcfg-eth0.bak
[root@vhost03 network-scripts]# vi ifcfg-eth0 DEVICE="eth0"
BOOTPROTO="dhcp"
HWADDR="00:0C:29:D7:D2:49"
IPV6INIT="yes"
NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE="Ethernet" [root@vhost03 Desktop]# service network restart [root@vhost03 Desktop]# ifconfig
eth0 Link encap:Ethernet HWaddr :0C::D7:D2:
inet addr:192.168.196.135 Bcast:192.168.196.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fed7:d249/ Scope:Link
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (140.3 MiB) TX bytes: (3.4 MiB) lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::/ Scope:Host
UP LOOPBACK RUNNING MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (1.0 KiB) TX bytes: (1.0 KiB) [root@vhost02 ~]# netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
192.168.196.0 0.0.0.0 255.255.255.0 U eth0
169.254.0.0 0.0.0.0 255.255.0.0 U eth0
0.0.0.0 192.168.196.2 0.0.0.0 UG eth0 [root@vhost03 Desktop]# more /etc/resolv.conf
# Generated by NetworkManager
domain localdomain
search localdomain
nameserver 192.168.196.2
根据上面的信息重新配置ifcfg-eth0文件
[root@vhost03 ~]# cd /etc/sysconfig/network-scripts/ [root@vhost03 network-scripts]# vi ifcfg-eth0 DEVICE="eth0"
BOOTPROTO="static"
HWADDR="00:0C:29:D7:D2:49"
IPV6INIT="yes"
NM_CONTROLLED="yes" ONBOOT="yes"
TYPE="Ethernet"
UUID="f08dcf57-d670-46b3-8298-240cd0b76bc2"
IPADDR=192.168.196.129
BROADCAST=192.168.196.255
NETMASK=255.255.255.0
GATEWAY=192.168.196.2 [root@vhost03 ~]# cd /etc/sysconfig/network-scripts/ [root@vhost03 network-scripts]# service network restart [root@vhost03 network-scripts]# vi /etc/resolv.conf # Generated by NetworkManager
domain localdomain
search localdomain
nameserver 192.168.196.2 [root@vhost03 ~]# ping www.baidu.com
PING www.a.shifen.com (115.239.210.27) () bytes of data.
bytes from 115.239.210.27: icmp_seq= ttl= time=6.46 ms
bytes from 115.239.210.27: icmp_seq= ttl= time=7.00 ms
ok,问题解决。分析下来,看来是之前获取的历史网卡信息变了,导致不能ping外网。
之前有在其它网络环境下重启过宿主机,应该和这个有关。解决问题的时候发现,只要重启网卡就会出现/etc/resolv.conf 被重置的现象。这是因为
[root@vhost02 ~]# chkconfig --list |grep -i network
NetworkManager 0:off 1:off 2:on 3:on 4:on 5:on 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@vhost02 ~]# service NetworkManager stop
NetworkManager进程会实时修改/etc/resolv.conf 中的信息