Config virtual network connection with bridge mode

摘要

在宿主机为 CentOS 7 的环境中通过KVM配置CentOS 7 虚拟机,使用 Bridge 模式配置网络连接,使虚拟机与宿主机处于同一网络环境中。

Create Brdige

Install Module

CentOS 7 在系统启动时默认加载了桥接模块。使用下面的命令可以判断这个模块是否加载。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@weilu_125 kvms]# modinfo bridge
filename: /lib/modules/3.10.0-862.el7.x86_64/kernel/net/bridge/bridge.ko.xz
alias: rtnl-link-bridge
version: 2.3
license: GPL
retpoline: Y
rhelversion: 7.5
srcversion: A0B6183F98024E85CD123C5
depends: stp,llc
intree: Y
vermagic: 3.10.0-862.el7.x86_64 SMP mod_unload modversions
signer: CentOS Linux kernel signing key
sig_key: 3A:F3:CE:8A:74:69:6E:F1:BD:0F:37:E5:52:62:7B:71:09:E3:2B:96
sig_hashalgo: sha256

如果这个模块没有加载,可以使用下面的命令进行加载。

1
modprobe --first-time bridge

安装桥接工具:

1
yum install bridge-utils -y

create a network bridge

要创建一个网桥,可以在 /etc/sysconfig/network-scripts/ 目录下创建一个名为 ifcfg-brN 的文件,将其中的 N 替换为数字,比如“0”。

1
vi /etc/sysconfig/network-scripts/ifcfg-br0

将下面的内容放置到这个文件中,对应你的主机环境修改相应配置:

1
2
3
4
5
6
7
8
9
TYPE=Bridge
NAME=br0
DEVICE=br0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.125
GATEWAY=192.168.0.1
NETMASK=255.255.255.0
DNS1=192.168.0.1

创建完网桥之后,需要将网络配置接口挂载到这个网桥上。比如,我这边使用的本机已有的适配器 eno1

1
vi /etc/sysconfig/network-scripts/ifcfg-eno1

将以下内容放置到该配置文件中:

1
2
3
4
5
6
TYPE=Ethernet
BRIDGE=br0
NAME=eno1
UUID=99244a4d-8cac-4023-9a09-8e50c547cd3a
DEVICE=eno1
ONBOOT=yes

使用如下命令重启网络服务:

1
systemctl restart network

查看网络配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@weilu_125 network-scripts]# ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.125 netmask 255.255.255.0 broadcast 192.168.0.255
inet6 fe80::329c:23ff:fee1:f0d9 prefixlen 64 scopeid 0x20<link>
ether 30:9c:23:e1:f0:d9 txqueuelen 1000 (Ethernet)
RX packets 3503 bytes 599352 (585.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1932 bytes 1004140 (980.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

eno1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 30:9c:23:e1:f0:d9 txqueuelen 1000 (Ethernet)
RX packets 510891 bytes 330776497 (315.4 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 285983 bytes 46787578 (44.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 16 memory 0xa1100000-a1120000

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 13058 bytes 1819263 (1.7 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 13058 bytes 1819263 (1.7 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

Config Guest Connection

在虚拟机的设置中,将 Network source 设置为通过刚刚设置的网桥进行连接。
enter image description here

然后修改虚拟机的网络配置:

1
2
3
4
5
6
7
8
9
TYPE=Ethernet
ONBOOT=yes
DEVICE=eth0
BOOTPROTO=static
IPADDR=192.168.0.161
NETMASK=255.255.255.0
BROADCAST=192.168.0.255
GATEWAY=192.168.0.1
DNS1=192.168.0.1

使用如下命令重启网络服务:

1
systemctl restart network

查看网络配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@localhost ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.161 netmask 255.255.255.0 broadcast 192.168.0.255
inet6 fe80::5054:ff:fe24:c503 prefixlen 64 scopeid 0x20<link>
ether 52:54:00:24:c5:03 txqueuelen 1000 (Ethernet)
RX packets 212 bytes 183776 (179.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 186 bytes 18719 (18.2 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 1000 (Local Loopback)
RX packets 68 bytes 5912 (5.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 68 bytes 5912 (5.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

test

From Host

1
2
3
4
5
6
7
8
9
[root@weilu_125 network-scripts]# ping 192.168.0.161 -c 3
PING 192.168.0.161 (192.168.0.161) 56(84) bytes of data.
64 bytes from 192.168.0.161: icmp_seq=1 ttl=64 time=0.386 ms
64 bytes from 192.168.0.161: icmp_seq=2 ttl=64 time=0.327 ms
64 bytes from 192.168.0.161: icmp_seq=3 ttl=64 time=0.320 ms

--- 192.168.0.161 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.320/0.344/0.386/0.033 ms

From Guest

1
2
3
4
5
6
7
8
9
[root@localhost ~]# ping 192.168.0.125 -c 3
PING 192.168.0.125 (192.168.0.125) 56(84) bytes of data.
64 bytes from 192.168.0.125: icmp_seq=1 ttl=64 time=0.101 ms
64 bytes from 192.168.0.125: icmp_seq=2 ttl=64 time=0.260 ms
64 bytes from 192.168.0.125: icmp_seq=3 ttl=64 time=0.252 ms

--- 192.168.0.125 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.101/0.204/0.260/0.074 ms