linux虚拟机如何连接外网
1、首先利用vmware快照功能将虚拟机还原至初始状态(前提是备份好快照,或者重新安装虚拟机)
2、查看初始的网络模式、网卡信息、连接状态
从虚拟设置中查询,当前的网络模式为仅主机模式:
查看网卡信息:
[root@linuxprobe Desktop]# cd /etc/sysconfig/network-scripts/ ## 进入网卡配置文件所在目录
[root@linuxprobe network-scripts]# ls ## 列出目录内的内容
ifcfg-eno16777728 ifdown-ipv6 ifdown-Team ifup-eth ifup-post ifup-tunnel
ifcfg-lo ifdown-isdn ifdown-TeamPort ifup-ippp ifup-ppp ifup-wireless
ifdown ifdown-post ifdown-tunnel ifup-ipv6 ifup-routes init.ipv6-global
ifdown-bnep ifdown-ppp ifup ifup-isdn ifup-sit network-functions
ifdown-eth ifdown-routes ifup-aliases ifup-plip ifup-Team network-functions-ipv6
ifdown-ippp ifdown-sit ifup-bnep ifup-plusb ifup-TeamPort
[root@linuxprobe network-scripts]# ifconfig ## 查看网卡信息,没有IP信息
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:e4:f7:b9 txqueuelen 1000 (Ethernet)
RX packets 3 bytes 276 (276.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 0 (Local Loopback)
RX packets 642 bytes 51980 (50.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 642 bytes 51980 (50.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
查看网络连接是否开启:关闭状态
测试一下与外网的连接:
[root@linuxprobe network-scripts]# ping -c 3 www.baidu.com ## 无法与外网连接
ping: unknown host www.baidu.com
2、 将网络模式调为桥接模式,查看网卡、IP信息
[root@linuxprobe network-scripts]# ls ## 列出网卡配置文件目录内容
ifcfg-eno16777728 ifdown-isdn ifdown-tunnel ifup-isdn ifup-Team
ifcfg-lo ifdown-post ifup ifup-plip ifup-TeamPort
ifdown ifdown-ppp ifup-aliases ifup-plusb ifup-tunnel
ifdown-bnep ifdown-routes ifup-bnep ifup-post ifup-wireless
ifdown-eth ifdown-sit ifup-eth ifup-ppp init.ipv6-global
ifdown-ippp ifdown-Team ifup-ippp ifup-routes network-functions
ifdown-ipv6 ifdown-TeamPort ifup-ipv6 ifup-sit network-functions-ipv6
[root@linuxprobe network-scripts]# ifconfig ## 查看网卡信息,没有IP信息
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:e4:f7:b9 txqueuelen 1000 (Ethernet)
RX packets 4 bytes 519 (519.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 0 (Local Loopback)
RX packets 778 bytes 62948 (61.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 778 bytes 62948 (61.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@linuxprobe network-scripts]# ping -c 3 www.baidu.com ## 测试与外网的联通性,不能联通
ping: unknown host www.baidu.com
3、将网络开关打开,查看网卡、IP、测试外网连接
[root@linuxprobe network-scripts]# ls ## 列出网卡配置文件目录下文件,多出一个配置文件
ifcfg-Auto_Ethernet ifdown-isdn ifup ifup-plusb ifup-wireless
ifcfg-eno16777728 ifdown-post ifup-aliases ifup-post init.ipv6-global
ifcfg-lo ifdown-ppp ifup-bnep ifup-ppp network-functions
ifdown ifdown-routes ifup-eth ifup-routes network-functions-ipv6
ifdown-bnep ifdown-sit ifup-ippp ifup-sit
ifdown-eth ifdown-Team ifup-ipv6 ifup-Team
ifdown-ippp ifdown-TeamPort ifup-isdn ifup-TeamPort
ifdown-ipv6 ifdown-tunnel ifup-plip ifup-tunnel
[root@linuxprobe network-scripts]# ll -h
total 216K
-rw-r--r--. 1 root root 298 Nov 3 11:07 ifcfg-Auto_Ethernet
-rw-r--r--. 1 root root 293 Nov 1 23:38 ifcfg-eno16777728
-rw-r--r--. 1 root root 254 Apr 2 2014 ifcfg-lo
lrwxrwxrwx. 1 root root 24 Nov 1 23:35 ifdown -> ../../../usr/sbin/ifdown
-rwxr-xr-x. 1 root root 627 Apr 2 2014 ifdown-bnep
-rwxr-xr-x. 1 root root 5.5K Apr 2 2014 ifdown-eth
-rwxr-xr-x. 1 root root 781 Apr 2 2014 ifdown-ippp
-rwxr-xr-x. 1 root root 4.1K Apr 2 2014 ifdown-ipv6
lrwxrwxrwx. 1 root root 11 Nov 1 23:35 ifdown-isdn -> ifdown-ippp
-rwxr-xr-x. 1 root root 1.7K Apr 2 2014 ifdown-post
-rwxr-xr-x. 1 root root 1.1K Apr 2 2014 ifdown-ppp
-rwxr-xr-x. 1 root root 837 Apr 2 2014 ifdown-routes
-rwxr-xr-x. 1 root root 1.5K Apr 2 2014 ifdown-sit
-rwxr-xr-x. 1 root root 1.5K Mar 31 2014 ifdown-Team
-rwxr-xr-x. 1 root root 1.5K Mar 31 2014 ifdown-TeamPort
-rwxr-xr-x. 1 root root 1.5K Apr 2 2014 ifdown-tunnel
lrwxrwxrwx. 1 root root 22 Nov 1 23:35 ifup -> ../../../usr/sbin/ifup
-rwxr-xr-x. 1 root root 13K Apr 2 2014 ifup-aliases
-rwxr-xr-x. 1 root root 859 Apr 2 2014 ifup-bnep
-rwxr-xr-x. 1 root root 10K Apr 2 2014 ifup-eth
-rwxr-xr-x. 1 root root 12K Apr 2 2014 ifup-ippp
-rwxr-xr-x. 1 root root 11K Apr 2 2014 ifup-ipv6
lrwxrwxrwx. 1 root root 9 Nov 1 23:35 ifup-isdn -> ifup-ippp
-rwxr-xr-x. 1 root root 642 Apr 2 2014 ifup-plip
-rwxr-xr-x. 1 root root 1.1K Apr 2 2014 ifup-plusb
-rwxr-xr-x. 1 root root 2.6K Apr 2 2014 ifup-post
-rwxr-xr-x. 1 root root 4.1K Apr 2 2014 ifup-ppp
-rwxr-xr-x. 1 root root 1.9K Apr 2 2014 ifup-routes
-rwxr-xr-x. 1 root root 3.2K Apr 2 2014 ifup-sit
-rwxr-xr-x. 1 root root 1.6K Oct 31 2013 ifup-Team
-rwxr-xr-x. 1 root root 1.9K Mar 31 2014 ifup-TeamPort
-rwxr-xr-x. 1 root root 2.6K Apr 2 2014 ifup-tunnel
-rwxr-xr-x. 1 root root 1.6K Apr 2 2014 ifup-wireless
-rwxr-xr-x. 1 root root 4.6K Apr 2 2014 init.ipv6-global
-rw-r--r--. 1 root root 14K Apr 2 2014 network-functions
-rw-r--r--. 1 root root 26K Apr 2 2014 network-functions-ipv6
[root@linuxprobe network-scripts]# date
Tue Nov 3 11:10:44 CST 2020
[root@linuxprobe network-scripts]# cat ifcfg-Auto_Ethernet ## 查看多出的文件(自动配置文件)
HWADDR=00:0C:29:E4:F7:B9
TYPE=Ethernet
BOOTPROTO=dhcp ## 网络分配方式为DHCP
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME="Auto Ethernet"
UUID=3590e81f-7a05-443a-8863-0f9a768fdd40
ONBOOT=yes ## 开机自动重启网卡
[root@linuxprobe network-scripts]# ifconfig ## 查看网卡信息,已经有IP信息
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.3.7 netmask 255.255.255.0 broadcast 192.168.3.255
inet6 fe80::20c:29ff:fee4:f7b9 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:e4:f7:b9 txqueuelen 1000 (Ethernet)
RX packets 30 bytes 3740 (3.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 63 bytes 7635 (7.4 KiB)
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 0 (Local Loopback)
RX packets 790 bytes 63968 (62.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 790 bytes 63968 (62.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@linuxprobe network-scripts]# ping -c 3 192.168.3.7 ## 本地主机测试测试网络联通性,联通
PING 192.168.3.7 (192.168.3.7) 56(84) bytes of data.
64 bytes from 192.168.3.7: icmp_seq=1 ttl=64 time=0.052 ms
64 bytes from 192.168.3.7: icmp_seq=2 ttl=64 time=0.040 ms
64 bytes from 192.168.3.7: icmp_seq=3 ttl=64 time=0.044 ms
--- 192.168.3.7 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.040/0.045/0.052/0.007 ms
[root@linuxprobe network-scripts]# ping -c 3 www.baidu.com ## 测试外网联通性,联通
PING www.a.shifen.com (39.156.66.18) 56(84) bytes of data.
64 bytes from 39.156.66.18: icmp_seq=1 ttl=51 time=14.4 ms
64 bytes from 39.156.66.18: icmp_seq=2 ttl=51 time=14.4 ms
64 bytes from 39.156.66.18: icmp_seq=3 ttl=51 time=14.5 ms
--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 8039ms
rtt min/avg/max/mdev = 14.404/14.485/14.598/0.128 ms
[root@linuxprobe network-scripts]#
4、将另一台虚拟机利用快照恢复至初始状态,网络模式调整为桥接模式,同时打开网络连接,测试两天虚拟机联通性
注:这是第二台虚拟机
查看此时网络连接:关闭
查看当前网卡信息:
[root@linuxprobe Desktop]# cd /etc/sysconfig/network-scripts/ ## 进入网卡配置文件所在目录
[root@linuxprobe network-scripts]# ls
ifcfg-eno16777728 ifdown-ppp ifup-eth ifup-sit
ifcfg-lo ifdown-routes ifup-ippp ifup-Team
ifdown ifdown-sit ifup-ipv6 ifup-TeamPort
ifdown-bnep ifdown-Team ifup-isdn ifup-tunnel
ifdown-eth ifdown-TeamPort ifup-plip ifup-wireless
ifdown-ippp ifdown-tunnel ifup-plusb init.ipv6-global
ifdown-ipv6 ifup ifup-post network-functions
ifdown-isdn ifup-aliases ifup-ppp network-functions-ipv6
ifdown-post ifup-bnep ifup-routes
[root@linuxprobe network-scripts]# ifconfig ## 查看当前网卡信息
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:aa:2b:29 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 0 (Local Loopback)
RX packets 258 bytes 20876 (20.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 258 bytes 20876 (20.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
打开网络连接查看网卡信息:
[root@linuxprobe network-scripts]# ls ## 列出网卡配置文件目录,多出一个文件
ifcfg-Auto_Ethernet ifdown-post ifup-bnep ifup-routes
ifcfg-eno16777728 ifdown-ppp ifup-eth ifup-sit
ifcfg-lo ifdown-routes ifup-ippp ifup-Team
ifdown ifdown-sit ifup-ipv6 ifup-TeamPort
ifdown-bnep ifdown-Team ifup-isdn ifup-tunnel
ifdown-eth ifdown-TeamPort ifup-plip ifup-wireless
ifdown-ippp ifdown-tunnel ifup-plusb init.ipv6-global
ifdown-ipv6 ifup ifup-post network-functions
ifdown-isdn ifup-aliases ifup-ppp network-functions-ipv6
[root@linuxprobe network-scripts]# ll -h
total 216K
-rw-r--r--. 1 root root 298 Nov 3 11:28 ifcfg-Auto_Ethernet
-rw-r--r--. 1 root root 293 Nov 3 06:44 ifcfg-eno16777728
-rw-r--r--. 1 root root 254 Apr 2 2014 ifcfg-lo
lrwxrwxrwx. 1 root root 24 Nov 3 06:42 ifdown -> ../../../usr/sbin/ifdown
-rwxr-xr-x. 1 root root 627 Apr 2 2014 ifdown-bnep
-rwxr-xr-x. 1 root root 5.5K Apr 2 2014 ifdown-eth
-rwxr-xr-x. 1 root root 781 Apr 2 2014 ifdown-ippp
-rwxr-xr-x. 1 root root 4.1K Apr 2 2014 ifdown-ipv6
lrwxrwxrwx. 1 root root 11 Nov 3 06:42 ifdown-isdn -> ifdown-ippp
-rwxr-xr-x. 1 root root 1.7K Apr 2 2014 ifdown-post
-rwxr-xr-x. 1 root root 1.1K Apr 2 2014 ifdown-ppp
-rwxr-xr-x. 1 root root 837 Apr 2 2014 ifdown-routes
-rwxr-xr-x. 1 root root 1.5K Apr 2 2014 ifdown-sit
-rwxr-xr-x. 1 root root 1.5K Mar 31 2014 ifdown-Team
-rwxr-xr-x. 1 root root 1.5K Mar 31 2014 ifdown-TeamPort
-rwxr-xr-x. 1 root root 1.5K Apr 2 2014 ifdown-tunnel
lrwxrwxrwx. 1 root root 22 Nov 3 06:42 ifup -> ../../../usr/sbin/ifup
-rwxr-xr-x. 1 root root 13K Apr 2 2014 ifup-aliases
-rwxr-xr-x. 1 root root 859 Apr 2 2014 ifup-bnep
-rwxr-xr-x. 1 root root 10K Apr 2 2014 ifup-eth
-rwxr-xr-x. 1 root root 12K Apr 2 2014 ifup-ippp
-rwxr-xr-x. 1 root root 11K Apr 2 2014 ifup-ipv6
lrwxrwxrwx. 1 root root 9 Nov 3 06:42 ifup-isdn -> ifup-ippp
-rwxr-xr-x. 1 root root 642 Apr 2 2014 ifup-plip
-rwxr-xr-x. 1 root root 1.1K Apr 2 2014 ifup-plusb
-rwxr-xr-x. 1 root root 2.6K Apr 2 2014 ifup-post
-rwxr-xr-x. 1 root root 4.1K Apr 2 2014 ifup-ppp
-rwxr-xr-x. 1 root root 1.9K Apr 2 2014 ifup-routes
-rwxr-xr-x. 1 root root 3.2K Apr 2 2014 ifup-sit
-rwxr-xr-x. 1 root root 1.6K Oct 31 2013 ifup-Team
-rwxr-xr-x. 1 root root 1.9K Mar 31 2014 ifup-TeamPort
-rwxr-xr-x. 1 root root 2.6K Apr 2 2014 ifup-tunnel
-rwxr-xr-x. 1 root root 1.6K Apr 2 2014 ifup-wireless
-rwxr-xr-x. 1 root root 4.6K Apr 2 2014 init.ipv6-global
-rw-r--r--. 1 root root 14K Apr 2 2014 network-functions
-rw-r--r--. 1 root root 26K Apr 2 2014 network-functions-ipv6
[root@linuxprobe network-scripts]# date
Tue Nov 3 11:32:01 CST 2020
[root@linuxprobe network-scripts]# cat ifcfg-Auto_Ethernet ## 查看
HWADDR=00:0C:29:AA:2B:29
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME="Auto Ethernet"
UUID=740b2fc9-4bb9-4070-8a9d-782def04d312
ONBOOT=yes
## BOOTPROTO=dhcp表示网络分配方式为DHCP;ONBOOT=yes 表示开机自动启动网卡
[root@linuxprobe network-scripts]# ifconfig ## 查看网卡信息,多出IP信息
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.3.13 netmask 255.255.255.0 broadcast 192.168.3.255
inet6 fe80::20c:29ff:feaa:2b29 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:aa:2b:29 txqueuelen 1000 (Ethernet)
RX packets 29 bytes 3395 (3.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 60 bytes 7339 (7.1 KiB)
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 0 (Local Loopback)
RX packets 390 bytes 31664 (30.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 390 bytes 31664 (30.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@linuxprobe network-scripts]# ping -c 3 192.168.3.13 ## 本地主机测试联通性,联通
PING 192.168.3.13 (192.168.3.13) 56(84) bytes of data.
64 bytes from 192.168.3.13: icmp_seq=1 ttl=64 time=0.038 ms
64 bytes from 192.168.3.13: icmp_seq=2 ttl=64 time=0.043 ms
64 bytes from 192.168.3.13: icmp_seq=3 ttl=64 time=0.043 ms
--- 192.168.3.13 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.038/0.041/0.043/0.005 ms
[root@linuxprobe network-scripts]# ping -c 3 www.baidu.com ## 测试与外网联通性,联通
PING www.a.shifen.com (39.156.66.14) 56(84) bytes of data.
64 bytes from 39.156.66.14: icmp_seq=1 ttl=51 time=16.5 ms
64 bytes from 39.156.66.14: icmp_seq=2 ttl=51 time=16.7 ms
64 bytes from 39.156.66.14: icmp_seq=3 ttl=51 time=16.5 ms
--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 8050ms
rtt min/avg/max/mdev = 16.540/16.624/16.752/0.139 ms
[root@linuxprobe network-scripts]# ping -c 3 192.168.3.7 ## 测试与第一台虚拟机联通性,联通
PING 192.168.3.7 (192.168.3.7) 56(84) bytes of data.
64 bytes from 192.168.3.7: icmp_seq=1 ttl=64 time=0.419 ms
64 bytes from 192.168.3.7: icmp_seq=2 ttl=64 time=0.208 ms
64 bytes from 192.168.3.7: icmp_seq=3 ttl=64 time=0.226 ms
--- 192.168.3.7 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 0.208/0.284/0.419/0.096 ms
说明连接外网的条件:网络模式为桥接模式;打开网络连接开关;会在网卡配置文件目录生成一个ifcfg-Auto_Ethernet自动配置文件,且该文件自动设置了网络配置方式DHCP,网卡开机自启
随后又进行了一些测试(略过),得出如下结论:
a、网络模式为桥接模式或者NAT模式,同时打开网络连接,可以实现与外网的连接;
b、仅主机模式,同时打开网络连接,不能实现与外网的连接;
c、两台虚拟机连接的条件为网络模式相同,同时都打开网络连接
d、只要打开网络连接,就会在网卡配置文件目录 /etc/sysconfig/networt-scripts/ 目录自动生成配置文件ifcfg-Auto_Ethernet,默认网络配置方式DHCP,同时网卡开机自启
e、一台虚拟机为桥接模式,另一台虚拟机为NAT模式,NAT虚拟机可以ping同桥接模式的虚拟机,但是桥接模式的虚拟机无法ping通NAT模式的虚拟机