Qemu: How-To Share Network Access with the Ghest OS

Qemu Network Sharing

Create a file /etc/qemu-ifup that contain:

#!/bin/sh
sudo modprobe tun
sudo /sbin/ifconfig $1 up 10.0.2.2 netmask 255.255.255.0 broadcast 10.0.2.255

# IP masquerade
sudo echo "1" > /proc/sys/net/ipv4/ip_forward
sudo /sbin/iptables -N nat
sudo /sbin/iptables -t nat -F
sudo /sbin/iptables -t nat -A POSTROUTING -s 10.0.2.15 -j MASQUERADE
sudo /sbin/iptables -t nat -A POSTROUTING -d 10.0.2.15 -o $1

Don’t forget to give it execution permissions:

chmod 755 /etc/qemu-ifup

Start qemu with the following parameters:

qemu /home/kevin/qemu-mdk10.1.img -n /etc/qemu-ifup

Setup the network in your ghest OS in qemu:

ifconfig eth0 10.0.2.15
route add default gw 10.0.2.2

Test the visibility of the guest OS from the host OS:

[root@localhost kevin]# ping 10.0.2.15
PING 10.0.2.15 (10.0.2.15) 56(84) bytes of data.
64 bytes from 10.0.2.15: icmp_seq=1 ttl=64 time=2.96 ms
64 bytes from 10.0.2.15: icmp_seq=2 ttl=64 time=0.295 ms
64 bytes from 10.0.2.15: icmp_seq=3 ttl=64 time=0.296 ms

--- 10.0.2.15 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.295/1.185/2.965/1.258 ms

Test the visibility of the host from the guest:

[root@localhost root]# ping 10.0.2.2
PING 10.0.2.2 (10.0.2.2) 56(84) bytes of data.
64 bytes from 10.0.2.2: icmp_seq=1 ttl=64 time=1.08 ms
64 bytes from 10.0.2.2: icmp_seq=2 ttl=64 time=0.433 ms
64 bytes from 10.0.2.2: icmp_seq=3 ttl=64 time=0.383 ms

--- 10.0.2.2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 0.383/0.634/1.087/0.321 ms

6 Responses to “Qemu: How-To Share Network Access with the Ghest OS”


  • Wow, I’ve been trying to get QEMU to recognize my network for a couple days, and your script, though very old, finally worked. I had to put in different options, my QEMU looks like this:

    qemu -cdrom /dev/cdrom -hda craw.img -m 512 -usb -net nic,vlan=0 -net user,script=/etc/qemu-ifup
    

    THANK YOU! I’ll definitely be linking to this post when I blog about my success.

  • Hi Joel !

    Happy to see that some old knowledge can be recycled ! ;)

  • That works good but how can I add another guest? I want to create a small virtual LAN.

  • Hi daniele,

    I’m sorry, I never tried to launch several guests. I don’t know how my script will behave.

    If you manage to make a more generic script to support many guests, feel free to comments about your experimentations !

  • This post was written with qemu 0.7.x. As Joel said above, with a recent version (0.8.x in this case), you have to launch qemu with the following command:

    qemu /home/kevin/qemu-mdk10.1.img -net nic -net tap
    

    You can also add -m 512 parameter if you need qemu to emulate 512 Mb of RAM.

  • hi

    I have apache installed on localhost for testing/development purposes and want access localhost on host machine from quest qemu. But I don’t want qemu to access network. Just a localhost on the host machine.

    Can you please explain how to do this?

Leave a Reply

Additional comments powered by BackType