November 30, 2008 · Networking · Email This Post

Sponsored Link
We have already discussed how to install virtualbox and create virtual machines in ubuntu.This tutorial will explain How to set up host interface networking for VirtualBox on Ubuntu.

To start, NAT is by far the easiest way to get your guests connected to the interweb, but you may want to use the guests as servers. For this you need Host Networking.

To configure Host Networking you need to configure network bridging, you basically go through four steps on the host machine

  • Install necessary packages
  • Declare bridge and real network interface you add to it
  • Declare virtual interfaces
  • Set permissions on /dev/net/tun

Preparing Your system

You need to install the following packages

sudo apt-get install uml-utilities bridge-utils

Now you need to take the backup of your networking file using the following command

sudo cp /etc/network/interfaces /etc/network/interfaces.backup

You have to edit /etc/network/interfaces on the host machine to declare the bridge, this procedure is slightly different if your host use static or dynamic IP.

sudo gedit /etc/network/interfaces

If you have Dynamic IP, on the host machine:

auto eth0
iface eth0 inet manual

auto br0
iface br0 inet dhcp
bridge_ports eth0 vbox0

# The loopback network interface
auto lo
iface lo inet loopback

"eth0″ is the name of your interface, it can be different depending on your machine.

"br0″ is an arbitrary name for the bridge.

"vbox0″ is an arbitrary name for the device VirtualBox will use, if you want more devices, you just add then like:

bridge_ports eth0 vbox0 vbox1 vbox2 vbox3 vbox4 and so on.

If you are using Static IP addresses modify the interfaces like this:

auto eth0
iface eth0 inet manual

auto br0
iface br0 inet static
address 192.168.0.100
netmask 255.255.255.0
gateway 192.168.0.1
bridge_ports eth0 vbox0 vbox1

# The loopback network interface
auto lo
iface lo inet loopback

Replace 192.168.0.100 with your IP, 255.255.255.0 with your netmask and 192.168.0.1 with your gateway. If you are using static IP addresses for eth0, eth1, etc; you will need to configure the br0 interface using the same settings! Otherwise your system will request an IP address via DHCP. You may add as many vbox# interfaces as you wish as long as they are also declared in /etc/vbox/interfaces; keep reading for details.

Save and exit the file.

You need to restart networking for the changes to take effect using the following command

sudo /etc/init.d/networking restart

Declare virtual interfaces in VirtualBox network file

To declare the virtual interfaces used by VirtualBox you need to edit /etc/vbox/interfaces on the host machine

sudo gedit /etc/vbox/interfaces

# Each line should be of the format :
# <interface name> <user name> [<bridge>]
vbox0 <your user name> br0
vbox1 <your user name> br0

Where "vbox#" is an arbitrary name. You may declare here as many virtual interfaces as you wish, as long as they have been properly declared in /etc/network/interfaces. In this specific article, we declared two vbox interfaces in /etc/network/interfaces, then finished setting them up in /etc/vbox/interfaces.

To take the modifications into account, restart the VirtualBox host networking script. If you installed VirtualBox OSE

sudo /etc/init.d/virtualbox-ose restart

If you installed the pre-compiled proprietary version

sudo /etc/init.d/vboxnet restart

The virtual interfaces are now created and added to the bridge.

That's it! Now the different scripts will take care of cleanly create/configure/remove bridges and virtual interfaces when you boot and shut your system down.

Set permissions on /dev/net/tun

You need to have read/write permissions on the file /dev/net/tun to be able to use the bridge, to set permissions

sudo chown root:vboxusers /dev/net/tun

sudo chmod g+rw /dev/net/tun

This file is created with the default permissions every time the system restarts, to make the new permissions permanent you have to edit the file /etc/udev/rules.d/20-names.rules and change

KERNEL=="tun",                          NAME="net/%k"

to

KERNEL=="tun",                          NAME="net/%k",  GROUP="vboxusers",     MODE="0660″

Configure networking in VirtualBox

Once you have everything ready, you can start the VirtualBox management interface on the host machine, configure the network of your virtual machine, and by selecting "host networking", enter the name of one of the virtual adapter you have configured. Start your virtual machine, it gets a network card presented, that you can set up as you wish (static IP address, DHCP) using the network configuration tools inside the virtual machine.

Sponsored Link

Incoming search terms:

Related posts

29 Comments to “How to set up host interface networking for VirtualBox on Ubuntu”

  1. malaeum says:

    Should the instruction “gedit /etc/init.d/networking restart” be “sudo /etc/init.d/networking restart”? :-)

    [Reply]

  2. admin says:

    @Malaeum

    Thanks for your comment i have updated the article.

    [Reply]

  3. Swapnil Jain says:

    If you do not want to edit you /etc/network/interface file than you can create a shell script and run it before starting the guestos in virtualbox as i did.


    tunctl -t tap1 -u
    brctl addbr br0
    ifconfig eth0 0.0.0.0 promisc
    brctl addif br0 eth0
    ifconfig br0 192.168.100.77 netmask 255.255.255.0
    #--- or "dhclient br0" ---- if you use dhcp server on your network
    brctl addif br0 tap1
    ifconfig tap1 up
    ;-)

    [Reply]

  4. Belzecue says:

    “… but you may want to use the guests as servers. For this you need Host Networking.”

    No you don’t. You CAN go to all that trouble, OR you can use the very simple instructions provided in the Virtualbox help manual (See Section 6.4.2 “Configure Port Forwarding With NAT”. The instructions work the same on any host, e.g. Windows or Linux, and I have done this quickly and flawlessly many times to get a guest service connected, e.g. exposing a webserver on the guest.

    —–
    You can set up a guest service which you wish to proxy using the command line tool VBoxManage. You will need to know which ports on the guest the service uses
    and to decide which ports to use on the host (often but not always you will want to use the same ports on the guest and on the host). You can use any ports on the host
    which are not already in use by a service. An example of how to set up incoming NAT connections to a ssh server on the guest requires the following three commands:

    VBoxManage setextradata “Linux Guest” “VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestweb/Protocol” TCP

    VBoxManage setextradata “Linux Guest” “VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestweb/GuestPort” 80

    VBoxManage setextradata “Linux Guest” “VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestweb/HostPort” 16080

    —–

    The above assumes a Linux Guest named (in Virtualbox) “Linux Guest”, and routes all TCP traffic on the Host port 16080 through the Guest port 80. The “guestweb” is any arbitrary name you want to assign to this NAT rule. Users hit http://hostname:16080/ and they get the Guest webserver. Voila.

    It really is that easy. See the Virtualbox doco for more.

    [Reply]

  5. ErroneousBosch says:

    These instructions, while useful, are frankly unnecessarily complex. TAP is a much simpler way of building the bridge, and requires much less tinkering and are a surer way to get the result desired.

    The Virtualbox manual’s instructions for setting up tap interfacing are, while somewhat poorly written, more than adequate. See section 6.8.1 of the VB manual.

    I don’t like NAT for doing a virtual server personally, and much prefer using host interfacing. NAT gets into some funky black magic to get a virtualized server to run properly

    [Reply]

  6. neeltje57 says:

    Thank you for these instructions, it works perfectly.

    [Reply]

  7. ssmokn says:

    This worked perfectly. I also had to add my userid to the vboxusers group. Thanks.

    [Reply]

  8. Antonio Censi says:

    VirtualBox 2.1, launched yersterday, simplifies this process, without so much tweaking. Give it a try.

    [Reply]

  9. markba says:

    @ErroneousBosch: “These instructions, while useful, are frankly unnecessarily complex…”

    You’re right, it does not look that simple, but once it is setup, it works like charm.

    For avoiding the tedious manual configuration, I developed VBoxTool so this can be done automagicilly with one config file: http://vboxtool.sourceforge.net/

    Even the enhancement in host interfacing in the last version of VBox (2.1.0) leave some advantages for port forwarding, which I’ve pointed out here: http://vboxtool.sourceforge.net/portforwarding.html
    (Note that I have not adjusted this page for much simpler host interfacing in VBox 2.1.0)

    [Reply]

  10. Sid Boyce says:

    I found with 2.1.0 which I installed yesterday that networking in the VM allows using host adaptors. I have 4 boxes and the VM’s get their IP addresses via DHCP from the Smoothwall firewall box, so they are seen as just other boxes on the local network. All without messing with bridging, I simply selected host networking and eth0 in each VM config.
    I can now do network printing from the VM’s, file transfers, ssh, etc.
    Nice work from Sun for a change – I’ve suffered Solaris and SPARC for many years.

    [Reply]

  11. Mark H says:

    Thanks for the help this is was I needed for some time. However, I use ufw as a firewall and cant seem to get ufw and virtualbox configured per the above to play together nicely. Can you provide some insight on how to configure ufw to allow virtualbox to work when bridged per your instructions? Currently Vbox does not have internet/network connectivity when ufw is enabled.

    Thanks in advance.

    [Reply]

  12. Mark H says:

    I followed the instructions on Ubuntugeek to install Virtualbox 2.1 and removed all of the bridging in the tutorial above which solved my problems. Works on wlan0 as well and is a whole lot easier. uwf and Virtualbox are working together. Thanks and keep up the good RSS info.

    [Reply]

  13. krisna says:

    i have follow this article, but i receive this error:

    Failed to power up VM! Error info:
    Failed to initialize Host Interface Networking.
    VBox status code: -3100 (VERR_HOSTIF_INIT_FAILED)

    any one can help?

    [Reply]

  14. Duke says:

    Outstanding write up. thanks a million! every step worked perfectly on the first try.

    [Reply]

  15. Murky says:

    I’m getting something similar:

    Unknown error creating VM (VERR_HOSTIF_INIT_FAILED).
    VBox status code: -3100 (VERR_HOSTIF_INIT_FAILED).

    Result Code:
    0×80004005
    Component:
    Console
    Interface:
    IConsole {1dea5c4b-0753-4193-b909-22330f64ec45}

    [Reply]

  16. shally87 says:

    a-liew@Yumie:~$ sudo /etc/init.d/virtualbox-ose restart
    * Shutting down VirtualBox host networking… [ OK ]
    * Starting VirtualBox host networking… Warning – failed to create the interface vbox0 for the user dns
    Warning – failed to create the interface vbox1 for the user lamp
    Warning – failed to create the interface vbox2 for the user mail-Server
    Warning – failed to create the interface vbox3 for the user leoserver
    [ OK ]

    I guesss i got this error while i was working with it.

    [Reply]

  17. shally87 says:

    any fix for that??

    [Reply]

  18. Dorijan Santini says:

    After following steps mentioned above (working on Debian Lenny) I also got
    error “VBox status code: -3100 (VERR_HOSTIF_INIT_FAILED).” But I found
    that if you put vbox (for example vbox0) in “Interface Name” under
    Host Interface Settings it fixes problem.

    Here is screen dump of working example: http://www.dorijan.com/vboxhostinterface.jpg

    [Reply]

  19. shally87 says:

    Now that i try again, i get it fixed.. by the way you should state clearly;

    # Each line should be of the format :
    # []
    vbox0 br0
    vbox1 br0

    the ‘your user name’ part people might get confused. So why not put host machine user name or ‘host user name’.. I get it fixed when i change it to the host user name.

    [Reply]

  20. David Lewis says:

    Is that it – I just replace the contents of interfaces file with

    auto eth0
    iface eth0 inet manual

    auto br0
    iface br0 inet dhcp
    bridge_ports eth0 vbox0

    # The loopback network interface
    auto lo
    iface lo inet loopback

    ????????????????????????????????

    [Reply]

  21. David Lewis says:

    What else is supposed to be in my interfaces file? Scared of doing this and destroying my internet connection.

    [Reply]

  22. Basil says:

    I did all according to plan – nothing working. Interfaces vbox0, vbox1, etc not seen…

    [Reply]

  23. Dr.Allcome says:

    Good work, thank you :).

    I had the mentioned problem under Ubuntu 8.04 with Virtual Box OSE 1.5.6. I as well had VM-Ware on this machine running.

    I learned something and could fix the issue with this manual.

    [Reply]

  24. Jason says:

    The “/etc/vbox/interfaces” and “/etc/udev/rules.d/20-names.rules” files do not appear to exist when running Ubuntu 9.10 and Virtualbox 3.0.10 r54097. Have they been moved or is there another way to accomplish those steps?

    Jason

    [Reply]

  25. severn says:

    Way to go, tex, this broke my install permanently.

    Thanks.

    [Reply]

  26. AirClyde says:

    On ur virtualbox console go to “File” and the go to “Preferences” then click the “Network” and then “Host-only” u add “host only network” then ur interfaces “vbox0″ “vbox1″ “vbox2″ “vbox3″ should appear. Once u add it will appear on the vbox guest machine by now. Go back to the vbox guest machine click “Settings” go to “Network” on “Adapter 1″ set it to “Bridge Adapter” then the “Name” click it to scroll down “vbox0..vbox1..vbox3…should appear without any problem…cheers !!!

    [Reply]

  27. Allan says:

    Hi,

    Thank you for the guide, it is very clear and concise.

    I was stuck on the bridge interface for the vbox and was getting the error:
    “Vbox status code:-3100 (VERR_HOSTIF_INIT_FAILED)”

    Following your guide solved the error.

    Thanks again.

    Allan

    [Reply]

  28. Dave says:

    kept getting error vboxnetadpctl when trying to add host-only network. This fixed it.

    sudo insmod /lib/modules/2.6.31-21-generic/updates/dkms/vboxnetadp.ko

    [Reply]

  29. djegsi says:

    Once you have everything ready, you can start the VirtualBox management interface on the host machine, configure the network of your virtual machine, and by selecting “host networking”, enter the name of one of the virtual adapter you have configured. Start your virtual machine, it gets a network card presented, that you can set up as you wish (static IP address, DHCP) using the network configuration tools inside the virtual machine

    Everything gone well but this last part l do not understand ????

    [Reply]

Leave a Reply