Skip to content

Instantly share code, notes, and snippets.

@magnetikonline
Last active March 10, 2024 20:31
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save magnetikonline/46a483cc8c9d0451074642f860d0cac1 to your computer and use it in GitHub Desktop.
Save magnetikonline/46a483cc8c9d0451074642f860d0cac1 to your computer and use it in GitHub Desktop.
VirtualBox create host-only interface and attach to VMs.

VirtualBox create host-only interface and attach

A host-only network interface is created virtually in software which appears to the host system.

This interface can be attached to one or more VMs, allowing connectivity between those VMs and communication between the host system and VMs.

Create & attach interface

Note: VirtualBox 6.1.28 has made a change (restriction) to the default allowed network ranges. This behaviour can be overridden by creating a /etc/vbox/networks.conf file on the host filesystem with the following contents to unlock all allowed ranges:

* 0.0.0.0/0 ::/0

Create a new host-only interface:

$ vboxmanage hostonlyif create
# Interface 'vboxnetX' was successfully created

Note down created interface name (e.g. vboxnetX) - referred to as IF_NAME for the following steps.

Set network range and disable DHCP for interface:

$ vboxmanage hostonlyif ipconfig IF_NAME --ip 192.168.2.1 --netmask 255.255.255.0
$ vboxmanage dhcpserver modify --ifname IF_NAME --disable

List final host-only interface(s) configured:

$ vboxmanage list hostonlyifs

Attach network adapter to existing VM(s):

$ vboxmanage list vms
# "VM name" {VM_UUID}
# "VM name" {VM_UUID}
# "VM name" {VM_UUID}

$ vboxmanage modifyvm VM_UUID --nic2 hostonly --hostonlyadapter2 IF_NAME

At this point you will have a new host-only network interface attached to one or more VMs.

Next, configure associated VM guest operating system(s) with the new attached interface.

Configure Ubuntu guests

In these examples configuring a VM guest with an ip address of 192.168.2.6 against a host-only interface of --ip 192.168.2.1 --netmask 255.255.255.0 (as shown in the example above).

Ubuntu 18.04+

$ ip link
$ cat /etc/netplan/interfaces.yaml
# network:
#   ethernets:
#     enp0s3:
#       addresses: []
#       dhcp4: true
#       optional: true
#     enp0s8:
#       addresses: [192.168.2.6/24]
#       dhcp4: false
#   version: 2

$ sudo netplan apply

Ubuntu pre 18.04

$ ip link
$ cat /etc/network/interfaces
# auto INTERFACE_ID
# iface INTERFACE_ID inet static
# 	address 192.168.2.6
# 	netmask 255.255.255.0

Reference

@sesteves01
Copy link

Very nice work (Thank's)

@magnetikonline
Copy link
Author

Not a problems @sesteves01 - yeah it's much easier setting this up via CLI rather than the Virtualbox UI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment