VirtualBox Settings for NAT Network & Port Forwarding
Last modified: 2023-10-18
In VirtuaoBox, we can connect between a Host OS and a Guest OS by setting NAT network and port forwarding.
Open network settings in Tools menu.
In General Options tab, fill each field as below. Regarding
IPv4 Prefixfield, we can see the IP address of local network by
ip configcommand in Windows, or
ip addrcommand inLinux. Here is the example.
- Name: MyNetwork
- IPv4 Prefix:
Enable DHCPto set static ip address.
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
- Open settings in guest OS.
- In Network configuration, select
NAT Networkand set the network name (e.g. MyNetwork) which has been created the previous section.
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.
Below is the Parrot OS example.
Right-click on the network icon at the top-right on screen.
Select a connection name and click on the settings icon at the bottom.
Go to IPv4 Settings tab, select
Methodmenu. 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 22.214.171.124)
- Open Settings.
- Go to Network and Internet settings.
- Click current connection.
- Click Edit in the IP settings.
Manualand enable the
- Fill each field. Please see the above (Parrot OS) section to check each value.
- Click Save.
Finally, we can connect to the guest OS from our host. For example,
# 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
# 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
To connect to host OS from the guest, we can achieve by follow.
# 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