问题:
虚拟机无法联网,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 系统上用于表示以太网接口,要注意ens33的ip不能与主机ip一样,但要与VMnet8子网在相同的网段内。
所以我们虚拟机ens33的ip我们可以配置为192.168.140.100。
我们手动配置他。
使用文本编辑器(如 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.140.100 # 你的静态 IP 地址
NETMASK=255.255.255.0 # 你的子网掩码
GATEWAY=192.168.140.2 # 你的网关
DNS1=192.168.140.2 # 可选,你的首选 DNS 服务器,写网关会自动获取
DNS2=192.168.140.2 # 可选,你的备用 DNS 服务器
更改完配置后需保存
重启网络服务
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.140.100 netmask 255.255.255.0 broadcast 192.168.140.255
inet6 fe80::20c:29ff:fe55:2c47 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:55:2c:47 txqueuelen 1000 (Ethernet)
RX packets 10379 bytes 967873 (945.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 12958 bytes 3723748 (3.5 MiB)
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 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.140.100
PING 192.168.140.100 (192.168.140.100) 56(84) bytes of data.
64 bytes from 192.168.140.100: icmp_seq=1 ttl=64 time=0.051 ms
64 bytes from 192.168.140.100: icmp_seq=2 ttl=64 time=0.054 ms
64 bytes from 192.168.140.100: icmp_seq=3 ttl=64 time=0.051 ms
64 bytes from 192.168.140.100: icmp_seq=4 ttl=64 time=0.061 ms
ping一下百度
[artly@localhost ~]$ ping www.baidu.com
PING www.baidu.com (220.181.38.149) 56(84) bytes of data.
64 bytes from www.baidu.com (220.181.38.149): icmp_seq=1 ttl=128 time=33.8 ms
64 bytes from www.baidu.com (220.181.38.149): icmp_seq=2 ttl=128 time=29.2 ms
64 bytes from www.baidu.com (220.181.38.149): icmp_seq=3 ttl=128 time=41.1 ms
64 bytes from www.baidu.com (220.181.38.149): icmp_seq=4 ttl=128 time=28.5 ms
64 bytes from www.baidu.com (220.181.38.149): icmp_seq=5 ttl=128 time=27.1 ms
64 bytes from www.baidu.com (220.181.38.149): icmp_seq=6 ttl=128 time=28.6 ms
主机ping一下虚拟机
shell软件连接一下虚拟机
完美解决问题。