How to Access a VirtualBox Ubuntu VM via SSH and Apache from Your Local Network

 

๐Ÿ–ฅ️ 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:

  1. Select your Ubuntu VM.

  2. Go to Settings → Network → Adapter 1.

  3. Ensure the adapter is set to NAT.

  4. Click Advanced → Port Forwarding.

  5. Add two rules:

NameProtocolHost IPHost PortGuest IPGuest Port
SSHTCP222222
HTTPTCP808080
  • 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:

bash
sudo apt update sudo apt install openssh-server

Ensure it’s running:

bash
sudo systemctl status ssh

Output should indicate that ssh.service is active (running):

arduino
● ssh.service - OpenBSD Secure Shell server Active: active (running)

Check if it's listening on the correct port:

bash
sudo ss -tuln | grep :22

You should see:

markdown
tcp LISTEN 0 4096 *:22 *:*

๐ŸŒ Step 3: Access the VM via SSH

Now, from your Windows host, open PowerShell or Command Prompt and run:

powershell
ssh vboxuser@localhost -p 2222

If you're accessing from another machine on the same LAN, use the host's IP address:

bash
ssh vboxuser@192.168.10.30 -p 2222

Replace vboxuser with your actual VM username and 192.168.10.30 with your Windows host's IP, which you can find by running:

powershell
ipconfig

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:

bash
sudo apt install apache2

Start and enable the service:

bash
sudo systemctl start apache2 sudo systemctl enable apache2

Verify that it’s running:

bash
sudo systemctl status apache2

Then, test it locally from the VM:

bash
curl http://localhost

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:

bash
sudo ufw allow 'Apache Full' sudo ufw enable

๐ŸŒ Step 6: Access the Web Page from Any Device

▶ From the host machine:

Open a browser and go to:

arduino
http://localhost:8080

▶ From any machine on your LAN:

Use your Windows host’s local IP address, like:

cpp
http://192.168.10.30:8080

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:

  1. Shut down the VM.

  2. Go to Settings → Network → Adapter 1.

  3. Change Attached to from NAT to Bridged Adapter.

  4. Start the VM and find its new IP address:

    bash
    ip a
  5. Access the web page directly using:

    cpp
    http://<VM-IP>

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

TaskCommand
SSH into VMssh vboxuser@localhost -p 2222
Install SSHsudo apt install openssh-server
Start Apachesudo systemctl start apache2
Allow Apache in firewallsudo ufw allow 'Apache Full'
Access site from browserhttp://localhost:8080 or http://<host-ip>:8080

Comments