TransWikia.com

Virtualbox NAT and host-only connection

Super User Asked by Beach Chicken on January 17, 2021

Because I’m working on multiple locations, I experience problems with my development servers. Because work on a variety of networks I do not want to configure the networking settings of my virtual machines every time I change network. Sometimes the network requires me to use DHCP. (public libraries and such)

Also I have to change network adapter often, I prefer ethernet but sometimes I have to work wireless.

So a lot of different networks and even different adapters.

Requirements

  • Internet connection
  • Access Host machine from Guest machine
  • Access Guest Machine from Host machine

I am looking for a configuration so I dont have to change my (virtual machines) network settings. Because I had to changed network (adapter)

Specs

host machine: Ubuntu, Virtualbox
guest machine: debian.
wireless interface: wlp2s0
eth adapter: eth1

Attempt #1

Just use NAT.

  • Added a NAT adapter to Virtualbox: (10.0.2.0/24, DHCP enabled)
  • Added a NAT adapter to the Guest machine.
  • Configure guest interface:
    • auto eth1
    • allow-hotplug eth1
    • iface eth1 inet static
    • address 10.0.2.5
    • netmask 255.255.255.0
    • gateway 10.0.2.2
    • network 10.0.2.0
    • broadcast 10.0.2.255

With this configuration the following works:

  • Internet connection
  • Access Host Machine from Guest machine

With this configuration, the following does not work:

  • Access Guest Machine from Host Machine

This seems to be a limitation when using NAT.

Attempt #2

NAT in combination with Bridged connection:

  • Virtualbox NAT Adapter (10.0.2.0/24, DHCP enabled)
  • Virtualbox Bridged Adapter (192.168.56.1/24, DHCP enabled)

Because sometimes I have to switch from wireless to Ethernet I also tried to configure a bridged adapter for both the network adapters on the Host Machine.

Host Machine IP: 192.168.56.101
Guest Machine IP: 192.168.56.100

With this configuration, the following does work:

  • Internet connection
  • Access Host Machine from Guest machine

With this configuration, the following does not work:

  • Access Guest Machine from Host Machine

This example has a problem because for a bridged connection the host machine IP has to be set in the range of the IP range of the bridged adapter. Sometimes I can’t because i am required to use DHCP.

Given the details of my attempts, do you guys can advise me a alternative configuration for achieving my goals?

3 Answers

In version 5 of VirtualBox, there is a conflict that surfaces when you use both NAT and Host-Only together. You will see that the VM's Host-Only adaptor does not have an IP address assigned by DHCP.

There are 2 possible solutions:

  1. Install the latest version of VirtualBox
  2. Replace both NAT and Host-Only by a single "Bridged"

References from Chapter 6 of VirtualBox documentation:

NAT

A virtual machine with NAT enabled acts much like a real computer that connects to the Internet through a router. The router, in this case, is the Oracle VM VirtualBox networking engine, which maps traffic from and to the virtual machine transparently.

The disadvantage of NAT mode is that, much like a private network behind a router, the virtual machine is invisible and unreachable from the outside internet.

Answered by Joshua Kan on January 17, 2021

I was struggling with this problem till a few days back. To fix this, a simple setup would require using two adapters on the guest.

  1. NAT: This would enable communication from the guest to external networks and also guest to host. To view the IP to use for communicating with the host, view the host’s IP in the ifconfig / ipconfig output on the guest. This is the gateway for the NAT network. This would not show up in ifconfig on the host since a VirtualBox creates a NAT network for each of the VMs.

  2. Host Only: This would allow communication between the host and the guest. By default VirtualBox assigns guest IPs using DHCP, but you can assign static IP for the guest using an IP below 192.168.56.100

You may also set up a third optional adapter internal if you have more than one guest and you would like to communicate between guests. Here is the question I had asked last week regarding this issue: *Edit* Make virtualbox host only network bidirectional

Answered by Ravi on January 17, 2021

OK, so there are 2 ways of sort of doing this.

1) Use the VirtualBox NAT network on the client. This doesn't provide a direct route from your host machine, but you can set preferences to forward ports. Kind of a pain to set up, but if the host machine was Windows or a Mac this is what I'd probably do, mostly because I'm not sure on firewalling/natting/routing capabilities on those systems (pretty sure Mac wouldn't have an issue, but Windows is unknown to me)

2) Use the VirtualBox Host Only Network. From your description, this may be better. Be aware that you will need to change the firewall script to point to whatever device is connected.... Here's teh details.

On the host machine, first set up the Host Only Network in your vbox preferences. In the Manager, click File and then Preferences, and then select Network. Flip over to Host Only, and then add one if needed. Here's what mine looks like: hostonly network tab 1

Hostonly tab2

Next, on the host, configure the vboxnet0 interface. It automagically just happens for me, setting my host machine to the .1 address in the subnet. Here's output of ifconfig on my host, referencing my ethernet and vboxnet0 address.

ifconfig info on host machine

In your guest machine, set the network to some address in your subnet (even with the DHCP server enabled, this makes it easier to get gateway/dns server) and set a nameserver in the usual way (ie, edit /etc/network/interfaces and set the static address/netmask/gateway there, then edit /etc/resolv.conf and set a name server there).

At this point, your host machine and your guest machine should be able to communicate back and forth with no issues. You may want to put appropriate entries in /etc/hosts so you don't have to enter the IPs everywhere...

The last step is to get internet going for the guest. This is a simple matter of turning the host into a router. Since the vboxnet0 interface doesn't come up until vbox is up and running, you don't want to do this at boot time for the host.

A simple firewall script to allow the guest vm to access the world, and the host machine can still access the world and the guest in the vboxonly network. Be aware that this script could be MUCH more secure. As it is, it would accept some client on whatever network the host is connected to using your host machine as a gateway to the guest vm.

#!/bin/bash

echo 1 > /proc/sys/net/ipv4/ip_forward

iptables=`which iptables`

$iptables -F

/sbin/iptables -t nat -A POSTROUTING -o enp2s0 -j MASQUERADE

$iptables -A FORWARD -i enp2s0 -o vboxnet0 -j ACCEPT
$iptables -A FORWARD -i vboxnet0 -o enp2s0 -j ACCEPT

Answered by ivanivan on January 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP