SYSTEM NOTICE

Auto translation by AI. Be sure, accuracy, nuances and authorial intent may not be fully reflected.

VirtualBox Network Best Practice — NAT + Host-only

When using VirtualBox on FreeBSD, there is actually one major issue. That is, there is a problem with bridged networking.

Bridged networking has the simplicity of plugging the VM's network directly into a LAN hub, and it used to be the standard network configuration for both VMware and VirtualBox. However, in recent versions of VirtualBox, NAT networking has become the standard. This is because it is easy to configure automatically, and it has the advantage that the VM's influence remains within the host and does not affect the physical network.

However, with NAT networking, you cannot SSH from the host to the guest, so if you want to log in to the guest, you might want to choose bridged networking.
However, when using bridged on a FreeBSD host, a strange problem occurs. Even though you can SSH into the guest from another machine on the LAN, you cannot connect from the host itself.
There have been many reports of issues with bridged networking on FreeBSD hosts, but the cause does not seem to have been identified.

Therefore, the configuration using two network paths, NAT + host-only, which I will introduce here, is widely used. This configuration is known as a standard setup even on hosts other than FreeBSD, not just as a workaround for the FreeBSD bridged network problem, but as a safer configuration.
I also recommend this configuration for Windows and Linux hosts.

Also, in full-scale server operations, it is common practice to physically separate the service network from the server management network, and the configuration introduced here is a similar secure setup.

Note that this article does not explain NAT, host-only, or bridged. For those, please see this article.

If you want to know more details, this is recommended.

The official page is here

Overview of the configuration

To explain this configuration simply, it uses NAT as the exit and host-only as the entrance. You use NAT as the exit to the outside world when browsing the web or downloading files via FTP, and you use host-only when logging into the guest via SSH.

VirtualBox settings (performed on the host)

I believe Adapter 1 is set to NAT by default.

Adapter 1

Enable Adapter 2 and select Host-only Adapter.

Adapter 2

The Adapter Type can remain as Intel PRO/1000 MT Desktop (82540EM).

After configuring, don't forget to click [OK]. vboxnet0 is created automatically when you click OK, but it won't be created if you don't click OK, so the network configuration will not be completed. (Unless it has already been created.)
Without vboxnet0, the VM will fail to start with an error.
When you upgrade VirtualBox and the VM doesn't start, this is usually the cause. In that case, just opening this menu and clicking [OK] will create vboxnet0 and it will start. It's good to remember this.

Guest-side settings

A point to note is not to set both to DHCP in the guest settings. For host-only, set a static IP to access via SSH.

  • Adapter 1 — NAT — DHCP (Automatic IP assignment)

  • Adapter 2 — host-only — Static IP

If you set both to DHCP, they will compete to set the gateway, and routing will not function correctly.

Regarding the IP address assigned to host-only, the host-only vboxnet0 network defaults to 192.168.56.x, so assign any number other than 1 (in the range of 2–254) to x. 192.168.56.1 is the host's address. Incidentally, vboxnet1 is 192.168.57.x, vboxnet2 is 192.168.58.x, and so on, allowing you to build multiple networks within the host machine.

The gateway is set automatically on the NAT side, so there is no need to set it on the host-only side.

FreeBSD Guest Configuration

In FreeBSD, the Intel PRO/1000 MT Desktop (82540EM) is identified by the device name 'em'.

  • em0 — NAT

  • em1 — host-only

Network configuration is performed by adding the following lines to /etc/rc.conf.

ifconfig_em0="DHCP"
ifconfig_em1="inet 192.168.56.x"
sshd_enable="YES"

Replace x in 192.168.56.x with any number (2–254). Once editing is complete, you can enable it with

$ doas service netif restart

(If you are using sudo, please replace doas with sudo).

With this, you will be able to access from the host using 'ssh 192.168.56.x'. Registering an appropriate hostname in /etc/hosts makes it easier to access using 'ssh hostname'.

Example of adding to /etc/hosts:

192.168.56.x fbsd

$ ssh fbsd

Ubuntu Guest Configuration

While FreeBSD only requires adding a few lines to rc.conf, manual network configuration in Ubuntu is quite cumbersome.

In Ubuntu, the NIC device name varies depending on the environment. First, please check the current interface name.

$ ip link show

Output example:

1: lo: ...
2: enp0s3: ...
3: enp0s8: ...

Typically, Adapter 1 (NAT) becomes enp0s3, and Adapter 2 (host-only) becomes enp0s8.

Ubuntu manages its network using Netplan. Configuration files are located under /etc/netplan/. The filename varies depending on the environment, but 00-installer-config.yaml or 50-cloud-init.yaml are common.

Check the existing configuration file and edit it to match the following content.

$ sudo nano /etc/netplan/50-cloud-init.yaml

Content:

network:
version: 2
ethernets:
enp0s3:
dhcp4: true
enp0s8:
dhcp4: false
addresses: - 192.168.56.xx/24

enp0s3 is NAT (DHCP), and enp0s8 is host-only (static IP). Replace xx with any number (2-254). Routes configuration is not required for the host-only side. The NAT side handles the gateway.

Indentation is critical in YAML. Be careful not to change the number of spaces.

Once editing is complete, apply the settings.

$ sudo netplan apply

With this, you will be able to access from the host using ssh 192.168.56.x. Similar to FreeBSD, it is convenient to register the hostname in /etc/hosts.

Example of adding to /etc/hosts (on the host side):

192.168.56.x ubuntu

$ ssh ubuntu

Also, if password authentication is required during the initial connection, please check if sshd is enabled on the Ubuntu guest side.

$ sudo systemctl enable --now ssh

Summary

With the settings introduced here, you should be able to use VirtualBox networking without any issues in most cases. Unless you have a specific need otherwise, I recommend using these settings for everything.

The advantage is that access to the VM is limited to the host, and external access is blocked, so there is no need to configure firewalls or similar settings.

While a bridged network has a simple configuration and allows full use of network features, you need to be careful with security settings like firewalls on the guest.

I explained that issues can occur with bridged adapters in VirtualBox on FreeBSD hosts, but for full-scale networking that requires bridged mode, I believe a different virtualization technology called bhyve is more suitable than VirtualBox. In practice, you should be fine if you remember the settings introduced here as a template.

See this article for how to use VirtualBox on FreeBSD

いいなと思ったら応援しよう!