I've struggled for a long time trying to set up a virtual network between my KVM virtual machines. I tried several forums and tutorials on the web, but still on my system it just wouldn't work. I eventually got it owrking, so I've decided to make some notes of my own. Hopefully it will be helpful to you as a reader as well.
Using a virtual switch like VDE (Virtual Distributed Ethernet), you'll be able to network a number of virtual machines (includeing the host machine) together without creating a TAP interface for each and every virtual machine. The way I've set it up is one TAP interface on the host machine that connects to the VDE switch, and also all the virtual machines connecting to the VDE switch, with the host machine acting as a "router" between the virtual network, and the physical "outside" network, without "bridging" my physical and virtual interfaces.
You'll notice that I'm using a rather old version of Ubuntu for this article (9.04 Jaunty Jackalope). The reason being that in my lab setup I have a machine with the complete 9.04 repositories including updates, so it's just easier for me to install my "lab" machines without wasting internet cap downloading packages for each machine. And besides, although I'm running later releases on my other computers, 9.04 still stays my favorate.
Step 1 What you'll need
You'll need to install tunctl, which is part of uml-utilities. This is to create the TAP interface on your host system. Off course this is only if you want your host machine to connect to your virtual network, which I think will be 98% of the cases.
Then you'll also need VDE (virtual distributed ethernet). This is the actual virtual network "infrastructure", the virtual LAN switch.
And then you'll need a working KVM virtual machine. Refer to this article of mine on how to set up a KVM virtual machine.
To set up this virtual network, you will need root access to the system you are working on.
Step 2 Creating and configuring your host machine's network interface
In order to connect to the virtual switch, we need a TAP interface on our host machine. A TAP interface is a virtual ethernet interface that transmits ethernet frames through the virtual network. (You'll also encounter TUN while working with virtual networks, this is in turn a virtual P2P interface that is used for creating VPN tunnels, but we won't use them here).
Create the TAP interface as root with the command tunctl.
You'll get some feedback on the name of the interface, usually the first one will be called tap0, the second tap1 and so forth.
You won't neciseraly see the virtual interface with ifconfig before it's been configured. So let's configure it:
# ifconfig tap0 192.168.0.10 netmask 255.255.255.0 up
Now you should see the interface when running ifconfig.
Step 3 Create the VDE virtual LAN switch
Now we can create the virtual LAN switch:
# vde_switch -s /tmp/vde.ctl -tap tap0
We are telling it to create the socket file in /tmp/vde.ctl, and to connect our host machine's interface tap0 to the switch. The socket will be used by the other virtual systems to connect to the switch.
The command line will not return anything, and it will look like it has hung. This actually means the switch is started. Press <enter> to view the VDE switch's prompt.
Step 4 Run the KVM virtual machine and connect it to the vde_switch
I'm using the vde QEMU wrapper to connect my virtual machines to the vde_switch. With the later KVM version (if build with VDE support) can be connected to vde_switch natively using the -net vde option when starting KVM. I personally have not done this yet, so I can't give step by step instructions on this yet.
The wrapper is invoked like this:
# vdeq kvm -hda local_hard_drive -net nic -net vde,sock=/tmp/vde.ctl -m 256
The virtual machine should now be connected to the virtual network. You'll have to configure the virtual machine's network once booted to be able to see it on the virtual network. Also remember to keep the virtual machine's MAC addresses different. When using several KVM virtual machines for example, all VM's will have the same MAC by default, and this will confuse your network and produce unexpected results. Best is to specify the MAC manually.
I hope this information is helpfull for somebody. I surely could have used these information when I was struggling, but once I got it figured out, I felt like I was on top of the world.
Want to master Microsoft Excel and take your work-from-home job prospects to the next level? Jump-start your career with our Premium A-to-Z Microsoft Excel Training Bundle from the new Gadget Hacks Shop and get lifetime access to more than 40 hours of Basic to Advanced instruction on functions, formula, tools, and more.
Other worthwhile deals to check out:
- 97% off The Ultimate 2021 White Hat Hacker Certification Bundle
- 98% off The 2021 Accounting Mastery Bootcamp Bundle
- 99% off The 2021 All-in-One Data Scientist Mega Bundle
- 59% off XSplit VCam: Lifetime Subscription (Windows)
- 98% off The 2021 Premium Learn To Code Certification Bundle
- 62% off MindMaster Mind Mapping Software: Perpetual License
- 41% off NetSpot Home Wi-Fi Analyzer: Lifetime Upgrades