How to setup a Virtual LAN on your machine using Oracle VirtualBox

After running many errands on google, wikipedia and suchthings in search of a good  Virtualization solution for my Windows-7 HP machine, I’ve discovered a sleek new solution for many virtualization tasks like trying out new Linux distros, testing my C# applications on a fresh Operating System, creating a virtual “Network of computers” on my machine, etc. This solution came in the form of Oracle VirtualBox. I had short-listed three virtualization candidates for this – VirtualBox, VMWare Player and Microsoft VirtualPC in that order. However, VirtualBox paid off so well that I had no need to look anywhere else. If you haven’t set up VirtualBox already, here is how you do it:

Step1: Download VirtualBox and a guest OS image

Download and install the latest package for your host OS from https://www.virtualbox.org/wiki/Downloads. In order to achieve virtualization, you will obviously need a guest OS either in the form of an installable CD/DVD or an .ISO image. Luckily, VirtualBox doesn’t insist on an actual CD/DVD disk. You may choose to install either from a disk, or directly from the .ISO image of the guest OS itself! Here are some places to get the latest versions of some of the popular OS images:

Debian: http://www.debian.org/mirror/list

Ubuntu: http://www.ubuntu.com/download

openSUSE: http://software.opensuse.org/

Fedora: http://fedoraproject.org/en/get-fedora-options

Linux Mint: http://www.linuxmint.com/download.php

Step2: Configure

VirtualBox provides many out of the box features including a versatile GUI to make the job of creating a virtual machine as easy as possible. Once you have installed VirtualBox, the next step is to create a virtual-machine for your new OS. This is like a shell that provides all basic features such as BIOS settings to control how the new OS starts, its hardware and graphics profile, and also its network setup. To create a new VM, start the VirtualBox program and click the new icon on the top:

VirtualBox will now ask you several questions such as the type of operating-system and how much disk-space to allocate to your virtual hard-drive.

How to allocate space to your virtual hard-drive?

The default setting of a dynamically growing disk suffices most purposes. The advantage of this is that you can play with several VMs with each getting a good hard-disk allocation like 100 or 200 Gigabytes. Mind you, this space is not actually allocated on your host-OS in case of dynamic memory, but the space will grow as and when used.

However, if you have plenty of space on your host-drive with memory running in several gigs or terabytes, or you want to allocate a fixed size on your host in advance, then you should go for fixed-size drive which performs better compared to dynamically allocated one.

The entire VM creation process is quite intuitive with a wizard-like interface. If you don’t understand anything, just go with the default settings. You can change later anything you want. Once you create your new VM, you can start it using the start icon on top, then proceed with the installation of your particular OS.

VirtualBox provides several ways to control and interact with the new machine. For instance, by default, the right control key (or right command-key in Mac Osx) is used to leave control to the host-OS. You can toggle between full-screen and normal mode by pressing Right-Ctrl+F key.

Once your installation is complete, you may have to install some guest addition packages on your guest OS. These are software that enable VirtualBox to better the user experience for that OS by providing features like 3D-graphics, USB support, native resolution/modes, etc.

Step 3: Establish a network between the guest and host machines

Here comes an important reason why most people (myself included) want to implement virtualization in the first place. To create and experiment with all kinds of networks without the risk (or taking the trouble) of creating an actual one. And here is where VirtualBox excels by providing several options for networking out of the box. VirtualBox installs an additional NIC (Network Interface Card) on your host computer to identify itself while communicating with the guest. By default the host gets an IP address of 192.168.56.1. You can change the network modes, IP and other network settings by right-clicking your virtual machine on the left and clicking Settings. These are the networking modes that work with VirtualBox guest computers:

  1. NAT (Default)
  2. Host-only Network (Most secure)
  3. Bridged Network (Least secure)
  4. Internal-Network (Betweeen guests only)
  5. Not Attached (No connectivity, guest isolated)

NAT: By default, the networking mode for your virtual machine is NAT (Network Address Translation) mode. This works something like this:

Assume that on your laptop, you are accessing internet through a router (typical in most home or wifi network scenarios). NAT works in the same way. This is a very secure way to access a network. In this case, your Guest computer sits behind the VirtualBox router. It can even access internet from through the host computer’s modem or LAN. However it is completely hidden from the rest of your network and the host computer thanks to the VirtualBox router-cum-firewall. Any traffic coming

from the guest computer will be seen as originating from your host computer.

Host-only Network: This is the second mode allowed by VirtualBox. Obviously, this is the most secure method among the three as there the “virtual-cable” is just laid between the guest and host computers, no other computer on the network will be visible to the guest. Think of this as a peer-to-peer computers between any two computers on a LAN:

In this case, the guest is actually (or rather “virtually”) cut off from the rest of the world, except the host computer. In this case, if you want to provide internet connectivity to the guest, you will have to turn your host into a proxy server. Then the guest can indirectly access the internet through your guest’s proxy server. This scenario is very useful when say, you are developing a proxy server and you want to test it. Other scenario is when you don’t need network in the guest at all. Of course, VirtualBox also provides an additional “Not Attached” mode if you want to totally isolate your guest from even the host computer too.

Bridged-Network: This is the preferred mode to establish full-fledged networking on your guest. In this case, your guest actually becomes a peer on your network. All other computers can “see” the guest as a different peer from the host. In other words, it is as good as setting up another peer with network and cabling on your LAN. VirtualBox achieves this by “sniffing” the incoming network packets on all your NICs directed at the guest, and in-turn “injecting” packets that are send by the guest to other peers:

Be warned however, that this mode is the least secure way to connect the guest, and use it only when you know what you are doing, and you really need to. Use the NAT mode as far as possible. I’ve myself never yet found a use for this mode. Especially, when you are connecting to the Internet without a proxy, remember that you are connecting directly without a router in between. In this case, you need a firewall which is quite strong. The Windows built-in firewall is preferred for windows guests which is on by default. On RHEL-like system such as Fedora or CentOS, there is a built-in firewall that you have to initially configure. Similarly on debian based systems like Ubuntu and MintOS, there is the ufw package which stands for “Uncomplicated firewall”.

Internal-Network: If, for some reason, you want all or some guests to internally communicate, while still keeping them isolated from host and the rest of your network, VirtualBox provides this mode out of the box:

Just set this mode for those guests you want to keep internally connected. By default all guests are assigned a dynamic IP address in the range of 10.0.2.0 (in all modes) by the built-in DNS servers in the VirtualBox. You will not want to change this to a static IP-address unless you have a specific reason to do so. Also, remember to keep all guests that you want internally connected in the same domain (for linux or Win-Server hosts), or the same workgroup (Windows hosts).

Not Attached: Finally, there is the mode for complete isolation, wherein the “virtual-cable” is as good as non-existent between the guest and host. In this mode, there is zero connectivity between your guest and host:

However, do remember that the guest can still access your CD-drive or an ISO depending on your setup. There is also a feature for “Shared Folders” which you can set in the virtual machine’s “Shared Folders” settings. When you map a folder on your guest, you have the option to “auto-mount” it when your gust starts. For example, you have shared your host machine’s C:\library folder as and mapped as library folder in guest. When your linux guest starts, it will be automatically mounted as “/media/sf_library” by default.

Hopefully, the above instructions should suffice. However, with the complexity involved in Virtualization, there is a big chance that anything has gone wrong or some components did not work as expected. In that case these are the holy sites to visit:

  1. VirtualBox User Manual: http://www.virtualbox.org/manual/UserManual.html
  2. VirtualBox Forums: https://www.virtualbox.org/wiki/Community
  3. SuperUser: http://www.superuser.com

Published by

Prahlad Yeri

Educate and solve problems is my mantra. I work on full stack web development and automation projects.

8 thoughts on “How to setup a Virtual LAN on your machine using Oracle VirtualBox”

  1. I ‘ve been really stuck up trying to established a connection between the Host OS & VirtualBox, Really Appreciate the help!! I ‘ve tried the solution that you suggested here but to no avail.

    I ‘ve installed Virtualbox 4.2.10 on RHEL 6 and installed guest OS RHEL 6 on it. I ‘ve configured NAT & Host-only adapter in order to established the connection with the Virtualbox from the Host OS. and The Host server is connected to Internet through a modem not to any Office network as this is a Personal server.

    The Host OS IP is 192.168.56.8 & its DNS is 192.168.56.1 which i ‘ve assigned through the system-config-network utility on RHEL 6. and followed the instructions for the Guest VM IP given here. i.e guest configured NAT & Host-only adapter for the Virtualbox given here. and also checked with the ifconfig command that the IPs assigned correctly. The Guest Virtualbox IP: 192.168.56.100 & DNS 192.168.56.1 which i ‘ve assigned through the system-config-network utility on RHEL 6.

    But still I can not ping the Host OS from the Guest OS i,e from the VirtualBox when i ping the host OS it says that the Network unreachable.

    The entries in the /etc/hosts & /etc/resolv.conf for the Host IPs and the DNS which is same as that of HOST OS is there. But it failed to establised connectivity from the Guest OS to Host OS. although i can ping the guest OS from the Host OS.

    Could you please suggest what i need to do inorder to establish connectivity from Guest OS to Host OS.

    Thanks Priya

    1. @Priya – First of all, thanks for the comment and appreciation. Secondly, you cannot have have both “NAT” and “Host-Only” type of connectivity between your host and the guest OSes. You can setup either one of these scenarios as I’ve mentioned in my article. Better make sure what kind of network you have established, by checking the settings applet for your VM in VirtualBox. Even without doing that, the right-hand panel itself displays your type of network with the selected guest VM. In this screenshot for instance, it is NAT and not Host-Only network: http://upload.wikimedia.org/wikipedia/commons/7/7c/VirtualBox_screenshot.png.

      As regards your connectivity, you say you can ping the guest OS from the host OS. This means connectivity is certainly established. However, if you cannot ping the host from the guest, it could mean only one of these things:

      1) You have got the IP of host wrong.
      2) There is a firewall/router on the host side that does not allow the guest to connect.

      Since you are so confident about the first one and have rechecked the /etc/hosts files, the second scenario only seems plausible. This indicates that you must be using the NAT type of connectivity where the virtual-box usually blocks the ICMP protocol (used to complete a ping request) through its NAT router. But thats not a reason to worry. Ping is not the only way to “locate” your host from the guest. Here are some other ways:

      1) If you have a HTTP web-server running on the host, run “telnet 192.168.56.8 80” from the command line. This checks for an open service on port 80 (HTTP). If the telnet request succeeds, you have the connectivity.

      2) If you have internet running on the host, then you can install a proxy server such as squid on the host and try to connect to internet from the guest machine.

      3) Similar telnet requests can be sent to ports 443, 21 and 25 for HTTPS, FTP & SMTP services respectively (if you have any of these services running).

  2. Oh, thank you Prahlad, after reading your article I’ve finally understood something about virtual networking :)
    I tried to use Virtualbox to install drivers for 3G Modem on guest Windows XP and share this connection to host Ubuntu 13.04 (there are no drivers for linux for this modem). I selected a bridged adapter as my network adapter in VM settings and then I selected eth0, ran VM and nothing happened on my host Ubuntu because my Wired connection 1 is always grey and I don’t use eth0 physically and bridge just doesn’t work.
    So, I should creat a host-only connection to make this networking and share Internet from guest to host, shouldn’t I?

    1. @Alex – If you are able to get internet on your guest machine, then yes, you can create a host-only connection to share it with your host. You also need to make some changes on your windows guest:

      1) Either install a proxy server there, and access internet through proxy by providing the port number on your ubuntu machine. OR
      2) On the windows machine, go to your adapter properties -> sharing tab, and allow internet sharing from there. This way you will be able to get direct internet connection on your ubuntu machine.

      1. Thank you Prahlad, I’ve set up the 2nd variant and on Windows(G)->Windows(H) system it has just worked like a charm.

        But Windows(G)->Ubuntu(H) doesn’t work the same way, because I’ve not found Gateway and Primary DNS options for vboxnet0 on Ubuntu. It seems like it’s designed to be a server only, but on Windows(H) I can change these additional options for Virtual Host Adapter to reverse the whole system.

        So I’ll try a proxy server on Windows(G) and will leave vboxnet0 settings by default.

  3. I’m very pleased to uncover this website. I need to to thank you for ones time due
    to this wonderful read!! I definitely really liked every part of it and I have you book marked to check out new stuff
    on your website.

Leave a comment