虚拟机centos7 识别不出网卡的解决方案

问题提出

之前在VMware安装centos 7(网络连接采取Nat方式),进入Centos 7后成功开启了sshd服务,然后在主机(我的windows系统)用puTTY软件可以ssh到虚拟机上。后来在用华为模拟软件ensp配置路由器时,因为需用到(虚拟/真实)网卡来连接外网,我在弄的时候不小心将VMnet8的配置搞乱了。所以,再次打开虚拟机时,我的Centos居然连不上网了!
于是,我一步步排查,终于找到了原因,并解决了识别不出网卡问题。
在这篇博客中,我将完成两件事:

  1. 解决上网问题;
  2. 开启sshd服务,让主机ssh到虚拟机上。

解决上网问题

虚拟机网络初始配置

我的虚拟机是采取NAT方式联网的。注意:如果你是使用Host-only或者桥接方式,这篇文章可能只能给你提供一个思路。

寻找问题原因

出现不能上网,一般我们会检查是不是VMware本身的网卡是不是有问题。我检查了几遍,并且另外安装一个centos 7系统,发现能上网。所以100%肯定是我这个Centos 7系统网络设置有问题!所以我先查看一下电脑有没有获取到正确的ip。于是,首先想到的命令是:ifconfig:

[root@localhost ~]# ifconfig
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 448  bytes 38976 (38.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 448  bytes 38976 (38.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:11:c6:c2  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

上面列出的lo(loopback)网卡是用于环回地址的。virbr0是网桥的网卡。而我们需要的是与VMnet8对应的那张网卡。
下图是在windows打开网络设置页面看到的。其中有两张虚拟网卡:

  1. VMnet1对应仅主机模式;
  2. VMnet8对应Nat模式,也就是我们在Centos 7中需要的与之对应的网卡

很明显,那张网卡还没开启。于是,我给ifconfig加一个参数-a:

[root@localhost ~]# ifconfig -a
ens33: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether 00:0c:29:bd:21:c3  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 448  bytes 38976 (38.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 448  bytes 38976 (38.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:11:c6:c2  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0-nic: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether 52:54:00:11:c6:c2  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

从输出中,我很快锁定了一张网卡:ens33。但我要测试到底是不是这张网卡。一般网卡的相关文件都是放在/etc/sysconfig/network-scripts/下:

[root@localhost ~]# ls -al /etc/sysconfig/network-scripts/ | grep ens33
-rw-r--r--. 1 root root   280 Nov 26 15:36 ifcfg-ens33

果然,就是它!!!先把它开启,然后在输入ifconfig命令查看:

[root@localhost ~]# ifconfig ens33 up
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::20c:29ff:febd:21c3  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:bd:21:c3  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7  bytes 578 (578.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 460  bytes 39900 (38.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 460  bytes 39900 (38.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:11:c6:c2  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

开启之后,发现ens33这张网卡并没有获取到ip地址,网关等相关配置。到此,我们已经知道根本原因,就是Centos 7的网卡没有获取到ip!但现在ens33网卡已经开启,我想重启一下网络,看看能不能获取ip地址:

[root@localhost ~]# service network restart
Restarting network (via systemctl):  Job for network.service failed because the control process exited with error code. See "systemctl status network.service" and "journalctl -xe" for details.                                                          [FAILED]

果然,还是失败了。我就想,为什么前几天安装得Centos系统不能上网,而现在安装的却能上网呢?中间只发生过一件可能影响网络的事:就是我更改过VMnet8的网络配置。


解决ip分配问题

经过搜索和排查,我发现在Centos系统上,管理网络的有两个工具:

  • network;
  • NetworkManager.

我去红帽官方查看了一下,找到了一篇post。我就简单说一下什么是network和NetworkManager:

  1. network: Network initscript是一个基本的网络接口start/stop框架,它是initscripts包的一部分;
  2. NetworkManager: NetworkManager是一套协作网络管理工具,旨在消除手动编辑网络配置文件的繁琐,使网络简单直接。 NetworkManager提供灵活的管理界面,包括GUI,CLI和TUI选项,用于管理本地,远程连接等等。
  3. 在系统启动过程中,NetworkManager的优先级大于network。也就是说NetworkManager先启动。

稍微总结一下,就是说NetworkManager比network更先进,启动顺序也在network前。我就有了一个大胆的猜想(注意,下面的一段话只是个人YY,请批判性阅读):

由于我们是虚拟机环境,外面的虚拟网卡已经有能力通过dhcp等协议将网络ip等必要东西分配好,而且networkManager是新的东西,可能在虚拟机上兼容不太好。你平时用不影响,但一旦你更改了VMnet8等相关虚拟网卡,NetworkManager可能识别不出,就无法分配到ip,而且由于它先启动,然后它挂了,但它还在占用这个分配ip的功能,进而network无法顶替,也就是冲突了。

接下来,我要关闭NetworkManager:

[root@localhost ~]# systemctl stop NetworkManager    //停止NetworkManager
[root@localhost ~]# systemctl disable NetworkManager  //进制开机启动
Removed symlink /etc/systemd/system/multi-user.target.wants/NetworkManager.service.
Removed symlink /etc/systemd/system/dbus-org.freedesktop.NetworkManager.service.
Removed symlink /etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service.

此时,你会发现你电脑的网络GUI不见了(因为你关闭了NetworkManager):

做完这些,输入ifconfig看一下:

[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.43.129  netmask 255.255.255.0  broadcast 192.168.43.255
        inet6 fe80::20c:29ff:febd:21c3  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:bd:21:c3  txqueuelen 1000  (Ethernet)
        RX packets 261  bytes 16686 (16.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 28  bytes 4549 (4.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Bingo!!!成了。

posted @ 2018-11-26 18:17  busui  阅读(19489)  评论(4编辑  收藏  举报