本文在ubuntu 20.04上实测。
由于工作需要,装了一下qemu,以前都是用vmware的,反正也不要钱。
现在考虑到版权的问题,改用qemu。
安装就简单了:
sudo apt-get install qemu-kvm libvirt-daemon-system \
libvirt-clients virtinst bridge-utils
装好就可以使用virsh工具了
virsh --list
添加网桥要按以下的步骤:
一、编辑netplan下面的网卡配置文件,添加br0
vi /etc/netplan/00-installer-config.yaml
network:
ethernets:
enp0s31f6:
dhcp4: false
dhcp6: false
bridges:
br0:
interfaces: [ enp0s31f6 ]
addresses: [192.168.60.245/24]
gateway4: 192.168.60.1
mtu: 1500
nameservers:
addresses: [114.114.114.114]
parameters:
stp: false
forward-delay: 4
dhcp4: no
dhcp6: no
version: 2
在这里,enp0s31f6就是物理网卡啦,关掉这个上面的dhcp
br0是新建立的网桥,它用到 enp0s31f6这个网卡,然后设置上IP地址,当然也可以是自动dhcp,关键看你的网络的情况。
因为这个配置启用后,工作的网卡的将会是br0这个网桥,而不再是enp0s31f6。
二、执行命令启用这个配置:
netplan apply
可能还要重启一下网络服务才行
systemctl restart network
通过ip a查看网卡情况
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: enp0s31f6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel master br0 state UP group default qlen 1000
link/ether 30:d0:42:fc:92:86 brd ff:ff:ff:ff:ff:ff
3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 30:d0:42:fc:92:86 brd ff:ff:ff:ff:ff:ff
inet 192.168.60.245/24 brd 192.168.60.255 scope global br0
valid_lft forever preferred_lft forever
inet6 fe80::32d0:42ff:fefc:9286/64 scope link
valid_lft forever preferred_lft forever
4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:5c:5d:0c brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
5: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:5c:5d:0c brd ff:ff:ff:ff:ff:ff
6: vnet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel master br0 state UNKNOWN group default qlen 1000
link/ether fe:54:00:0c:58:45 brd ff:ff:ff:ff:ff:ff
如果使用virt-manager创建虚拟机的话,在配置选项中将会直接看到br0这个网桥了。
三、如果是使用命令行的话可能还需要配置一下
Now we can make KVM aware of this bridge. create a scratch XML file called host-bridge.xml
and insert the following:
<network>
<name>host-bridge</name>
<forward mode="bridge"/>
<bridge name="br0"/>
</network>
Use the following commands to make that our default bridge for VMs:
And then list the networks to confirm it is set to autostart:
virsh net-define /tmp/br0.xml
virsh net-start br0
virsh net-autostart br0
virsh net-list --all
此时ubuntu桌面自带的网络管理界面是不起作用的,更改不了网络,也无法正确的显示网络情况。
备用的 netplan配置文件:
# This is the network config written by 'subiquity'
network:
ethernets:
enp0s31f6:
dhcp4: no
optional: true
bridges:
br0:
interfaces: [enp0s31f6]
addresses: [192.168.60.245/24]
optional: true
gateway4: 192.168.60.1
nameservers:
addresses: [114.114.114.114,8.8.8.8]
version: 2
参考链接:
这个最好
Creating an Ubuntu 20.04 KVM Networked Bridge Interface – Answertopiahttps://www.answertopia.com/ubuntu/creating-an-ubuntu-kvm-networked-bridge-interface/
https://levelup.gitconnected.com/how-to-setup-bridge-networking-with-kvm-on-ubuntu-20-04-9c560b3e3991
https://www.cyberciti.biz/faq/ubuntu-20-04-add-network-bridge-br0-with-nmcli-command/
ubuntu官方的链接,讲bridge最容易的步骤。
Network - Configuration | Ubuntuhttps://ubuntu.com/server/docs/network-configuration