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.
1. Creat a New Network
-
Open network settings in Tools menu.
-
In General Options tab, fill each field as below. Regarding
IPv4 Prefix
field, we can see the IP address of local network byip config
command in Windows, orip addr
command inLinux. Here is the example.- Name: MyNetwork
- IPv4 Prefix:
10.x.x.x/24
or192.168.x..x/24
Then uncheck
Enable DHCP
to 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
2. Set the Network to Each Guest OS Network
- Open settings in guest OS.
- 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.
-
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
Manual
inMethod
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
- Open Settings.
- Go to Network and Internet settings.
- Click current connection.
- Click Edit in the IP settings.
- Select
Manual
and enable theIPv4
switch. - Fill each field. Please see the above (Parrot OS) section to check each value.
- 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