Create VM with ip bridge to host network (in the same network as host)
On 2018/05/28 at 22:20
In the post Configuring NAT Network for VM, I setup VM to run in the NAT private network. But recently, I encounter a case that ask me to run multiple virtual devices in the same network as host. This is just look like a router running in bridge mode. So the following of this article tells how I accomplish the bridge mode on my Linux host machine.
Architecture
+---------------------------------+
| |
| Host Machine |
| |
| +----------------------+ |
| | | | +----------------------------+
| | br0 | | | |
| | 10.11.68.156/22 | | | |
| | | | | Virtual Machine |
| +---+--------------+---+ | | |
| | | | | +-------------------+ |
| +------+---+ +----+----+ | | | | |
| | | | | | | | enps3 (WAN) | |
| | enp3s0 | | tap0 +------------------+ 10.11.68.157/22 | |
| | (WAN) | | | | | | | |
| | | +---------+ | | +-------------------+ |
| +----------+ | | |
| | +----------------------------+
+---------------------------------+
Instructions
Stop NetworkManager to prevent it to get control of network interface
$ systemctl stop network-manager
Create a bridge and connect it to the WAN
brctl addbr xx
brctl addif xx enp3s0
Flush the ip from WAN interface and assign it to the bridge
$ ip addr flush dev enp3s0
$ ip link set dev xx up
$ ip addr add dev xx 10.11.68.156/22
Add default route
$ ip route add default via 10.11.71.254 dev xx
Create a tap device and connect it to the bridge
$ ip tuntap add dev tap0 mode tap
$ brctl addif xx tap0
Finally, fire up the VM with a NIC connect to the host WAN network
$ qemu-system-x86_64 --enable-kvm \
-boot d -cdrom ~/Downloads/archlinux-2018.05.01-x86_64.iso \
-net nic -net tap,ifname=tap0
-m 512