0

For a program I'm installing on the host machine, which requires results from a VM, I need to set my VM (https://utopianknight.com/malware/cuckoo-installation-on-ubuntu-20/) as host-only. Normally, host-only should have no internet, however, in the tutorial I need to follow some steps to give internet access to the VM:

vboxmanage hostonlyif create

vboxmanage hostonlyif ipconfig vboxnet0 --ip 192.168.56.1

The vboxnet I/F is then selected for the VM. Now using iptables, the following network forwarding rules are set up to provide internet connection to the VM:

sudo iptables -A FORWARD -o eth0 -i vboxnet0 -s 192.168.56.0/24 -m conntrack --ctstate NEW -j ACCEPT

sudo iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

Now, IP forwarding in the kernel so that these settings are set to Active (required for WWW Internet access):

echo 1 | sudo tee -a /proc/sys/net/ipv4/ip_forward

sudo sysctl -w net.ipv4.ip_forward=1

My questions are:

  1. I would like to know what is going on here exactly and how do these rules allow a host-only VM to have internet when, otherwise, it shouldn't.

  2. I have another VM configured with bridged networking. My host is able to ping my host-only VM, but my bridged VM is not able to ping the host only VM even though both host machine (not host VM) and the bridged VM are in the same subnet. Therefore why can't the bridged VM ping the host-only VM? Why do I need to add the host-only interface (vboxnet0) as a 2nd interface to my bridge VM so it can ping the host-only VM?

I would really appreciate it if someone could clarify this to me. Thank you in advance.

1 Answer 1

2

Normally to understand things we split networking to the layers (in the OSI meaning) and abstract away from the unrelated details belonging to other layers. In layer 1 (physical), we have a host which is physically connected to some network. In layer 2, however, we have two systems here: a host with two Ethernet interfaces (one of which is happened to be eth0 and the other vboxnet0) and a VM with one. The host connected to WAN on via eth0, and the vboxnet0 is connected to the VM's interface.

This is how it should be understood. Abstract away for a minute from all those details about virtual machines, virtualization hosts, "host only" networking and its perceived "restrictions" and so on. This is how your network looks like (albeit, virtual, but the network is always virtual, it's just an imaginary layer above the physical things):

(Internet) --- [eth0] Host [vboxnet0] --- [eth0] VM

So, assign some addresses to eth0 and to vboxnet0, and make sure those are from different subnets. VM's vNIC eth0 should have an IP from the same subnet as the vboxnet0 address, so your VM and host can communicate.

You have two computers which are connected to each other and one of them is also connected to the Internet. Without special setup on the "host" computer, there will be no Internet connectivity on the "VM", right? This is why "host-only" networking is often thought that it doesn't allow internet connection.

But if you perform this special setup on the "host" computer, the "VM" will be able to reach Internet through it. For that, the "host" computer should become a router, and, optionally, do the NAT to somewhat "hide" the presence of another computer, so it's internet connection will be "tethered".

The special setup I am talking about is the following: you need to enable packet forwarding on the "host" computer, so it will pass packets for the other computer, which happens to be a VM, and, whichever address you assigned to the host's vboxnet0 is configured as the default gateway on the VM, so it knows it should access Internet through the host.

The systems on the eth0 side of the host should know where to find this network you used for the vboxnet0 and VM's eth0, you need to add routes to them towards this network through the host's eth0 address. Or, if you choose NAT path, you need to translate all VM's packets going out of the "WAN" interface to the address of the host's "WAN" address. The "WAN" interface is eth0 in this case:

iptables -t nat -A POSTROUTING -s <vboxnet0-network> -o eth0 -j MASQUERADE

(-j SNAT --to-source <eth0-address> will work too and allow more granular NAT setup, but -j MASQUERADE is easier and more convenient if eth0 receives its address dynamically).

In principle, if you did not set up any other firewall on your host, this is all you need. The ACCEPT rules that you added into filter table are redundant in this case. But if you have additional setup (for instance, if FORWARD policy is set to DROP traffic or if you have a dropping rule in the filter FORWARD table), you've need to add other rules, which permits VM packets to be passed forth and back.


A short summary

  • Abstract unrelated details. Virtual networks are networks in the full right and should be treated as such. Virtual machines don't differ absolutely from physical ones when it comes for the network connectivity. Just presume all computers and all interfaces and connections are real and go further.
  • Host-only networking never meant to "isolate" virtual machine from the Internet. It means just what it states: in this mode the virtual machine has the only connection — its virtual network interface is wired to some the special network interface on the host. If the host happens to be a router, it may well route packets for the VM, enabling it to access the Internet.

Update

even though both host machine (not host VM) and the bridged VM are in the same subnet

This is why it is not able to ping. Bridged and host-only machines are in different Ethernet segments. Think again you have three Ethernet cards in a single computer named "Host". Two of them are bridged together. Cards are connected to other systems:

(Internet) --- [eth0 + BridgedHostIf] --- [vNIC] VM-2
                      Host
                   [HostOnlyIf]
                       |
                     [vNIC]
                      VM-1

So, you have three Layer 2 interfaces here, but only two Layer 3 (IP) interfaces: bridge (which is named eth0 because it is a "kind of" bridge made with VirtualBox quirks) and HostOnlyIf.

Now, when I decomposed your network for you, let me ask your second question in different terms: what happens if you configure the same network on two different network interfaces on the same computer? Will those machines be able to communicate with each other? What needs to be done for them to be able to communicate?

Notice we really have some deep VirtualBox magic here. Where exactly VirtualBox bridging code taps into host networking stack and how it interacts with other networking inside the kernel, including the Netfilter code (which performs NAT) and routing / packet forwarding code. I don't really know. If VirtualBox bridge would be the native Linux bridge, I would be able to explain. But to to understand how VirtualBox "bridges" really work you need to read its sources. Sorry here.

The lesson for this part is to not to try to run VirtualBox in production on servers. It is designed to be used on dev's desktop machines, considering its user-friendly desktop-oriented interface. It is not really designed to be run on servers. If you want to have predictable and configurable virtualization production environment, use something appropriate, like libvirt-based or Proxmox VE.

10
  • Thank you very much for your reply! I'd like to ask you the for the following:
    – jefazo92
    Jul 13, 2022 at 10:07
  • 1. Why do you say that in Layer 1 (Physical), there's only 1 network seen whereas in layer 2 (Data Link) there's two networks seen instead? Layer 1 just refers to how bits are transferred in the media (bits in copper wires/EM waves).
    – jefazo92
    Jul 13, 2022 at 10:07
  • 2. Throughout your answer, you say that NAT is optionally done. Could you expand upon this? NAT is a requirement if the VM is going to be routable so why do you say it's optional?
    – jefazo92
    Jul 13, 2022 at 10:07
  • 3. Can you explain me then why (for another bridged VM), I need to add the vboxnet0 interface to it so that the bridged VM can communicate with the guest VM? Is it because the host-only VM exists in a network (or is it subnet?) of it's own and without adding the interface there's no way for either the host or the bridged VM to communicate with the host-only VM?
    – jefazo92
    Jul 13, 2022 at 10:12
  • 1. vboxnet0 is seen as the Ethernet interface and its correspondent eth0 in the VM is seen as Ethernet, having an unique MAC addresses and so on, Ethernet is Layer 2 protocol. There is no physical objects that correspond to this connection, so this Layer 2 is purely virtual. Jul 13, 2022 at 10:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .