qemu使vm网络和本机网络打通,
原理:主机网络改成桥接,建虚拟机时候的tap1设备帮到这个桥上即可
假设本机ip 192.168.139.85
参考上篇 http://haoningabc.iteye.com/blog/2224128
一.主机上关闭NetworkManager
systemctl stop NetworkManager
sysetmctl disable NetworkManager
二.让主机变桥接网络:
[root@centos32 jslinux-network]# cat /etc/sysconfig/network-scripts/ifcfg-br0 DEVICE=br0 TYPE=Bridge ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=none IPADDR=192.168.139.85 NETMASK=255.255.240.0 GATEWAY=192.168.128.1 DNS1=114.114.114.114
[root@centos32 jslinux-network]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 NAME="eth0" DEVICE="eth0" ONBOOT="yes" NM_CONTROLLED=yes BOOTPROTO=none BRIDGE=br0
重启网络
三.以tap方式启动vm
qemu-system-i386 -kernel /root/jslinux/obj/linux-x86-basic/arch/i386/boot/bzImage -drive file=ubuntu.qcow2,if=ide,cache=none -append "console=ttyS0 root=/dev/sda rw rdinit=/sbin/init notsc=1" -nographic -boot order=dc,menu=on -net nic,vlan=0,macaddr=52:54:00:12:34:22,model=e1000,addr=08 -net tap,ifname=tap1,script=no,downscript=no
关键地方是
-net tap,ifname=tap1,script=no,downscript=no
四.主机上把tap连接到桥上
#ip link show ip link set tap1 up brctl addif br0 tap1 #brctl show echo 1 > /proc/sys/net/ipv4/ip_forward ifconfig br0 promisc up ifconfig eth0 promisc up
Linux系统默认是禁止数据包转发的。所谓转发即当主机拥有多于一块的网卡时,其中一块收到数据包,根据数据包的目的ip地址将包发往本机另一网卡,该网卡根据路由表继续发送数据包。这通常就是路由器所要实现的功能。
配置Linux系统的ip转发功能,首先保证硬件连通,然后打开系统的转发功能
五.vm上设置ip
ip link #看mac地址是否为52:54:00:12:34:22 ip link set eth0 up ifconfig eth0 192.168.139.88 netmask 255.255.240.0 route add default gw 192.168.128.1 ip route echo "nameserver 114.114.114.114" >/etc/resolv.conf ping www.baidu.com
在vm中
ctl+a c
切换到
monitor
(qemu) info network hub 0 \ hub0port1: tap.0: index=0,type=tap,ifname=tap1,script=no,downscript=no \ hub0port0: e1000.0: index=0,type=nic,model=e1000,macaddr=52:54:00:12:34:22 (qemu)