MENU

Centos 7安装KVM之网桥搭建

September 10, 2019 • Read: 20 • 技术

首先检查下网桥模块是否已加载,这在CentOS 7中是默认已加载的,输入命令看到以下信息展示表示已加载:

$ modinfo bridge
filename:       /lib/modules/3.10.0-957.27.2.el7.x86_64/kernel/net/bridge/bridge.ko.xz
alias:          rtnl-link-bridge
version:        2.3
license:        GPL
retpoline:      Y
rhelversion:    7.6
srcversion:     24DDA8C6E1594CDB8543B49
depends:        stp,llc
intree:         Y
vermagic:       3.10.0-957.27.2.el7.x86_64 SMP mod_unload modversions
signer:         CentOS Linux kernel signing key
sig_key:        52:0A:4E:2D:9D:55:3E:F8:42:01:C1:88:B8:7F:E5:1B:9D:E1:1A:5E
sig_hashalgo:   sha256

如未加载,就手动加载下:

$ modprobe --first-time bridge

安装bridge-utils以控制网络适配器

$ yum install bridge-utils -y
$ systemctl restart network

配置网桥

$ cd /etc/sysconfig/network-scripts/
$ cp ifcfg-eth0 ifcfg-br0
$ cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BRIDGE=br0                                        //添加此行
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=eth0
UUID=a9b64dbf-9ed4-4442-9c2f-1e7ce7933bef
DEVICE=eth0
ONBOOT=yes
#IPADDR=172.16.15.62                                //注释这四行
#PREFIX=24
#GATEWAY=172.16.15.1
#DNS1=114.114.114.114
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_PRIVACY=no

$ cat /etc/sysconfig/network-scripts/ifcfg-br0
TYPE=Bridge                                        //修改为Bridge
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=br0                                           //修改为br0
#UUID=a9b64dbf-9ed4-4442-9c2f-1e7ce7933bef        //注释
DEVICE=br0                                        //修改为br0
ONBOOT=yes
IPADDR=172.16.15.62
PREFIX=24
GATEWAY=172.16.15.1
DNS1=114.114.114.114
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_PRIVACY=no

重启网卡服务

$ systemctl restart network

查看网卡

$ brctl show
bridge name    bridge id        STP enabled    interfaces
br-04ac05a69b08        8000.02427e052b4e    no
br-f3891125479d        8000.024286b73b5d    no
br0        8000.f01fafcee251    no        eth0
                            vnet0
docker0        8000.02425482387c    no        vethcf2bd6b
virbr0        8000.5254001583fb    yes        virbr0-nic

查看ip信息

$ ifconfig | head -20
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.15.62  netmask 255.255.255.0  broadcast 172.16.15.255
        inet6 fe80::d343:69ca:993c:28f2  prefixlen 64  scopeid 0x20<link>
        ether f0:1f:af:ce:e2:51  txqueuelen 1000  (Ethernet)
        RX packets 8111089  bytes 10759121734 (10.0 GiB)
        RX errors 0  dropped 5293  overruns 0  frame 0
        TX packets 3980653  bytes 300572384 (286.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

br-04ac05a69b08: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.20.0.1  netmask 255.255.0.0  broadcast 172.20.255.255
        ether 02:42:7e:05:2b:4e  txqueuelen 0  (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

br-f3891125479d: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.21.0.1  netmask 255.255.0.0  broadcast 172.21.255.255
        ether 02:42:86:b7:3b:5d  txqueuelen 0  (Ethernet)

ping下外网测试

$ ping baidu.com
PING baidu.com (39.156.69.79) 56(84) bytes of data.
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=1 ttl=47 time=23.5 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=47 time=23.1 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=47 time=39.0 ms

至此,网桥就搭建成功了。

Last Modified: September 11, 2019
Archives Tip
QR Code for this page
Tipping QR Code