VirtualBox Settings for NAT Network & Port Forwarding

Last modified: 2023-10-18

Virtual Machine

In VirtuaoBox, we can connect between a Host OS and a Guest OS by setting NAT network and port forwarding.

1. Creat a New Network

  1. Open network settings in Tools menu.

  2. In General Options tab, fill each field as below. Regarding IPv4 Prefix field, we can see the IP address of local network by ip config command in Windows, or ip addr command inLinux. Here is the example.

    • Name: MyNetwork
    • IPv4 Prefix: 10.x.x.x/24 or 192.168.x..x/24

    Then uncheck Enable DHCP to set static ip address.

  3. In Port Forwarding tab, set ip/port for each guest OS. Here is the example.

    • Name: Guest 1
    • Protocol: TCP
    • Host IP: (empty)
    • Host Port: 9999 (note that this value must not duplicate the other Host Port in other guest OS line)
    • Guest IP: 192.168.11.100
    • Guest Port: 1234

2. Set the Network to Each Guest OS Network

  1. Open settings in guest OS.
  2. In Network configuration, select NAT Network and set the network name (e.g. MyNetwork) which has been created the previous section.

3. Set Static IP Address in Guest OS

Now start the guest OS in VirtualBox.
Since we disabled DHCP, we need to set static IP address attached for the guest as it was set in Port Forwarding settings of Network configurations.

Linux

Below is the Parrot OS example.

  1. Right-click on the network icon at the top-right on screen.

  2. Select a connection name and click on the settings icon at the bottom.

  3. Go to IPv4 Settings tab, select Manual in Method menu. Then fill each field as below. These values should be matched as the previous network settings for the guest OS.

    • Address: 192.168.11.100
    • Netmask: 24
    • Gateway: 192.168.11.1
    • DNS servers: 192.168.11.1 (or public DNS server ip like 8.8.8.8)

Windows

  1. Open Settings.
  2. Go to Network and Internet settings.
  3. Click current connection.
  4. Click Edit in the IP settings.
  5. Select Manual and enable the IPv4 switch.
  6. Fill each field. Please see the above (Parrot OS) section to check each value.
  7. Click Save.

4. Connect From Host to Guest

Finally, we can connect to the guest OS from our host. For example,

HTTP

# In guest OS, start web server on port 1234.
python3 -m http.server 1234

# In host OS, we can access to the web server on local port 9999.
curl http://localhost:9999

SSH

# In guest OS, start SSH server on port 1234.
sudo vim /etc/ssh/sshd_config # edit port number to 1234 from the default port 22.
sudo systemctl start ssh

# In host OS, we can connect SSH on local port 9999
ssh parrot@localhost -p 9999

5. Connect From Guest to Host

To connect to host OS from the guest, we can achieve by follow.

HTTP

# In host OS, start web server on arbitrary port
python3 -m http.server 8000

# In guest OS, we can access to the web server by specifying the second ip address (x.x.x.2) in local network.
curl http://192.168.11.2:8000