配置 QEMU Ubuntu 虚拟机通过网桥连接外网


创建网桥和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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM