因为搭建的KVM虚拟机需要使用到桥接网络,故需要配置网桥。

CentOS:

1、查看物理网卡接口名

ip -f inet a s
nmcli connection

2、使用nmcli创建网桥

*注:本文网络接口名以enp1s0为例。

# 新建vmbr0网桥
[root@localhost ~]# nmcli connection add type bridge con-name vmbr0 ifname vmbr0 autoconnect yes
Connection 'vmbr0' (c632af1f-4c0e-4b4f-8a1c-375947ec963b) successfully added.     #执行成功提示信息
# 将网卡enp1s0桥接到vmbr0网桥
[root@localhost ~]# nmcli connection add type bridge-slave ifname enp1s0 master vmbr0
Connection 'bridge-slave-enp1s0' (79d21e5f-dee3-438d-809d-2c0ad73455be) successfully added.    #执行成功提示信息
# 给vmbr0网桥设置IP地址(如果桥接副网卡,不用来连接宿主机,可不设置IP)
[root@localhost ~]# nmcli c m vmbr0 ipv4.address 192.168.1.10/24 ipv4.gateway 192.168.1.1 ipv4.method manual
# 设置vmbr0网桥开机启动
[root@localhost ~]# nmcli c m vmbr0 c.autoconnect yes
# 关闭enp1s0网卡
[root@localhost ~]# nmcli connection down enp1s0
# 打开新建的vmbr0​​​​网桥
[root@localhost ~]# nmcli connection up vmbr0

Debian:

1、相关软件包安装

apt install bridge-utils

2、查看物理网卡接口名

ip -f inet a s

3、修改网络连接配置文件

*注:本文网络接口名以enp1s0、enp2s0为例。

vim /etc/network/interfaces

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
 
source /etc/network/interfaces.d/*
 
# The loopback network interface
auto lo
iface lo inet loopback
iface enp1s0 inet manual
iface enp2s0 inet manual
vim /etc/network/interfaces.d/vmbr0

## static ip config file for vmbr0 ##
auto vmbr0
iface vmbr0 inet static
        address 192.168.20.156/22
        gateway 192.168.20.1
        bridge-ports enp1s0
        bridge-stp off
        bridge-fd 0

4、重启网络并检查路由表

systemctl restart network-manager
brctl show
ip r    # 一般情况应该有两条以上的路由表

如果ping不通外网IP,可添加路由表

ip route add default via 网关IP地址 dev vmbr0