【Linux】虚拟机 CentOS 7 识别不出网卡的解决方案

1,651 阅读7分钟

1. 问题提出

之前在 VMware 安装 CentOS 7(网络连接采取 Nat 方式),进入 CentOS 7 后成功开启了 sshd 服务,然后在主机(我的 Windows 系统)用 puTTY 软件可以 ssh 到虚拟机上。后来在用华为模拟软件 ensp 配置路由器时,因为需用到(虚拟/真实)网卡来连接外网,我在弄的时候不小心将 VMnet8 的配置搞乱了。所以,再次打开虚拟机时,我的 CentOS 7 居然连不上网了!

于是,我一步步排查,终于找到了原因,并解决了识别不出网卡问题。

在这篇博客中,我将完成两件事:

  1. 解决上网问题
  2. 开启 SSHD 服务,让主机 SSH 到虚拟机上(SSH 包括两个部分:服务端的 SSHD 和 SSH 客户端

2. 解决过程

2.1 上网问题

虚拟机网络初始配置

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

img

寻找问题原因

出现不能上网,一般我们会检查是不是 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 的网络配置。

2.2 解决 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 前。我就有了一个大胆的猜想:

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

成功了!这久违的 IP 地址可算出现了!

然后接下来就是使用 SecureCRT 来进行 SSH 连接:

忙活了半天,终于连上了!

📚本文系转载www.cnblogs.com/busui/p/100…