Setting guest network

Guest (VM) networking in kvm is the same as in qemu, so it is possible to refer to other documentations about networking for qemu. This page will try to explain how to configure the most frequent types of network needed.

1. User Networking

Use case:

Prerequisites:

Solution:

qemu-system-x86_64 -hda /path/to/hda.img -net nic -net user

Notes:

2. private virtual bridge

Use case:

Prerequisites:

/sbin/ip
/usr/sbin/brctl
/usr/sbin/tunctl

Solution:

sudo /usr/sbin/brctl addbr br0

#!/bin/sh
set -x
 
switch=br0
 
if [ -n "$1" ];then
        /usr/bin/sudo /usr/sbin/tunctl -u `whoami` -t $1
        /usr/bin/sudo /sbin/ip link set $1 up
        sleep 0.5s
        /usr/bin/sudo /usr/sbin/brctl addif $switch $1
        exit 0
else
        echo "Error: no interface specified"
        exit 1
fi

#!/bin/sh
# generate a random mac address for the qemu nic
# shell script borrowed from user pheldens @ qemu forum
echo $(echo -n DE:AD:BE:EF ; for i in `seq 1 2` ;
do echo -n `echo ":$RANDOM$RANDOM" | cut -n -c -3` ;done)

qemu-system-x86_64 -hda /path/to/hda.img -net nic,macaddr=$macaddress -net tap

Notes:

qemu-system-x86_64 -hda /path/to/hda.img -net nic,macaddr=$macaddress -net tap,script=/path/to/qemu-ifup

3. public bridge

WARNING: The here shown method, will not work with most(all?) wireless drivers, as these do not support bridging.

Use case:

Prerequisites:

/sbin/ip
/usr/sbin/brctl
/usr/sbin/tunctl

Solution 1: using distro sysconfig script

DEVICE=switch
BOOTPROTO=dhcp
ONBOOT=yes
TYPE=Bridge

Solution 2: manual

sudo /usr/sbin/brctl addbr br0

sudo /usr/sbin/brctl  addif br0 eth0

#!/bin/sh
set -x
 
switch=br0
 
if [ -n "$1" ];then
        /usr/bin/sudo /usr/sbin/tunctl -u `whoami` -t $1
        /usr/bin/sudo /sbin/ip link set $1 up
        sleep 0.5s
        /usr/bin/sudo /usr/sbin/brctl addif $switch $1
        exit 0
else
        echo "Error: no interface specified"
        exit 1
fi

#!/bin/sh
# generate a random mac address for the qemu nic
# shell script borrowed from user pheldens @ qemu forum
echo $(echo -n DE:AD:BE:EF ; for i in `seq 1 2` ;
do echo -n `echo ":$RANDOM$RANDOM" | cut -n -c -3` ;done)

qemu-system-x86_64 -hda /path/to/hda.img -net nic,macaddr=$macaddress -net tap

Notes:

qemu-system-x86_64 -hda /path/to/hda.img -net nic,macaddr=$macaddress -net tap,script=/path/to/qemu-ifup

4. iptables

you can also connect your guest vm to a tap in your host. then setting iptables rules in your host to become a router + firewall for your vm.

5. vde

another option is using vde (virtual distributed ethernet).

Networking (last edited 2008-05-05 18:27:08 by fk6)