Changes between Version 2 and Version 3 of Version2/VirtualBox


Ignore:
Timestamp:
Sep 12, 2011, 5:23:23 PM (13 years ago)
Author:
adelcastillo
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Version2/VirtualBox

    v2 v3  
    22[[TOC(heading=Índice)]]
    33
    4 Nota: El contenido de este wiki ha sido copiado de http://www.tolaris.com/2009/03/05/using-host-networking-and-nat-with-virtualbox/ con el objetivo de que no se pierda la información en caso de que la web cierre.
    54
    6 == Using host networking and NAT with VirtualBox ==
     5== Requisitos ==
    76
    8 I use VirtualBox every day. The satellite world is infested with bad Windows-based management tools that fail to run in Wine. So I often run those apps in a Windows virtual machine, safely sandboxed the way Windows belongs.
     7Una vez instalado !VirtualBox a través del gestor de paquetes de tu distribución procedemos a configurarlo para usarlo como cliente Opengnsys. Estos pasos se han probado en !ArchLinux usando la versión 4.1.2 de !VirtualBox.
    98
    10 Note to hardware developers: if your network-based device does not have a standards-compliant HTTP interface, you lose. If it has a Windows-based management tool instead, you lose twice. I will buy your product only if I have no other choice.
     9== Módulos ==
    1110
    12 I imagine running Windows apps is what 90% of VirtualBox users use it for, but it can do so much more than that. I also run several Linux-based VMs, and use them to test server configs, or even whole networks before rolling out the real thing. If you do this, you probably want to use more than the basic NAT networking that VirtualBox uses by default. For instance, wouldn’t it be nice to install an SSH server in the VM, minimise the VirtualBox GUI, and SSH in from a terminal just like you would a real server?
    13 
    14 I assume you are using VirtualBox 2.1.4 from a Linux host running Ubuntu 8.04 “Hardy Heron”. Similar commands can be used on any recent Debian or Ubuntu release. You’ll have to adapt some things to use it on RPM- or source-based Linux distributions. Assume all commands are run as root (directly or with sudo).
    15 
    16 Update 2010-08-18: These instructions are still valid as of VirtualBox 3.2.8 and Ubuntu 10.04 “Lucid Lynx”. VirtualBox now creates a “vboxnet0″ interface by default, but this is not a bridge. Follow the instructions below.
    17 
    18 === Step 1: Create a bridge interface ===
    19 
    20 First, we have to create a bridge interface for the VMs. Install the bridge utilities:
    21 apt-get install bridge-utils
    22 
    23 Now make the bridge start on boot. Add the following to /etc/network/interfaces:
     11Hay que asegurarse de que los módulos: ''vboxdrv, vboxnetflt y vboxnetadp'' se encuentren cargados. Añádelos a ''/etc/modules'' (o ''/etc/rc.conf'' para !ArchLinux) para asegurar que lo estén en cada arranque:
    2412
    2513{{{
    26 # VirtualBox NAT bridge
    27 auto vnet0
    28 iface vnet0 inet static
    29         address 172.16.0.1
    30         netmask 255.255.255.0
    31         bridge_ports none
    32         bridge_maxwait 0
    33         bridge_fd 1
    34         up iptables -t nat -I POSTROUTING -s 172.16.0.0/24 -j MASQUERADE
    35         down iptables -t nat -D POSTROUTING -s 172.16.0.0/24 -j MASQUERADE
     14vboxdrv
     15vboxnetflt
     16vboxnetadp
    3617}}}
    3718
    38 Either reboot or start it manually:
     19También se pueden cargar manualmente con el comando ''modprobe'', por ejemplo:
     20
    3921{{{
    40 ifup vnet0
     22# modprobe vboxdrv
     23}}}
     24== Configuración de VirtualBox ==
     25 
     26Lo primero que necesitamos es la ''Oracle VM VirtualBox Extension Pack''. Se puede descargar desde http://www.virtualbox.org/wiki/Downloads. Una vez descargado abrimos !VirtualBox y seleccionamos en el menú: ''File -> Preferences''. Se nos abrirá una ventana donde seleccionamos la opción ''Extensions''. Le damos al botón de añadir una y buscamos el archivo descargado. Tardará unos instantes en instalarse. Sin cerrar la ventana de preferencias, seleccionamos ahora la opción ''Network''. Añadiremos una nueva interfaz de red que probablemente se llame ''vboxnet0''. Podemos editarla y rellenar con estos datos por ejemplo:
     27
     28{{{
     29IPv4 Address: 172.16.0.1
     30IPv4 Netmask: 255.255.255.0
    4131}}}
    4232
    43 We now have a bridge interface to which VirtualBox can attach virtual machines. That traffic will be NATed to your host’s IP address when the guest OS accesses the Internet. However, the traffic won’t yet route.
     33El resto de datos no tienen porque rellenarse y el servidor DHCP lo desactivamos. Ya podemos cerrar la ventana de preferencias.
    4434
    45 Note: if you are already using a firewall such as iptables, shorewall, or ufw, you should remove the two iptables lines above and add equivalent commands to your firewall configuration. Otherwise NAT will probably not function.
     35== Configuración de dnsmasq ==
    4636
    47 === Step 2: Enable IP forwarding ===
     37En el fichero de configuración /etc/dnsmasq.conf tenemos que fijarnos en que al menos la interfaz que se va a usar  sea la que acabamos de crear con !VirtualBox ''vboxnet0''. También se puede añadir maquinas para que tengan IP fijas:
    4838
    49 Now you must tell the kernel to route traffic. Find the ‘net.ipv4.ip_forward’ line in /etc/sysctl.conf, and uncomment it:
     39{{{
     40interface=vnet0
     41dhcp-range=172.16.0.2,172.16.0.254,1h
     42dhcp-option=option:router,172.16.0.1
     43dhcp-option=option:dns-server,80.58.0.33,80.58.32.97
     44dhcp-option=option:domain-name,example.com
     45
     46dhcp-host=aa:bb:cc:dd:ee:02,172.16.0.2
     47dhcp-host=aa:bb:cc:dd:ee:03,172.16.0.3
     48}}}
     49
     50Que no se nos olvide reiniciar este servicio después de aplicar los cambios.
     51
     52== Configuración de maquinas virtuales ==
     53
     54Accedemos a la configuración de una maquina virtual que haya sido creada dándole al botón ''Settings'' después de seleccionarla estando apagada. Le damos a la opción ''System''. Habilitamos la opción ''Network'' dentro de la opción ''Boot order'' y la ponemos en primer lugar. Luego nos vamos a la opción ''Network'' y ponemos a ''Attached to:'' a ''Host-only'' y la interfaz a la que creamos anteriormente, probablemente ''vboxnet0''. También podemos aprovechar para cambiar la MAC y usar alguna que hayamos configurado como IP fija en dnsmasq.
     55
     56Con esto es suficiente para que las máquinas virtuales arranquen por red. Si queremos además que tengan internet hay que seguir el siguiente paso.
     57
     58== Configuración para ofrecer internet ==
     59
     60En el fichero /etc/sysctl.conf buscamos la línea ‘net.ipv4.ip_forward’ y la descomentamos para que quede así:
    5061
    5162{{{
     
    5465}}}
    5566
    56 And load it:
     67Para que tenga efecto sin tener que reiniciar podemos usar el comando:
    5768
    5869{{{
    59 sysctl -p
     70# sysctl -p
    6071}}}
    6172
    62 === Step 3: Setup DHCP and DNS for clients ===
    63 
    64 OK, now you can forward and NAT traffic from client VMs. But you still have to configure static IPs in each guest’s OS. Here is where DNSMasq shines. It provides an all-in-one DHCP/DNS server in a small footprint. Install it:
     73Además hay que arreglar esta regla de iptables:
    6574
    6675{{{
    67 apt-get install dnsmasq
     76# iptables -t nat -I POSTROUTING -s 172.16.0.0/24 -j MASQUERADE
    6877}}}
    6978
    70 And edit /etc/dnsmasq.conf to include:
    71 
    72 {{{
    73 interface=vnet0
    74 dhcp-range=172.16.0.2,172.16.0.254,1h
    75 }}}
    76 
    77 That’s all you really need, but you may want to explicitly define DNS servers and domains for the guests, or static assignments. Add:
    78 
    79 {{{
    80 dhcp-option=option:dns-server,172.16.0.1,208.67.222.222,208.67.220.220
    81 dhcp-option=option:domain-name,example.com
    82 dhcp-host=08:00:27:00:00:02,vmxp,172.16.0.2       # Windows XP
    83 dhcp-host=08:00:27:00:00:03,vmubuntu,172.16.0.3   # Ubuntu
    84 }}}
    85 
    86 This defines the host OS and the OpenDNS servers as the DNS servers (instead of passing on whatever your host OS uses), tells all guests they are part of the domain example.com, and defines two static assignments by MAC address.
    87 
    88 === Step 4: Set up the virtual machine ===
    89 
    90 Start the VirtualBox interface, and edit your virtual machine’s settings.
    91  1. Choose “Network”.
    92  1. Enable a network adaptor.
    93  1. Under “Attached to:”, select “Host Interface”.
    94  1. If you assigned a static DHCP assignment above, be sure to set the same MAC address.
    95  1. Under “Host Interfaces”, select the bridge you created in step 1, vnet0.
    96 
    97 Example:
    98 
    99 (TODO Add image)
    100 Your virtual machines will now automatically receive an IP address in the 172.16.0.0/24 network, will resolve DNS, will NAT to your host’s external IP address, and can directly address each other.
     79Si se quiere que se ejecute al inicio en cada arranque se deberá configurar manualmente según la distribución.