# networking with qemu

## JackDog

Does anyone know the best way to setup qemu to simply use a virtual ehternet device instead of piggy backing on my primary eth0? I would like to write a script that would launch qemu in such a way that it uses a virtual eth device so that it can actually be "on the network" rather than firewalled on a private subnet...

----------

## bravecobra

I've tried this, but I can't seem to get it working

----------

## Lejban

 *QEMU Documentation wrote:*   

> 3.7.1 Using tun/tap network interface
> 
> This is the standard way to emulate network. QEMU adds a virtual network device on your host (called tun0), and you can then configure it as if it was a real ethernet card.
> 
> As an example, you can download the `linux-test-xxx.tar.gz' archive and copy the script `qemu-ifup' in `/etc' and configure properly sudo so that the command ifconfig contained in `qemu-ifup' can be executed as root. You must verify that your host kernel supports the TUN/TAP network interfaces: the device `/dev/net/tun' must be present. 

 

http://fabrice.bellard.free.fr/qemu/qemu-doc.html#SEC20

----------

## bravecobra

Did some research today and placed my findings in my blog: http://blog.bravecobra.com/index.php/linux/2005/02/25/emulating_an_os_with_qemu

----------

## transienteagle

bravecobra,

good work and many thanks. This had me foxed for quite a while. To get it all working (internet / access to host and host lan etc) I set up the windows XP guest on a different subnet.

My actual configuration turned out to be; Host ip address 10.0.0.1, DNS server 10.0.0.138, host tun device 11.0.0.8;

                                                            guest ip address 11.0.0.5 default gateway 11.0.0.8 dns server 10.0.0.138;

my script to get windows XP working within qemu is

```

#!/bin/bash

#sources

# gentoo forums search qemu networking (http://forums.gentoo.org/viewtopic-t-252270-highlight-qemu.html)

# http://blog.bravecobra.com/index.php/linux/2005/02/25/emulating_an_os_with_qemu

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

echo "1" > /proc/sys/net/ipv4/ip_forward

# set up the vitual switch (emerge vde)

vde_switch -tap tun0 -daemon

# start the tun interface

ifconfig tun0 11.0.0.8 up

# Make sure that we have a suitable route into the XP guest

route add -host 11.0.0.5 gw 11.0.0.8

# make sure we have suitable priviliges on the socket used by the vitual switch

chmod 666 /tmp/vde.ctl

# start windows using the vde wrapper

vdeq qemu -m 512 -hda hd.img
```

many rgds

TE

----------

## ExElNeT

Is there a way to get a all lan clients and vms in the same subnet? I have 2 vms running on one machine and they cant connect to eachother. A connection to the DNS server and the gateway is possible. 

My goal is to create a small network using vms.

----------

## ooky

I had the same problem.

https://forums.gentoo.org/viewtopic-p-2236102.html#2236102

----------

## fourhead

@transienteagle

First, thanks for your how-to, it helped me a lot, but I'm still not 100% where I want to be. I have TWO qemu instances that I want to communicate with each other. Now with your how-to I was able to do the following:

Host: eth0 192.168.1.10, tun0 192.168.1.30

qemu-1: eth0 192.168.1.40, gw 192.168.1.30

qemu-2: eth0 192.168.1.50, gw 192.168.1.30

On the host, route looks like this:

```

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

192.168.1.50    192.168.1.30    255.255.255.255 UGH   0      0        0 tun0

192.168.1.40    192.168.1.30    255.255.255.255 UGH   0      0        0 tun0

192.168.1.0     *               255.255.255.0   U     0      0        0 eth0

192.168.1.0     *               255.255.255.0   U     0      0        0 tun0

loopback        *               255.0.0.0       U     0      0        0 lo

default         192.168.1.1     0.0.0.0         UG    0      0        0 eth0

```

I used the script you provied to get this up. Within the virtual machines, I can ping the gateway/host tun0 and I can ping the host (192.168.1.10). From the host, I can ping both virtual machines. But, I can't ping qemu-1 from qemu-2 or vice versa, and that's exactly what I need! From the routing table on the host, shouldn't any packet to 192.168.1.40 or 50 be routed to 192.168.1.30? This works when I ping host->qemu but it doesn't work when a packet "comes out of" tun0 and has to go back into tun0 to the other virtual machine.

I'm sure there's just a little iptabes rule missing, but how do I do this? Any help would be greatly appreciated.

----------

## Redeeman

why not just use the --user-net thing, then you dont need a tun/tap device

----------

## fourhead

OK, I tried that, I started both qemu's with -user-net. Both vms then get the IP 10.0.2.15, the DHCP server and router that qemu provides is 10.0.2.2. I can reach the Internet with both vms, but of course I can't ping between them. So I set the second vm to a static config with 10.0.2.16 and left all other settings as they are ... but nothing happend. I still can't ping from one vm to the other.

What I tried with tun and bridging was the following:

Start the first qemu on tun0, tun0 has 192.168.1.30. Start the second qemu on tun1 with 192.168.1.31. The internal IP's that the guest OS has 192.168.1.40 and .50 respectively. I now thought I could create a bridge br0

brctl addbr br0

and then connect tun0 and tun1 together:

brctl addif tun0

brctl addif tun1

ifconfig br0 up

But the both VMs still can't ping each other. I'm pulling my hair out because of this, I'm trying the whole day now. Is there any easy iptables rule or bridge setup so that a ping that goes from qemu1->tun0->tun1->qemu2. Or, as I first tried it, use VDE, connect both qemus to the same tun device which also didn't work. I always have full networking, EXCEPT between the two virtual machines....

Any help on this would be very appreciated, I have to get this to work somehow!!

Tom

----------

## mel_82

I think that ping between VM was implemented in QEMU from 0.8..

Is it true?

----------

