KVM——以桥接的方式搭建虚拟机网络配置

以桥接的方式搭建虚拟机网络,其优势是可以将网络中的虚拟机看作是与主机同等地位的服务器。

 

在原本的局域网中有两台主机,一台是win7(IP: 192.168.0.236),一台是CentOS7(IP: 192.168.0.251),现在要在CentOS(以下简称HOST)中搭建虚拟机,并使得虚拟机能够处于与Win7和HOST相同的网络环境中。

 

HOST原本的网络配置

打开HOST的网络链接配置文件:

vi /etc/sysconfig/network-scripts/ifcfg-enp3s0

其文件内容为:

TYPE=Ethernet
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_AUTOCONF=yes
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=enp3s0
UUID=99244a4d-8cac-4023-9a09-8e50c547cd3a
DEVICE=enp3s0
HWADDR=b0:6e:bf:bb:12:ad

#static assignment
ONBOOT=yes
BOOTPROTO=static

IPADDR=192.168.0.251
GATEWAY=192.168.0.1
NETMASK=255.255.255.0

 

DNS服务器配置在如下文件中:

vi /etc/sysconfig/network

其内容为:

DNS1=192.168.0.1

 

配置桥接

以桥接的方式配置网络,修改 ifcfg-enp3s0文件:

TYPE=Ethernet
BRIDGE=br0

#DEFROUTE=yes
#PEERDNS=yes
#PEERROUTES=yes
#IPV4_AUTOCONF=yes
#IPV6INIT=yes
#IPV6_AUTOCONF=yes
#IPV6_DEFROUTE=yes
#IPV6_PEERDNS=yes
#IPV6_PEERROUTES=yes
#IPV6_FAILURE_FATAL=no
NAME=enp3s0
UUID=99244a4d-8cac-4023-9a09-8e50c547cd3a
DEVICE=enp3s0
#HWADDR=b0:6e:bf:bb:12:ad

#static assignment
ONBOOT=yes
#BOOTPROTO=static

#IPADDR=192.168.0.251
#GATEWAY=192.168.0.1
#NETMASK=255.255.255.0

同时,在相同目录下拷贝一份该文件,并命名为 ifcfg-br0:

cp /etc/sysconfig/network-scripts/ifcfg-enp3s0 /etc/sysconfig/network-scripts/ifcfg-br0

然后修改该文件内容为:

TYPE=Bridge
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_AUTOCONF=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=br0
#UUID=99244a4d-8cac-4023-9a09-8e50c547cd3a
DEVICE=br0
#HWADDR=b0:6e:bf:bb:12:ad

#static assignment
ONBOOT=yes
BOOTPROTO=static

IPADDR=192.168.0.251
GATEWAY=192.168.0.1
NETMASK=255.255.255.0

修改完成这两个文件之后,重启网络服务:

systemctl restart network.service

 

配置虚拟网络

使用如下命令打开KVM管理工具:

virt-manager

看到窗口:

在【编辑】菜单中选择【连接详情】,打开如下窗口:

选择【网络接口】这个Tab,将br0接口给停止掉,即左侧下方红色的大叉叉,注意不是圆圈的叉叉,那个是删除,此处不要删除。

然后选择【虚拟网络】这个Tab,将原本的一个default网络给停止并删除掉,然后重新创建一个虚拟网络,创建过程如下:

设置一个虚拟网络的名称,然后下一步:

然后

然后是选择转发的物理网络,此处注意是选择enp3s0,模式采用路由的方式:

配置完成之后回到网络接口界面,启动br0,然后再回到虚拟网络界面,启动vnet0。

此时我们查看ifconfig:

br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.251  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::b26e:bfff:febb:12ad  prefixlen 64  scopeid 0x20<link>
        ether b0:6e:bf:bb:12:ad  txqueuelen 1000  (Ethernet)
        RX packets 70809  bytes 4050362 (3.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 718  bytes 69637 (68.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp3s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether b0:6e:bf:bb:12:ad  txqueuelen 1000  (Ethernet)
        RX packets 93610  bytes 20510654 (19.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 10830  bytes 918705 (897.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 1  (Local Loopback)
        RX packets 1610  bytes 122960 (120.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1610  bytes 122960 (120.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.100.1  netmask 255.255.255.0  broadcast 192.168.100.255
        ether 52:54:00:b3:49:b9  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

vnet0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::fc54:ff:fe00:8db  prefixlen 64  scopeid 0x20<link>
        ether fe:54:00:00:08:db  txqueuelen 1000  (Ethernet)
        RX packets 9522  bytes 786944 (768.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 76790  bytes 18908454 (18.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

可以看到类似这样的几个连接信息:enp3s0、br0、vnet0、virbr0。

注意,此时我们的HOST网络访问都是正常的。

然后创建一台虚拟机,其虚拟机配置为:

 

配置其网络连接为:

TYPE=Ethernet
UUID=55ec455e-aa43-47df-8657-d7846c355792
ONBOOT=yes
NM_CONTROLLED=yes
DEVICE=eth0
#HWADDR=52:54:00:00:08:DB
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6NIT=no
NAME="System eth0"
#HWADDR=00:0C:29:22:D2:64
PEERDNS=yes
PEERROUTES=yes
LAST_CONNECT=14674126

#static assignment
BOOTPROTO=static
IPADDR=192.168.0.211
NETMASK=255.255.255.0
BROADCAST=192.168.0.255
GATEWAY=192.168.0.1
DNS1=192.168.0.1

然后查看虚拟机的网络连接信息:

eth0      Link encap:Ethernet  HWaddr 52:54:00:00:08:DB  
          inet addr:192.168.0.211  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::5054:ff:fe00:8db/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:74804 errors:0 dropped:0 overruns:0 frame:0
          TX packets:9511 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:18772134 (17.9 MiB)  TX bytes:786170 (767.7 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:10 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:578 (578.0 b)  TX bytes:578 (578.0 b)

virbr0    Link encap:Ethernet  HWaddr 52:54:00:60:DA:35  
          inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

 

测试:

在Win7上Ping HOST:

正在 Ping 192.168.0.251 具有 32 字节的数据:
来自 192.168.0.251 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.0.251 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.0.251 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.0.251 的回复: 字节=32 时间<1ms TTL=64

192.168.0.251 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 0ms,最长 = 0ms,平均 = 0ms

在Win7上Ping虚拟机:

正在 Ping 192.168.0.211 具有 32 字节的数据:
来自 192.168.0.211 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.0.211 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.0.211 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.0.211 的回复: 字节=32 时间<1ms TTL=64

192.168.0.211 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 0ms,最长 = 0ms,平均 = 0ms

在HOST上Ping虚拟机:

PING 192.168.0.211 (192.168.0.211) 56(84) bytes of data.
64 bytes from 192.168.0.211: icmp_seq=10 ttl=64 time=0.206 ms
64 bytes from 192.168.0.211: icmp_seq=11 ttl=64 time=0.114 ms
64 bytes from 192.168.0.211: icmp_seq=12 ttl=64 time=0.138 ms
64 bytes from 192.168.0.211: icmp_seq=13 ttl=64 time=0.135 ms

在虚拟机上Ping HOST:

PING 192.168.0.251 (192.168.0.251) 56(84) bytes of data.
64 bytes from 192.168.0.251: icmp_seq=1 ttl=64 time=0.104 ms
64 bytes from 192.168.0.251: icmp_seq=2 ttl=64 time=0.277 ms
64 bytes from 192.168.0.251: icmp_seq=3 ttl=64 time=0.254 ms
64 bytes from 192.168.0.251: icmp_seq=4 ttl=64 time=0.220 ms

在虚拟机上Ping Win7:

PING 192.168.0.236 (192.168.0.236) 56(84) bytes of data.
64 bytes from 192.168.0.236: icmp_seq=1 ttl=128 time=0.826 ms
64 bytes from 192.168.0.236: icmp_seq=2 ttl=128 time=0.405 ms
64 bytes from 192.168.0.236: icmp_seq=3 ttl=128 time=0.384 ms
64 bytes from 192.168.0.236: icmp_seq=4 ttl=128 time=0.434 ms

所有的网络都是通畅的,万事大吉!

 

KVM安装使用

posted @ 2017-11-01 14:41  大肥肥就是我  阅读(26651)  评论(0编辑  收藏  举报