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:

1$ chmod 755 /etc/qemu-ifup

Start qemu with the following parameters:

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

Setup the network in your ghest OS in qemu:

1$ ifconfig eth0 10.0.2.15
2$ route add default gw 10.0.2.2

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

1$ ping 10.0.2.15
2PING 10.0.2.15 (10.0.2.15) 56(84) bytes of data.
364 bytes from 10.0.2.15: icmp_seq=1 ttl=64 time=2.96 ms
464 bytes from 10.0.2.15: icmp_seq=2 ttl=64 time=0.295 ms
564 bytes from 10.0.2.15: icmp_seq=3 ttl=64 time=0.296 ms
6
7--- 10.0.2.15 ping statistics ---
83 packets transmitted, 3 received, 0% packet loss, time 2000ms
9rtt min/avg/max/mdev = 0.295/1.185/2.965/1.258 ms

Test the visibility of the host from the guest:

1$ ping 10.0.2.2
2PING 10.0.2.2 (10.0.2.2) 56(84) bytes of data.
364 bytes from 10.0.2.2: icmp_seq=1 ttl=64 time=1.08 ms
464 bytes from 10.0.2.2: icmp_seq=2 ttl=64 time=0.433 ms
564 bytes from 10.0.2.2: icmp_seq=3 ttl=64 time=0.383 ms
6
7--- 10.0.2.2 ping statistics ---
83 packets transmitted, 3 received, 0% packet loss, time 2001ms
9rtt min/avg/max/mdev = 0.383/0.634/1.087/0.321 ms

6 archived comments

Comments are closed. These were posted on Disqus, and are archived here.

  1. 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.

    1. Kevin Deldycke Author

      Hi Joel !

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

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

    1. Kevin Deldycke Author

      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 !

  3. Kevin Deldycke Author

    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.

  4. 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?