问题:
虚拟机无法联网,ping不通
[artly@localhost ~]$ ping 192.168.100.15
connect: Network is unreachable
同时,我的端口没有ens33。
[artly@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 200 bytes 17344 (16.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 200 bytes 17344 (16.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0virbr0: 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:0b:b9:cd 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
解决:
VMnet1和VMnet8确保拥有
首先我们需要确保证安装完虚拟机后主机有VMnet1和VMnet8,这是我们可以连上网的基础。
如果没有的话,打开虚拟网络编辑器
选择还原默认设置
配置网络接口
输入ifconfig
[artly@localhost ~]$ ifconfig
可以发现,我们少一个ens33,它通常在 Linux 系统上用于表示以太网接口。
我们需要手动配置他。
使用文本编辑器(如 nano
或 vi
)打开 /etc/sysconfig/network-scripts/ifcfg-ens33
文件:这里我们选择使用nano。
sudo nano /etc/sysconfig/network-scripts/ifcfg-ens33
[artly@localhost ~]$ sudo nano /etc/sysconfig/network-scripts/ifcfg-ens33
[sudo] password for artly:
输入密码按回车后进入编辑界面
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=db85cc07-820b-486e-9c5d-24775ec69557
DEVICE=ens33
ONBOOT=yes # no改为yes
IPADDR=192.168.1.100 # 你的静态 IP 地址
NETMASK=255.255.255.0 # 你的子网掩码
GATEWAY=192.168.1.1 # 你的网关
DNS1=8.8.8.8 # 可选,你的首选 DNS 服务器
DNS2=8.8.4.4 # 可选,你的备用 DNS 服务器
如果你对你电脑的ip,子网掩码不清楚的话
可右击电脑右下角网络标志,打开网络设置,点击属性可查看
更改完配置后需保存
重启网络服务
sudo systemctl restart network
[artly@localhost ~]$ systemctl restart network
加载可能需要一点时间,等待一下就好。
验证
再次输入ifconfig,会发现ens33出现了,而且还带着ipv4地址
[artly@localhost ~]$ ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.8.100 netmask 255.255.255.0 broadcast 192.168.8.255
inet6 fe80::20c:29ff:fe55:2c47 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:55:2c:47 txqueuelen 1000 (Ethernet)
RX packets 396 bytes 24318 (23.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 56 bytes 5854 (5.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: 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 464 bytes 40416 (39.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 464 bytes 40416 (39.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0virbr0: 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:0b:b9:cd 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
ping一下自己
[artly@localhost ~]$ ping 192.168.8.100
PING 192.168.8.100 (192.168.8.100) 56(84) bytes of data.
64 bytes from 192.168.8.100: icmp_seq=1 ttl=64 time=0.048 ms
64 bytes from 192.168.8.100: icmp_seq=2 ttl=64 time=0.053 ms
64 bytes from 192.168.8.100: icmp_seq=3 ttl=64 time=0.051 ms
64 bytes from 192.168.8.100: icmp_seq=4 ttl=64 time=0.132 ms
64 bytes from 192.168.8.100: icmp_seq=5 ttl=64 time=0.056 ms
64 bytes from 192.168.8.100: icmp_seq=6 ttl=64 time=0.055 ms
完美解决问题。