๐ฅ️ How to Access a VirtualBox Ubuntu VM via SSH and Apache from Your Local Network
Setting up a Virtual Machine (VM) using VirtualBox is a powerful way to test servers or applications in an isolated environment. But what if you want to connect to that VM from your host machine or even other devices on your LAN via SSH or a browser?
In this guide, we’ll walk through the steps to:
-
Connect to a VirtualBox VM using SSH
-
Access a web page hosted in Apache2 inside the VM from any device on the same network
Let’s get started.
๐งฑ Basic Setup Overview
We’re using the following configuration:
-
Host machine: Windows with VirtualBox
-
Guest machine (VM): Ubuntu Desktop with Apache2 and SSH installed
-
Network mode: NAT with port forwarding
-
Goal:
-
SSH into the VM
-
Access an Apache2 web server from LAN devices
-
๐ Step 1: Configure Port Forwarding in VirtualBox
Open VirtualBox and follow these steps:
-
Select your Ubuntu VM.
-
Go to Settings → Network → Adapter 1.
-
Ensure the adapter is set to NAT.
-
Click Advanced → Port Forwarding.
-
Add two rules:
| Name | Protocol | Host IP | Host Port | Guest IP | Guest Port |
|---|---|---|---|---|---|
| SSH | TCP | 2222 | 22 | ||
| HTTP | TCP | 8080 | 80 |
-
Leave Host IP and Guest IP blank unless you want to restrict access.
-
This setup allows:
-
SSH access to the VM at
localhost:2222 -
Web access to Apache2 at
localhost:8080
-
๐ Step 2: Set Up SSH in the Ubuntu VM
Start the VM and install the SSH server if it’s not already installed:
Ensure it’s running:
Output should indicate that ssh.service is active (running):
Check if it's listening on the correct port:
You should see:
๐ Step 3: Access the VM via SSH
Now, from your Windows host, open PowerShell or Command Prompt and run:
If you're accessing from another machine on the same LAN, use the host's IP address:
Replace vboxuser with your actual VM username and 192.168.10.30 with your Windows host's IP, which you can find by running:
Look for the IPv4 Address under your active network adapter.
๐ Step 4: Install and Verify Apache2 on the VM
Inside the Ubuntu VM, install Apache2:
Start and enable the service:
Verify that it’s running:
Then, test it locally from the VM:
You should see the HTML of the default Apache welcome page.
๐ Step 5: Allow Apache Through the Firewall
If you’re using UFW (Uncomplicated Firewall) in the VM, allow Apache traffic:
๐ Step 6: Access the Web Page from Any Device
▶ From the host machine:
Open a browser and go to:
▶ From any machine on your LAN:
Use your Windows host’s local IP address, like:
This works because of the port forwarding rule you created.
๐ก Optional: Use Bridged Adapter for Direct LAN Access
If you prefer not to use port forwarding and want the VM to behave like a full device on the network:
-
Shut down the VM.
-
Go to Settings → Network → Adapter 1.
-
Change Attached to from NAT to Bridged Adapter.
-
Start the VM and find its new IP address:
-
Access the web page directly using:
Now, you don’t need to forward ports—other devices on the same network can directly access the VM.
✅ Conclusion
By setting up SSH and Apache2 in a VirtualBox VM and using port forwarding or bridged networking, you can:
-
SSH into your VM from your host or any LAN machine.
-
Serve and access a web application from any browser on your network.
๐งช Test Commands Summary
| Task | Command |
|---|---|
| SSH into VM | ssh vboxuser@localhost -p 2222 |
| Install SSH | sudo apt install openssh-server |
| Start Apache | sudo systemctl start apache2 |
| Allow Apache in firewall | sudo ufw allow 'Apache Full' |
| Access site from browser | http://localhost:8080 or http://<host-ip>:8080 |
Comments
Post a Comment