在VMWare Workstation中新建了一个虚拟机,由于虚拟机的建立确实需要花费不少时间,所以在真正使用时不使用第一次创建的虚拟机,而是使用它的克隆。当克隆的虚拟机出错时,重新克隆即可。

为了使虚拟机能够联网,使用VMWare中的桥接模式。

桥接模式–Bridged

桥接模式:桥接模式就是将主机网卡与虚拟机的网卡利用虚拟网桥进行通信。在桥接的作用下,类似于把物理主机虚拟为一个交换机,所有桥接设置的虚拟机连接到这个交换机的一个接口上,物理主机也同样插在这个交换机当中,所以所有桥接下的网卡与网卡都是交换模式的,相互可以访问而不干扰。

在桥接模式下,虚拟机ip地址需要与主机在同一个网段,如果需要联网,则网关与DNS需要与主机网卡一致。其网络结构如下图所示:

kvm虚拟机桥接宿主机网卡_vim

操作步骤

  1. 菜单栏"虚拟机"->“管理”->“克隆”
  2. 克隆虚拟机



    以上三步骤都是选择默认值,但新虚拟机的名称和路径需要根据自己的选择进行更改

    成功建立虚拟机!
  3. 对虚拟机进行设置

    硬件->处理器->虚拟化引擎->全选

    根据主机内存修改虚拟机内存,我将虚拟机内存修改为900M
    选项->高级->进程优先级->抓取的输入内容:高
    选项->高级->设置->禁止内存页面修整
  4. 开启此虚拟机,设置网络

注意:Linux7与Linux8中的设置并不相同

Linux7

[root@localhost ~]# cd /etc/sysconfig/network-scripts/      ##进入系统网络管理目录
[root@localhost network-scripts]# ls                        ##查看目录
ifcfg-ens33  ifdown-ppp       ifup-ib      ifup-Team
ifcfg-lo     ifdown-routes    ifup-ippp    ifup-TeamPort
ifdown       ifdown-sit       ifup-ipv6    ifup-tunnel
ifdown-bnep  ifdown-Team      ifup-isdn    ifup-wireless
ifdown-eth   ifdown-TeamPort  ifup-plip    init.ipv6-global
ifdown-ib    ifdown-tunnel    ifup-plusb   network-functions
ifdown-ippp  ifup             ifup-post    network-functions-ipv6
ifdown-ipv6  ifup-aliases     ifup-ppp
ifdown-isdn  ifup-bnep        ifup-routes
ifdown-post  ifup-eth         ifup-sit
[root@localhost network-scripts]# ifconfig                 ##查看网卡
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0c:29:b7:f3:75  txqueuelen 1000  (Ethernet)
        RX packets 37  bytes 9279 (9.0 KiB)
        RX errors 0  dropped 12  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 88  bytes 7736 (7.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 88  bytes 7736 (7.5 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:14:c3:5c  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
[root@localhost network-scripts]# rm -fr ifcfg-ens33    ##删除ens33的配置文件
[root@localhost network-scripts]# vim ifcfg-ens33       ##重新编辑该文件

kvm虚拟机桥接宿主机网卡_vim_02


注意:ip地址、子网掩码、网关地址和dns地址在Windows系统中的cmd命令行下输入:

ipconfig/all进行查询。

[root@localhost network-scripts]# systemctl stop NetworkManager     ##关闭网络管理员
[root@localhost network-scripts]# systemctl restart network         ##重启网络
[root@localhost network-scripts]# systemctl start NetworkManager    ##开启网络管理员
[root@localhost network-scripts]# ping 192.168.1.18 -c 3			##ping主机
PING 192.168.1.18 (192.168.1.18) 56(84) bytes of data.
64 bytes from 192.168.1.18: icmp_seq=1 ttl=64 time=0.279 ms
64 bytes from 192.168.1.18: icmp_seq=2 ttl=64 time=0.577 ms
64 bytes from 192.168.1.18: icmp_seq=3 ttl=64 time=0.564 ms

--- 192.168.1.18 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 0.279/0.473/0.577/0.138 ms
[root@localhost network-scripts]# ping www.baidu.com -c 3          ##ping百度首页
PING www.baidu.com (180.101.49.12) 56(84) bytes of data.
64 bytes from www.baidu.com (180.101.49.12): icmp_seq=1 ttl=52 time=42.1 ms
64 bytes from www.baidu.com (180.101.49.12): icmp_seq=2 ttl=52 time=43.6 ms
64 bytes from www.baidu.com (180.101.49.12): icmp_seq=3 ttl=52 time=52.6 ms

--- www.baidu.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 42.191/46.155/52.638/4.621 ms

Linux8

[root@RHTL8_clone ~]# cd /etc/sysconfig/network-scripts/
[root@RHTL8_clone network-scripts]# ls
ifcfg-ens160
[root@RHTL8_clone network-scripts]# rm -f ifcfg-ens160
[root@RHTL8_clone network-scripts]# vim ifcfg-ens160

kvm虚拟机桥接宿主机网卡_vim_03

[root@RHTL8_clone network-scripts]# systemctl restart NetworkManager
[root@RHTL8_clone network-scripts]# nmcli connection show
NAME           UUID                                  TYPE      DEVICE 
System ens160  ea74cf24-c2a2-ecee-3747-a2d76d46f93b  ethernet  ens160 
virbr0         8d3419ec-7d1c-444c-a17d-dac71d7ed4af  bridge    virbr0 
[root@RHTL8_clone network-scripts]# ping 192.168.1.18 -c 3		##ping主机
PING 192.168.1.18 (192.168.1.18) 56(84) bytes of data.
64 bytes from 192.168.1.18: icmp_seq=1 ttl=64 time=0.511 ms
64 bytes from 192.168.1.18: icmp_seq=2 ttl=64 time=0.611 ms
64 bytes from 192.168.1.18: icmp_seq=3 ttl=64 time=0.575 ms

--- 192.168.1.18 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 48ms
rtt min/avg/max/mdev = 0.511/0.565/0.611/0.049 ms
[root@RHTL8_clone network-scripts]# ping 192.168.1.118 -c 3		##ping另一台虚拟机
PING 192.168.1.118 (192.168.1.118) 56(84) bytes of data.
64 bytes from 192.168.1.118: icmp_seq=1 ttl=64 time=0.260 ms
64 bytes from 192.168.1.118: icmp_seq=2 ttl=64 time=0.749 ms
64 bytes from 192.168.1.118: icmp_seq=3 ttl=64 time=0.802 ms

--- 192.168.1.118 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 87ms
rtt min/avg/max/mdev = 0.260/0.603/0.802/0.245 ms
[root@RHTL8_clone network-scripts]# ping www.baidu.com -c 3		##ping百度首页
PING www.baidu.com (180.101.49.12) 56(84) bytes of data.
64 bytes from www.baidu.com (180.101.49.12): icmp_seq=1 ttl=52 time=42.8 ms
64 bytes from www.baidu.com (180.101.49.12): icmp_seq=2 ttl=52 time=44.3 ms
64 bytes from www.baidu.com (180.101.49.12): icmp_seq=3 ttl=52 time=43.6 ms

--- www.baidu.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 6ms
rtt min/avg/max/mdev = 42.770/43.545/44.290/0.643 ms

参考文献:

  1. https://blog.51cto.com/13648313/2164307