Skip to content

Instantly share code, notes, and snippets.

@pietryszak
Forked from magnetikonline/README.md
Created October 8, 2022 06:05
Show Gist options
  • Save pietryszak/fd678b740375a6c0eff8df1ebdceb555 to your computer and use it in GitHub Desktop.
Save pietryszak/fd678b740375a6c0eff8df1ebdceb555 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

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