Set Up a VirtualBox Host-Only Network for a Debian VM
Goal
I wanted to run a Debian virtual machine locally and connect to it directly from the host. A VirtualBox host-only network provides a private network between the host and its virtual machines.
Add a host-only adapter
First, make sure a host-only network exists in VirtualBox. In the VM’s Settings → Network, enable an adapter and select Host-Only Adapter as its attachment type. Choose the host-only network you want to use.
The VirtualBox DHCP server may be enabled for that network. You can use DHCP instead of configuring a static guest address. If you want a fixed address, check the network’s subnet and DHCP range first, then choose an unused address outside that range or reserve it in DHCP. The example below uses 192.168.56.0/24; use the subnet configured on your host-only network.
Host-only networking connects the host and guest but does not provide Internet access by itself. If the VM also needs Internet access, add a second network adapter and attach it to NAT.
Assign a static IP to the Debian guest
Start the VM and run ip address to find the name of the new network interface. In my VM it was enp0s8; your interface may have a different name.
If Debian uses ifupdown to manage this interface, edit /etc/network/interfaces as root and add a stanza like this:
auto enp0s8
iface enp0s8 inet static
address 192.168.56.2/24
Replace enp0s8 with your interface name and choose an unused address in the host-only subnet. This file applies to interfaces managed by ifupdown; if your installation uses NetworkManager, configure the adapter’s IPv4 settings there instead. See the Debian interfaces(5) manual.
Save the change and restart the guest or bring the interface up with your network manager. Confirm the address with ip address. Then test connectivity in both directions using the host-only IP addresses. A host firewall may block ping, so test a service on the guest if ICMP does not get through.
For the current adapter and DHCP options, see the Oracle VirtualBox User Manual, section 9.9, “Host-Only Networking.”