Configuring the VirtualBox Network Adapter
The foundation of this configuration lies in how VirtualBox handles network traffic for your virtual machine. By default, most installations use the "NAT" (Network Address Translation) mode, which isolates the guest OS from the host's local network while allowing outbound internet access. To enable port forwarding, you must interact with the specific settings of this NAT interface.
- Shut down your Ubuntu virtual machine completely if it is currently running. Changes to network adapters often require the VM to be powered off to apply correctly.
- Select the Ubuntu VM in the VirtualBox Manager list and click the Settings button (gear icon).
- In the left-hand menu, select Network.
- Ensure that the Attached to dropdown is set to NAT. If it is already set to NAT, proceed to the next step. Do not change this to Bridged or Host-only unless you have a specific reason to do so, as NAT is the simplest method for local development access.
- Click the Advanced section at the bottom of the panel to expand additional options.
- Locate the Port Forwarding button and click it to open the rule editor.
Creating the Port Forwarding Rule
Once the Port Forwarding dialog box is open, you will see a table listing existing rules. Typically, SSH (port 22) might already have a rule here if you previously configured remote access. You need to add a new rule specifically for HTTP traffic.
- Click the Add button (green plus icon) to create a new rule.
- In the Name field, enter a descriptive label such as
Apache-Web. This helps you identify the rule later if you add more services. - In the Protocol dropdown, select TCP. Web servers rely on TCP connections for reliable data transmission.
- In the Host IP field, leave it blank. Leaving this empty tells VirtualBox to listen on all available network interfaces on your Windows 11 machine, including localhost (127.0.0.1).
- In the Host Port field, enter
22222. This is the port number your Windows 11 browser will use to connect to the VM. - In the Guest IP field, leave it blank. This directs traffic to whatever IP address the Ubuntu guest currently holds within the NAT network.
- In the Guest Port field, enter
80. This is the standard port where Apache listens for incoming web requests.
After entering these details, click OK to save the rule, then click OK again to close the Settings window. Your configuration is now saved permanently for this virtual machine.
Verifying Apache is Running in Ubuntu
Before testing the connection, ensure that the Apache service is actually active inside the Ubuntu guest. Sometimes, after a fresh install or update, the service may not start automatically.
Boot your Ubuntu VM and log in. Open the terminal and run the following command to check the status:
sudo systemctl status apache2
If the service is inactive or failed, start it using:
sudo systemctl start apache2
To ensure it starts automatically on boot, enable it with:
sudo systemctl enable apache2
You can also verify that Apache is listening on port 80 by checking the network sockets:
sudo netstat -tlnp | grep :80
If you see a line indicating LISTEN on port 80, the server is ready to receive forwarded traffic.
Testing the Connection from Windows 11
Now that the infrastructure is in place, test the connectivity from your Windows 11 host. Open any web browser (Chrome, Firefox, Edge, etc.) and navigate to:
http://localhost:22222
You should see the default Apache2 Ubuntu Default Page. This confirms that the traffic is successfully passing through VirtualBox’s NAT engine, translating from your host's port 22222 to the guest's port 80.
If you encounter a "Connection Refused" error, several factors could be at play. First, ensure that no firewall on the Ubuntu guest is blocking port 80. While less common in minimal installs, you can check with:
sudo ufw status
If UFW is active, allow HTTP traffic with:
sudo ufw allow 80/tcp
Additionally, verify that your Windows 11 firewall is not blocking inbound connections on port 22222. Usually, VirtualBox handles its own firewall exceptions, but in rare cases, manual intervention is required. You can temporarily disable the Windows Firewall to test if it is the culprit, though this is not recommended for long-term security.
This setup provides a clean, isolated environment for web development. It prevents conflicts with other local services that might also want to use port 80 on your Windows machine. If you need to host multiple sites, you can add additional rules for different ports, such as mapping host port 22223 to guest port 80 for a second site, or mapping to port 443 for HTTPS.
For those who recently installed their operating system, refer to our guide on Installing Ubuntu 24.04 on VirtualBox 7.0.20 (Windows 11 PC) for initial setup steps. If you experience issues starting the VM after making network changes, check our troubleshooting article on PowerSuspend: VM Failed to Start or Interrupted in Oracle VirtualBox on Windows 11.
Frequently Asked Questions
Why am I getting a "Connection Refused" error?
This usually means the Ubuntu guest is not listening on port 80 or the firewall is blocking the connection. Verify that Apache is running with systemctl status apache2 and ensure UFW is not blocking port 80. Also, confirm that the VirtualBox port forwarding rule is correctly mapped to host port 22222.
Can I access the VM from another computer on my LAN?
No, not with NAT port forwarding alone. NAT only exposes the port to the host machine (localhost). To access the VM from other devices on your local network, you would need to switch the network adapter to "Bridged Adapter" mode and assign a static IP to the Ubuntu guest, or use a different routing strategy.
How do I stop Apache from running?
To stop the service temporarily, run sudo systemctl stop apache2 in the Ubuntu terminal. To prevent it from starting automatically at boot, use sudo systemctl disable apache2. Remember to remove or disable the corresponding port forwarding rule in VirtualBox if you no longer need access.
What if I want to use HTTPS instead of HTTP?
You can add another port forwarding rule in VirtualBox. Map a different host port (e.g., 22223) to guest port 443. Ensure that Apache has an SSL certificate configured and is listening on port 443 inside the Ubuntu guest. Then access it via https://localhost:22223.
Does this affect internet speed in the VM?
Port forwarding itself has negligible impact on performance. However, NAT mode shares the host's network connection. If your host machine is under heavy network load, the VM's internet access might slow down slightly. For high-performance networking needs, consider switching to a Bridged Adapter.