Using Port Forwarding in Virtual Box
There are a variety of reasons to work in a virtual machine. You might want to keep your development environment separate from a personal environment or you might want to use features in a different operating system. When using a virtual machine it may not be immediately obvious how to connect from a browser on the host machine to a server on the virtual machine. This article will explain the setup steps required.
Our arrangement that looks something like this:
Our host OS is Windows running Virtual Box. The guest operating system is Ubuntu running a django server. Before we begin we need to know the IP addresses of the host OS and the guest OS.
Windows Host IP Address
Open a command prompt and run the command “ipconfig”. This will list information about every network adapter on the system. Find an adapter that is assigned an IP address. Not this for later, we will call this the host IP address.
Ubuntu Guest IP Address
Open a command prompt and run the command “ifconfig”. This command will list all of the network settings for the linux guest operating system. Note the IP, we’ll call it the guest IP address.
Port Forwarding in Virtual Box
Virtual Box has the ability to route a port on the host OS to a port on the guest OS. To do this we will need to pick an unused port on the host system. We’ll use 8080. If this port is taken on your machine, pick a different port. On the guest operating system we can configure the Django server to run on any port. We’ll use 8000. We can run the command “python manage.py runserver <guest-ip>:8000”. With these settings in mind we can open the port forwarding settings in Virtual Box. We do this by right clicking the network icon and selecting “Network Settings…”
In the Window that pops up, click on the “Port Forwarding” button. In the dialog that appears, create a new rule. Enter the IP and port information collected earlier. After confirming your settings, Windows firewall may say that Virtual Box was blocked by the firewall. Create an exception to allow appropriate access.
Testing the Settings
Start the Django development server. Open a browser on the host OS. Change the address to <host-ip>:8080. The request will be routed by Virtual Box to the guest OS. If you see your Django project then the process worked!