Creating a bridge in linux
putting your vm's in existing network
When running virtual machines on a host in your home network. You might want to make those vm's first class citizens. Meaning: give them a ipadress in within your existing home network. To do this you can use a bridge. With a bridge you can connect two seperate networks.
add the lan ip to bridge (instead of nic) if want same network as host
sudo ip link set eno1 down
sudo ip link add name br0 type bridge
sudo ip addr add 192.168.178.55/24 dev br0
sudo ip link set eno1 master br0
sudo ip link set eno1 up
sudo ip link set br0 up
sudo ip route add default via 192.168.178.1
another solution is creating a new network for your vm's
create bridge:
sudo ip link add name br0 type bridge
bring up:
sudo ip link set br0 up
assign ip to bridge with new subnet:
ip a add 10.0.0.1/24 dev br0
start vm (with tap interface):
sudo qemu-system-x86_64 -enable-kvm -m 1024M -cdrom archlinux-2016.09.03-dual.iso -boot order=d -drive file=maria.cow,if=virtio -net nic -net bridge,br=br0
attach tap interface to bridge :
sudo ip link set tap0 master br0
give guest vm a ip within host subnet:
ip a add 10.0.0.2/24 dev ens3
bring interface up:
ip link set ens3 up
add route in guest (default -> host ip) :
ip route add default via 10.0.0.1
enable routing on host
echo 1 > /proc/sys/net/ipv4/ip_forward
allow traffic from guest to to outside (internet)
source NAT: iptables -t nat -A POSTROUTING --out-interface eno1 -j MASQUERADE
create route from other pc on network to guest
ip route add 10.0.0.0/24 via 192.168.178.55