Networking in VirtualBox

Notes

Note that I'm using the older ifconfig command rather than the shiny new and official ip command. There are a couple reasons for this. The first and worst reason is that I haven't really spent the time to learn ip yet. The second and much better reason (that props up the first) is that ip isn't available on several of the Linuxes I'm working with. I'm also using IP v4: I don't know if VirtualBox even supports IP v6 internally, and again, I don't fully understand it.

Default Network

If you boot a virtual machine in VirtualBox and don't do anything in particular about the networking, the machine will end up with one virtual network card. The emulated card appears to be chosen based on the operating system you use, with the default for recognized OSes usually being the "Intel PRO/1000 MT Desktop (82540EM)." If VirtualBox doesn't recognize the OS name you type in, it's more likely to assign the OS an emulated "PCnet-FAST III (Am79C973)." Perhaps this is considered "more compatible," but I assume it's also less desirable or VB wouldn't bother using the other card with recognized OSes. I usually switch to the Intel even with more obscure Linuxes, without any ill effect yet. When you boot the OS, it will (generally - only exception so far is the OpenWRT images) come up with that card as eth0 and an IP address of 10.0.2.15, and routing to the outside world through the host computer network card. An odd point about the networking (that can be really useful if you have to bring up the NIC by hand, and I've had to on several occasions) is that the gateway for your 10.0.2.15 NIC is 10.0.2.2 (not, as I'd normally expect, 10.0.2.1).

# ifconfig eth0 10.0.2.15 netmask 255.255.255.0 up
# route add default gw 10.0.2.2 eth0
# echo "nameserver 8.8.8.8" >> /etc/resolv.conf

Leading "#" indicate that this is a root command prompt. Use sudo if that's your preferred methodology.

The netmask is non-standard for a "10.*" network, but is the one they use. The last line is probably needed if the NIC didn't come up automatically: you should use the settings from the host's /etc/resolv.conf but the line above adds one of Google's publicly available DNSes.

You can have several virtual machines running at once on a host machine: all will come up with the IP 10.0.2.15, but since each is on its own private network, they're invisible to each other and don't interfere with each other.

Internal Network

You can add up to four emulated network cards to a VirtualBox virtual machine. The "Attached to:" option under Settings -> Network -> Adapter <N> has a number of options, and the one I've used the most so far is "Internal Network," which allows you to create a network exclusively between the running virtual machines. By default it's called "intnet," but you can change that. VirtualBox has a way to set up a DHCP server on this network by running a VirtualBox command on the host: I like the machines to have fixed IPs, so I haven't even tried that. Without a DHCP server, IP addresses have to be assigned by hand (this can be put into /etc/rc.local: probably not "best practice," but it works and is more or less what rc.local was intended for):

# ifconfig eth1 192.168.168.2 netmask 255.255.255.0 up

The IP range you use is up to you, but it's strongly recommended that you use one of the standard private network ranges. I recommend staying away from 192.168.0.* and 192.168.1.* as they're commonly used by home routers. This won't cause direct interference as your virtual machine's internal network won't ever come in contact with your home network, but it might cause you to mis-read an address as being on a different network. For similar reasons, if you own a BeagleBone you should stay away from 192.168.7.*, as the BB may use that locally. And if you might ever consider using VirtualBox Host-only networking (if you're reading this you probably will), don't use 192.168.56.* as that's the default for Host-only. Like most things, you can change it, but the defaults are easiest to use.

Note that since this is a small internal network only, I'm not concerned with routing or nameservers. If you want names - which are generally more memorable to humans than numbers - you can set up a /etc/hosts file like this:

# /etc/hosts example file:
192.168.168.2   tc6
192.168.168.3   stretch
192.168.168.4   slitaz

This is a quick and dirty solution: you have to create or copy this to every machine, and changing it means editing the file on every machine. But the alternative is setting up a virtual machine to play DNS, which is a lesson I'm not willing to learn today.

Host-only Networking

2019-12 update: this was accurate when it was written, but Host-only Networking has changed. See the 2019 update.

The default networking on a VB virtual machine (shown as "NAT" or Network Address Translation) can only communicate with the outside world. "Host-only networking" allows the virtual machine to share a network interface with the host machine. Before you can switch it on for a specific virtual machine, you need to switch it on in Virtual Box -> File -> Preferences -> Network -> "Host-only Networks" tab. Here you can add or delete multiple host-only networks, but there are none to start. Adding one gives you a network called "vboxnet0" with an IP address for the host of 192.168.56.1 and a subnet mask of 255.255.255.0. Now you can go to the network setup of any VM and add a NIC attached to this network. Most VMs don't bring this up automatically. With Debian Stretch (and presumably most Debian variants) it can be brought up as root with dhclient eth1 (or whatever adapter number was assigned to this network). After that you can ssh to the host from the guest or vice versa. Or perform any network operations that aren't blocked by your firewall(s): most actions should be fine as this is considered an internal connection. Fixed IPs can be assigned in the same way as above: routing is needed, you can add hostnames to /etc/hosts if you want.

Since virtual machines on the host-only network are visible to each other, this may entirely replace using an internal network.

Other Options

Other options I haven't experimented with include "NAT Network," "Bridged Adapter," and "Generic Driver." I haven't had a need for them yet, but I'll expand this entry if I do.