虚拟机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.
我去红帽官⽅查看了⼀下,到了⼀篇。我就简单说⼀下什么是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/-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成了。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论