创建网桥和tap设备

$ brctl addbr ms_br
$ ip link set ms_br up
$ ip addr add 192.168.10.1/24 dev ms_br
$ ip tuntap add dev ms_tap mode tap
$ brctl addif ms_br ms_tap
$ ip link set ms_tap up
$ ip addr add 192.168.10.10/24 dev ms_tap

开启转发和NAT

$ iptables -A FORWARD -s 192.168.10.0/24 -j ACCEPT
$ iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -o eno1 -j MASQUERADE # 替换 eno1 为实际连接外网的物理网口
$ iptables -L -n
$ iptables -t nat -L -n
$ sysctl net.ipv4.ip_forward=1

启动虚拟机并ping外网

$ qemu-system-x86_64 -m 16G -smp 8 --enable-kvm -hda centos.qcow2 -nographic -serial mon:stdio -netdev tap,id=mynet0,script=no,downscript=no,ifname=ms_tap -device virtio-net,netdev=mynet0 # centos.qcow2 可以从 https://cloud.centos.org/centos/7/images/ 下载
$ ip addr add 192.168.10.11/24 dev ens3
$ ip route add default via 192.168.10.1 dev ens3
$ ping www.baidu.com