VirtualBox

Changes between Initial Version and Version 1 of Automatic_Bridge_Ubuntu


Ignore:
Timestamp:
Mar 19, 2007 9:29:26 AM (17 years ago)
Author:
Michael Thayer
Comment:

Tutorial about bridging on Ubuntu

Legend:

Unmodified
Added
Removed
Modified
  • Automatic_Bridge_Ubuntu

    v1 v1  
     1== HOWTO Automatically configure bridge interfaces on VirtualBox ==
     2
     3Version: 0.1[[BR]]
     4Author: Antonio Augusto da Silva (a.k.a. KhaoticMind)[[BR]]
     5Date: 2007/03/16[[BR]]
     6
     71. Introduction
     8
     9This is a quick tutorial on how to setup your Linux environment and VirtualBox,
     10so that it can automatically create network interfaces and put them in bridge
     11when the VM is launched.
     12
     13In fact the Vbox User Manual has all the details that you need to accomplish it, but i thought it would be better to give some more "hands on" for you guys ;)
     14
     15I've tested this setup in my home computer, running Kubuntu 6.10. I've an ADSL
     16modem to which i connect through my eth0 port. The modem has DHCP, so i get my IP automatically.
     17
     181.1 What is bridging anyway?
     19
     20Well... I thought you would already know it (since you are reading this howto).
     21But hey... let me try to give you a brief explanation. Bridging in the sense we
     22are working here, is to configure your computer, so that, some (or all), of the
     23networks it has connected to it will look like they are in fact one single
     24network.
     25
     26One example of its use is if you have a notebook that can connect to your home
     27computer through a wireless adapter and the home computer is also connect to the Internet (or to any other network) through a cable. To make the notebook able to access the Internet you have to options: routing[1] and bridging[2]. Both solutions will work, but if you do routing only TCP/IP packets will be able to be exchanged (ok, ok, 90% of the world uses TCP/IP but hang with me). On the other hand bridging will give you a lot more of flexibility, since now the notebook will appear is it were direct connect to the other network, just like you. Doing so the notebook can then get its own IP address with DHCP, and you (and anybody on the same network than you) will be able to exchange any packet with the notebook without any problem.
     28
     29What happens here is that your computer was turned in a Switch (or a HUB), and
     30simple connects all machines that are connected to it, like you had cables connection every one of them. To do this on linux you create a "virtual" interface, that will be the actual bridge, and simple "plug" all interfaces you
     31want to be connect to it. So what we will do here is configure a single bridge, and say that our network interface (usually eth0), and all network interfaces created by the VMs, will be connected to it.
     32
     331.2 Prerequisites
     34
     35Before continuing the first thing you're going to need is make sure you have
     36tunctl and brctl in your machine. These are the programs that we will use in the next sections to create our virtual interfaces and our bridge.
     37
     38In Ubuntu these two programs are on the uml-utilities and bridge-utils packages, respectively.
     39
     40To install these packages issue the following command:
     41
     42{{{
     43sudo apt-get install bridge-utils uml-utilities
     44}}}
     45
     462. Configuring the bridge
     47
     48Ok, so lets start with the fun ;)
     49
     50First of all, lets configure the network setup so that you will have a br0
     51interface at boot, and your network interface (eth0) will be part of it.
     52
     53To accomplish this in Ubuntu I've configured my /etc/network/interfaces to look
     54something like this:
     55
     56{{{
     57auto br0
     58iface br0 inet dhcp
     59       bridge_ports eth0
     60
     61auto eth0
     62iface eth0 inet manual
     63}}}
     64
     65Quite simple huh? With this setup you should have what we want. To test it
     66immediately run reinitialise the network:
     67
     68{{{
     69sudo /etc/init.d/networking restart
     70}}}
     71
     72After it finish its job, doing an
     73{{{
     74ifconfig
     75}}}
     76 should show you something like this:
     77
     78{{{
     79br0        Encapsulamento do Link: Ethernet  Endereço de HW 00:11:D8:37:D8:B9
     80          inet end.: 192.168.1.2  Bcast:192.168.1.255  Masc:255.255.255.0
     81          endereço inet6: fe80::211:d8ff:fe37:d8b9/64 Escopo:Link
     82          UP BROADCAST RUNNING MULTICAST  MTU:1500  Métrica:1
     83          pacotes RX:6973 erros:0 descartados:0 excesso:0 quadro:0
     84          Pacotes TX:7301 erros:0 descartados:0 excesso:0 portadora:0
     85          colisões:0 txqueuelen:0
     86          RX bytes:3557787 (3.3 MiB) TX bytes:932172 (910.3 KiB)
     87
     88eth0       Encapsulamento do Link: Ethernet  Endereço de HW 00:11:D8:37:D8:B9
     89          endereço inet6: fe80::211:d8ff:fe37:d8b9/64 Escopo:Link
     90          UP BROADCAST RUNNING MULTICAST  MTU:1500  Métrica:1
     91          pacotes RX:184453 erros:75 descartados:0 excesso:0 quadro:0
     92          Pacotes TX:255573 erros:0 descartados:0 excesso:0 portadora:0
     93          colisões:0 txqueuelen:1000
     94          RX bytes:23372490 (22.2 MiB) TX bytes:136539269 (130.2 MiB)
     95          IRQ:217 Endereço de E/S:0x6000
     96}}}
     97
     98Note that br0 is the interface that gets the IP. The eth0 is like a "dummy"
     99interface.
     100
     1013. Giving the needed access
     102
     103Ok, now we need to give some permissions to devices and files. Note that, as
     104everything involving permissions, there is always some risk involved on it. But
     105hey, its for a good cause!
     106
     1073.1 /dev/net/tun
     108
     109First things first, to allow VBox to automatically create the virtual interface
     110it needs to have write access to /dev/net/tun. The best way I thought of doing it (without doing a chmod 666), was to change the ownership of the device to the vboxusers group. Since everybody that will run VBox will already be part of that group, this looked like a good solution. After that we need to give write
     111permissions to the members of the group. Following are the commands:
     112
     113{{{
     114sudo chown root.vboxusers /dev/net/tun
     115sudo chmod g+rw /dev/net/tun
     116}}}
     117
     1183.2 ifconfig and brctl[[BR]]
     119Besides that you (optionally) need to setuid in the "ifconfig" and "brtcl"
     120commands. Setuid is necessary on these commands because you need to call them on the script used to configure your network, but they both need to be run as root to do what we want.
     121
     122People concerned with this security (like me) would raise an eyebrow with this,
     123since setuids always introduce some risk on the system. In my case I decided the risk was low, and decided to live with it.
     124
     125But if you don't want to use setuid below I will show a way to make the bridging work without needing to do setuids (but having to put root password every time you boot/shutdown a VM).
     126
     127So, to do the setuid run the following commands:
     128
     129{{{
     130chmod +s /sbin/ifconfig
     131chmod +s /usr/sbin/brctl
     132}}}
     133
     1344. The scripts
     135
     136Now that everything is in place lets configure the scripts that will be called
     137by VBox every time a VM is turned on/off. I call these scripts tapUP and
     138tapDown, but you can name them whatever you want. The commands i use are the
     139same as described in the VBox User Manual, with the exception that i don't use
     140sudo (since i did a setuid in ifconfig and brctl).
     141
     142TapUP:
     143{{{
     144#!/bin/sh
     145/sbin/ifconfig $2 up
     146/usr/sbin/brctl addif br0 $2
     147}}}
     148
     149TapDown:
     150{{{
     151#!/bin/sh
     152/usr/sbin/brctl delif br0 $2
     153}}}
     154
     155If you decided to not do the setuid in step 3.2, you can use kdesu (for
     156example), to ask root password when running these commands. The files would look something like this:
     157
     158TapUP:
     159{{{
     160#!/bin/sh
     161kdesu "/sbin/ifconfig $2 up && /usr/sbin/brctl addif br0 $2"
     162}}}
     163
     164TapDown:
     165{{{
     166#!/bin/sh
     167kdesu /usr/sbin/brctl delif br0 $2
     168}}}
     169
     170I put both command between commas and use the "&&" so that it asks for the
     171password only once.
     172
     1735. Wrapping it up
     174
     175Well, thats it! Quite easy huh?  Now all you need to do is create and virtual machine, go on its "Network" options, change the "Attached to" to "Host Interface", give the Interface a name (each machine should use a different interface!), and point the "Startup Application" and "Terminate Application", to your TapUP and TapDown files.
     176
     177I've tested it with a Windows guest, and everything worked like a charm.
     178
     1796. Thanks
     180
     1817. Changelog
     182
     1830.1 -> Initial version
     184
     1858. References
     186
     187[1] http://en.wikipedia.org/wiki/Routing[[BR]]
     188[2] http://en.wikipedia.org/wiki/Bridging_%28networking%29

© 2023 Oracle
ContactPrivacy policyTerms of Use