VirtualBox Networking

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
340
Reaction score
367
Credits
11,754
Most of you may wonder, “Has Jarret lost his mind? What is he talking about, VirtualBox Networking? Everyone knows how to set up networking under VirtualBox!”. I’d say most days, yes, I have lost my mind. But anyway, about VirtualBox Networking…

When you create a Virtual Machine inside the VirtualBox program, you have a choice of six different options from which to choose:

  1. Not Attached
  2. NAT
  3. NAT Networking
  4. Bridged
  5. Internal Network
  6. Host-Only

NOTE: There is a seventh and eighth option in VirtualBox 6.1.34r150636, there is an experimental ‘Cloud Network’ that I will not cover. Since it is experimental, it could change before it is out of an experimental phase. The other option is ‘Generic Driver’, which leans more towards experienced users to set up a UDP Tunnel between two Virtual Machines.

Let’s look into these so you have a more informed choice about which one to use for your intended purpose. I must admit, I have blindly followed using NAT Network nearly every time, but this may not suit certain virtual machines that you create.

Let’s look over a quick overview of the various aspects of what we’ll be discussing.

Aspect Overview

The physical machine you are using is the Host system. Your Host system most likely uses a Wi-Fi or direct connection to a connection to the Internet. The adapter being used is a physical adapter.

On your Host system, you install VirtualBox. Inside VirtualBox, you have one or more Virtual Machines. The Virtual Machines run an Operating System (OS) on virtual hardware with virtual network adapters. All the hardware ‘appears’ to run like a physical system. We will assign the Network Adapter an IP Address, have a MAC Address, etc.

The Internet is accessible by some of the Network Adapters, but is being accessed through a Virtual Network Adapter that can work ‘through’ the physical network adapter on your host system.

We made all the network settings under the machine’s Settings when you select ‘Network’ in the left pane. You’ll see there are four adapters which can all use different options. You can enable zero to four of the adapters for each machine.

For any examples I make, I am using VirtualBox on my system. The local network on the Host adapter has an IP Address of 192.168.1.203 with a subnet mask of 255.255.255.0. The Gateway is 192.168.1.1.

On the host system is a virtual adapter that has an IP Address of 192.168.122.1 with a subnet mask of 255.255.255.0. There is a second virtual network with an IP Address of 10.24.4.224 with a subnet mask of 255.255.255.0.

I created a machine using Ubuntu 22.04 and updated it. I then cloned the machine to a second one, but linking it so I didn’t use twice as much drive space. For each section, I set both machines to the specified network adapter option.

Not Attached

The option of ‘Not Attached’ allows you to have a virtual network adapter, but no connection.

This is the same as having a machine with a network card, but no attached network cable.

The reason for this is you may have set up a virtual machine with a specific connection type, but then decided you didn’t want the virtual machine to connect to a network of any type (for whatever reason you may have). So, you can simply go in and set the adapter as ‘Not Attached’.

Otherwise, you can install an Operating System on a Virtual Machine and want it to install the network drivers, so you set the adapter as enabled, but ‘Not Attached’.

NAT

The NAT setting is the default setting for any enabled Network Adapters. Using NAT is basically set up a Virtual Machine as a client on the Host system and VirtualBox is being used as a ‘Proxy’ or ‘Gateway’. The Virtual Machine sends requests to the VirtualBox Gateway for accessing the Internet.

Each Virtual Machine you create using NAT will have the same IP Address. On my system it is 10.0.2.29. Since the machines will have the same IP Address, they cannot access each other. The NAT systems can only connect to the VirtualBox NAT Gateway, which in my case has an IP Address of 10.0.2.2. We can find the Gateway Address by using the command ‘ip r’ from a terminal on the machine.

We can see the network in Figure 1.

Figure 1.jpg

FIGURE 1

From the diagram, you can see that each system can access the Internet, but no other systems. The virtual machines cannot even access the Host.

If you wish for access to the Virtual Machine from an external system, then you need to configure Port Forwarding.

Port Forwarding

Both NAT and NAT Networking allow for Port Forwarding in the Adapter settings of VirtualBox. When you choose either NAT or NAT Networking from the ‘Attached to’ listing, there is an option below it for ‘Advanced’. Once you select ‘Advanced’, a section should appear and at the bottom is a button to click to set up Port Forwarding.

Once you have clicked the button, see a new window with the Port Forwarding Rules. In the top right of the window is an icon with a plus sign to add a rule. Once you click it, you can create a new rule. Remember, this is for incoming connections that will be received by the Host system and forwarded to the Virtual Machine that you are configuring.

For an example, I set up SSH on a Virtual Machine and enabled its firewall to open Port 22. On the Host system, I shouldn’t forward Port 22 in case I use SSH on it. So, I will forward Port 2222.

I set a new rule and give it the name SSH. Any name you use is fine as long as you know what it is for. The Protocol can remain as TCP. The ‘Host IP’ is my Host address left blank and the Host Port is 2222. We left the Guest IP blank and a Port of 22. Any connections coming into my Host system on Port 2222 are forwarded to the Virtual Machine on Port 22.

NOTE: Leave the IP Address boxes blank. VirtualBox will determine these as needed since the addresses can change because of a DHCP Server.

From a system on the Host’s network, use the command ‘ssh -p 2222 [email protected]’. It should prompt you to continue as well as for the password for ssh.

Change the username and the IP Address of the Host as you need.

NAT Network

We must initially set the NAT Network up in VirtualBox after we installed it. Once VirtualBox starts, select File then Preferences. Once the Preferences window opens, select Network in the left pane. On the right side, click on the icon with the plus sign to create a ‘NATNetwork’.

When configuring the ‘NATNetwork’, you can change the name. If only one network is created, then it will always be the default when you select a ‘NATNetwork’. If you have over one configured, then you can specify which to use.

When configuring a ‘NATNetwork’, you can specify the network name, network and number of bits for the subnet, and whether DHCP or IPv6 is enabled on the network. An example of the IP Network would be 10.0.0.0/8. The default network is 10.0.2.0/24.

With NAT Networking, we set the default Gateway to 10.0.2.1, while it sets the default DHCP Server to 10.0.2.3.

Since it set a DHCP Server up, each Virtual Machine has its own IP Address on the configured network. Each Virtual Machine can ping the other machines on the same network. The machines can also ping the Host and the access the Internet.

The NAT Network option, like NAT, allows you to set up Port Forwarding so it can access services from outside the virtual network. Without Port Forwarding set up, network systems cannot access services on a Virtual Machine.

Bridged

With the ‘Bridged’ network, the Virtual Machines are treated as if they were an actual machine on the local network. The virtual network is connected to a virtual bridge which is also connected to the Virtual Adapter on your Host. In this manner, the Virtual Machine receives an IP Address from the DHCP Server on your local network.

Without Port Forwarding, systems on the local network can access services on the Virtual Machine as if it were a physical machine on your network.

The ‘Bridged’ option allows full access and may be the best options in a lot of cases. As long as you want full access to and from the Virtual Machine, then this option works. If you want to limit access to the machine, then do not use ‘Bridged’.

Internal Network

The Internal Network is a network that exists only in the Virtual environment. Initially, each Virtual Machine is set up for DHCP networking. You can create a machine to act as a DHCP Server, or manually set the IP Address on the systems.

Once the systems are set with an IP Address on the same logical network, they can ping one another. The Virtual Machines cannot ping or access the Host, as well as the Host cannot access the Virtual Machines.

If the Virtual Machines are on separate logical networks, then they cannot see one another.

Of course, all of this means that the Internet is not accessible either.

If you want to set up a group of Virtual Machines and set up a network to test settings, this is perfect. Two systems can be on two different logical networks, while a third acts as a bridge between the two. You can enable the bridged system to allow access to the Internet.

Host-Only

When you use the Host-Only option, you can choose a specific Adapter. You create the Adapters under the initial VirtualBox screen. Go under Files, Host Network Manager to set up the Adapters or make changes.

You can choose the Create icon to make a new Adapter. Set to ‘Configure Adapter Manually’. Here, you can set the IPv4 Address and the Subnet Mask. For example, I can set the IPv4 Address as ‘10.0.0.1’ and the Subnet Mask as 255.0.0.0. On the DHCP Server tab, I would set the Server Address as a different IP than on the other tab. Here, I could use ‘10.0.0.2’ with the same Subnet Mask ‘255.0.0.0’. The Lower Address given by the DHCP Server could be the first number given to a Virtual Machine, such as ‘10.0.0.10’. The Upper Address would be the highest number, or last address, such as ‘10.0.0.200’. Make sure you check to ‘Enable Server’ on the DHCP tab to allow the DHCP Server to operate, if you need it.

The Adapter name must be selected under the option when you select ‘Host-Only’. Be sure to select the proper Adapter if you create multiple adapters.

NOTE: Each of the Adapters you create will create a Virtual Network Adapter on your Host system.

Once the Virtual Machine is running, then they are assigned an address from the DHCP Server you configured in the Host Network Manager.

The Host cannot Ping the Virtual Machines, and the machines cannot ping the Host (not even the Virtual Adapter that is running on the Host).

The Virtual Machines cannot access the Internet.

‘Host-Only’ is the same as the ‘Internal Network’, but has a built-in DHCP Server if you choose.

Conclusion

I hope this helps open more options on VirtualBox for you.

It is best to have a network option that works better for your personal configuration and network needs.
 


I think it's worth mentioning the ability to forward a usb interface directly to a VM for completely organic networking. Usb dongles cost pennies and it saves so much config. And if you want to run other vms or containers on a host vm, then this method lets you have most control from within the VM.
 

Members online


Latest posts

Top