Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

In setting up virtual machines with VirtualBox, I often want the following characteristics

  • vm has a static ip
  • host can access vm without port forwarding
  • vm can access the internet
  • I can move my laptop from network to network (e.g. from home to office to coffeeshop) without worrying about securing or reconfiguring the vm

None of the VirtualBox network connection methods staisfies these requirements on their own.

  • NAT
    Requires port forwarding if you want to connect to the vm from the host.

  • Host-only
    The vm can not access the internet, unless the host is a router.

  • Bridged
    Exposes the vm to the network; not portable.

share|improve this question

4 Answers 4

up vote 15 down vote accepted

I can get the setup I want by setting up two adapters on the vm.

VirtualBox 4.2.12
Ubuntu 12.04 guest

In VirtualBox > Preferences > Network, set up a host-only network.

Mine is called vboxnet0, it is manually configured:
ip 192.168.56.1
netmask 255.255.255.0
no dhcp

VirtualBox network configuration VirtualBox network configuration

Then, in the network settings for the virtual machine, set up two adapters:

Adapter 1
host only, vboxnet0

Adapter2
NAT

Boot the virtual machine and log in through the console VirtualBox provides.

Run this to see your adapters:

ls /sys/class/net

In my case the adapters were named eth1 and eth2 (and lo, the loopback interface).

Then, edit your network configuration.

sudoedit /etc/network/interfaces


# The loopback network interface
auto lo
iface lo inet loopback

# Host-only interface
auto eth1
iface eth1 inet static
        address         192.168.56.20
        netmask         255.255.255.0
        network         192.168.56.0
        broadcast       192.168.56.255

# NAT interface
auto eth2
iface eth2 inet dhcp

Note that eth1 has no default gateway specified. eth2 will get a default gateway from dhcp.

share|improve this answer
    
Now to reach the guest from your host, did you have to add a route and a name in /etc/hosts on the host machine? Your host doesn't have an interface on the 192.168.56.0 net, does it? –  Mojo Jan 10 at 18:29
    
No! VirtualBox creates an interface on the host for that network, for you. (Never mind.) –  Mojo Jan 10 at 19:20
    
Update: VirtualBox 4.3 introduced a NAT service that looks like it might obviate the need for two interfaces. 6.4. Network Address Translation Service –  Christian Long Jan 26 at 17:36
1  
This helped me a lot, I use Virtualbox for development, and when I'm in my house, everything works correctly since I have the network setup to my likings, the nightmare starts when I move to another network (family, companies...), I have to change websites URLs, delete some systems files, reboot I don't know how many times and the list goes on. This solution doesn't care about your current network which is why I like it...Just plug your PC to any network and focus on your work. –  Nabil Kadimi Jun 14 at 11:22
    
Thanks, huge help! –  JSON Dec 11 at 6:55

Yeah I had this problem it was a total pain! But I solved it by simply installing Squid Cache Proxy server on my physical PC, and that way - my host-only internet virtualbox PCs could connect to the internet !

I did a quick 3 minute guide here - for anyone who want's to how it works ! http://b0zmeister.wordpress.com/allowing-host-only-virtualbox-guest-to-connect-to-the-internet/

share|improve this answer
    
That worked great for me, thanks for sharing. It's by far the simplest way to get it all working. –  this.lau_ Dec 28 '13 at 11:22
    
Hi Laurent - no problems - glad it helped! Gotta love running the virtualbox PCs off a different network - but - with internet connection working :) –  B0zmeister Jan 4 at 18:32

I could solve my problem with a mix of Christian Long solution. I added 2 adapters:

Adapter 1 - NAT

Adapter 2 - host only, vboxnet0

The only diference was in VM's interfaces file:

sudoedit /etc/network/interfaces

# The loopback network interface
auto lo
iface lo inet loopback
# NAT
auto eth0
iface eth0 inet dhcp
# Host only
auto eth1
iface eth1 inet dhcp

In VirtualBox Network config I left DHCP checked.

After a VM reboot everything worked fine.

share|improve this answer
    
This works for me, but I want the VMs to have static IP addresses. The minute I do that, Internet stops working. If I set up both with DHCP, it works fine. How do I make the host-only interface get a static IP? –  Umar Farooq Khawaja Apr 7 at 23:27

I Just added 2 adapters:

Adapter 1 host only, vboxnet0

Adapter2 NAT

And it works perfect, I can access the virtual machine from the host, and I have internet on the vm.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.