Changes between Initial Version and Version 1 of Version2/VirtualBox


Ignore:
Timestamp:
Sep 4, 2011, 10:22:15 AM (14 years ago)
Author:
adelcastillo
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Version2/VirtualBox

    v1 v1  
     1[[TranslatedPages]]
     2[[TOC(heading=Índice)]]
     3
     4Nota: 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.
     5
     6== Using host networking and NAT with VirtualBox ==
     7
     8I 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.
     9
     10Note 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.
     11
     12I 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
     14I 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
     16Update 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
     20First, we have to create a bridge interface for the VMs. Install the bridge utilities:
     21apt-get install bridge-utils
     22
     23Now make the bridge start on boot. Add the following to /etc/network/interfaces:
     24
     25{{{
     26# VirtualBox NAT bridge
     27auto vnet0
     28iface 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
     36}}}
     37
     38Either reboot or start it manually:
     39{{{
     40ifup vnet0
     41}}}
     42
     43We 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.
     44
     45Note: 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.
     46
     47=== Step 2: Enable IP forwarding ===
     48
     49Now you must tell the kernel to route traffic. Find the ‘net.ipv4.ip_forward’ line in /etc/sysctl.conf, and uncomment it:
     50
     51{{{
     52# Uncomment the next line to enable packet forwarding for IPv4
     53net.ipv4.ip_forward=1
     54}}}
     55
     56And load it:
     57
     58{{{
     59sysctl -p
     60}}}
     61
     62=== Step 3: Setup DHCP and DNS for clients ===
     63
     64OK, 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:
     65
     66{{{
     67apt-get install dnsmasq
     68}}}
     69
     70And edit /etc/dnsmasq.conf to include:
     71
     72{{{
     73interface=vnet0
     74dhcp-range=172.16.0.2,172.16.0.254,1h
     75}}}
     76
     77That’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{{{
     80dhcp-option=option:dns-server,172.16.0.1,208.67.222.222,208.67.220.220
     81dhcp-option=option:domain-name,example.com
     82dhcp-host=08:00:27:00:00:02,vmxp,172.16.0.2       # Windows XP
     83dhcp-host=08:00:27:00:00:03,vmubuntu,172.16.0.3   # Ubuntu
     84}}}
     85
     86This 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
     90Start 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
     97Example:
     98
     99(TODO Add image)
     100Your 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.