We'll use Ubuntu Server our guest operating system for its relative ease of use and because there is already a large amount of support information that can be found online for its setup and maintenance in VirtualBox. I'll be using the 64-bit version of Ubuntu Server 14.04.3. Make sure you choose the version with the correct architecture for your system: 32 vs. 64 bit. Download a copy of Ubuntu Server from the link.
Before beginning the installation process of any virtual machine, it's always a good idea to check the hash value of the file you've downloaded to ensure its integrity. It is quite annoying to have to begin the process again because your download was corrupted in some way. You can find the hash values of various Ubuntu downloads here. (For a quick rundown on the process, see our previous post on why and how to check a file's hash value. See also our tutorial on how to build your own file hash checker in Python.)
Virtual Machine Configuration in VirtualBoxLet's jump in. After downloading and installing VirtualBox, if you haven't already, fire it up and click "New" to create a new virtual machine. Give your machine a name (I'm calling mine "ubuMongoLab," in preparation for the database project), select Linux as your type and select the appropriate version information from the dropdown menu.
After clicking "Next," we are prompted to select the amount of desired memory for the new machine. I'm going to leave the allocated memory at the recommended 512MB. You can tweak this for your own setup. In my experience, Ubuntu Server virtual machines tend to experience lag if you reduce the available memory below 386MB.
Click "Next." Select "Create a virtual hard drive now" and click "Create."
On the next page, we configure the storage on the physical drive. Select "Dynamically allocated." This means that disk space for the virtual machine will only be taken up on your host machine's physical hard drive only after the virtual machine's disk has actually been written to.
On the next screen, which determines "File location and size," I'm going to leave the default location and file name. The default size for the virtual machine's hard drive is 8GB. However, since I am using this VM for a database project that requires importing a fair amount of data, I'm going to allocate significantly more than that. Since I plan on using this virtual machine for other projects as well, I'm going to give it a good chunk of space: 50GB.
Click "Create." The dialog should disappear and the new virtual machine should appear in your VirtualBox Manager. In the image below, I've created a group/folder for the new virtual machine called MongoTutorial.
Before starting up the new machine, let's tweak a few settings. Single-click the VM and then click "Settings." Under "General," I add in a short description under the "Description" tab:
Under "System," I unclick "Floppy" on the "Motherboard" tab.
I'm also going to allocate a second processor to the VM under the "System->Processor" tab:
For now, we can leave everything else as-is. I'm going to leave the defaults for the display, since we're not going to be using any graphical interfaces. And we'll leave the single default network adapter attached as NAT until the machine is up and running and fully updated. Click "OK" to save your settings. Make sure your host machine is connected to the internet before beginning installation of the Ubuntu Server OS. This is not strictly necessary, but it can speed the process up a bit.
Installing Ubuntu Server on the New Virtual MachineNow single-click the machine in your VirtualBox menu, then click "Start" to fire up the new machine. You'll now be prompted to select your startup disk. Use the file icon next to the drop down menu to navigate to the Ubuntu Server .iso file you downloaded earlier and select it. This is the virtual equivalent to inserting an actual disk with the Ubuntu Server .iso file on it into a physical machine on which you want to install the system. Then click "Start".
Select your language and click enter (note: your mouse will not work here, the installation process is entirely keyboard-based):
Select "Install Ubuntu Server" using the up/down arrow keys, if it is not already selected, and hit enter.
Select the language for your new VM operating system and hit enter:
Then select appropriate values for the next two screens: your country and keyboard layout. Next we're going to enter a name for the new system. I'm going to choose the same name as I gave the VM itself, though they do not have to be the same. Enter the name, press the down arrow to select "Continue" then hit enter.
Then provide a name for the main administrative user of the new system:
Add a username for the account itself (I'm using the same name for both):
Choose whether to encrypt the home directory:
Select your time zone and let it do its thing. Next, for partitioning, I'm going to choose "Guided - use entire disk." This will install the operating system to the entirety of the disk we have allocated inside VirtualBox for the new virtual machine.
Select the virtual disk you have already allocated for the new machine and hit enter.
Let it do its thing while it installs the new system (this could take a few minutes):
Follow the instructions for the proxy setup if you have a proxy, select continue and hit enter:
I'm going to update this system manually, so I select "No automatic updates" on the update config screen:
I'm also going to use manual package selection:
Select "Yes" to install the GRUB boot loader and hit enter:
Select boot into the new system (I've found you don't have to "remove the virtual disk"):
It should shutdown and boot right into the new system:
Supply the credentials you entered for the main user during the installation process and you will be logged in to your new virtual machine!
Updating and Upgrading the ServerWe are now going to update and upgrade the operating system and install open-ssh server so we can access it from other machines on the LAN, whether the host or otherwise. Which means we also don't have to worry about installing VirtualBox guest additions, since you can just access the VM over ssh from your preferred host. Enter the following command at the command line, and enter the password credentials when propted to do so: sudo apt-get update. This will update the system's repositories. Now we'll update the existing software on the system. On the command line, now enter: sudo apt-get dist-upgrade. Hit "Y" if you are prompted to do so.
Now I'm going to install a couple programs I'll need for the current project: open-ssh server and mongodb. Open-ssh server allows you remote login access to the VM, so we can access it from a preferred host. I'm also installing mongodb as I created this VM for a database project. To install a program, on the command line, enter: sudo apt-get install <PROGRAM NAME>. For ssh: sudo apt-get install openssh-server. And for MongoDB: sudo apt-get install mongodb. Hit "Y" if you are asked to proceed:
Now let's shut down the machine so we can take a VirtualBox snapshot of our fresh install with updates and our necessary software. Enter: sudo shutdown -h now.
We'll now take a snapshot of the virtual machine inside VirtualBox, that way if we run into any serious problems from this point on (ex. it gets corrupted somehow) we can always revert to the present state of the machine and system. Click "Snapshots" inside the VirtualBox Manger.
Then click the camera icon. Add any notes you like, then click okay.
Switching to Bridged Networking ModeNow we'll adjust the network settings of the virtual machine so we can log into it over ssh from other computers on the LAN, for example, the host machine on which the VM is a guest, or other hosts on the network. To do this, we are going to put the VM into bridged networking mode.
Click "Settings" for the VM inside the VirtualBox Manager, and then select "Network." From the "Attached to" dropdown menu select "Bridged Adapter." Note, the "Name" field contains the name of the connected network interface on the host machine. On my host machine, the eth1 interface is connected to the local network. Select the appropriate interface for your system and then click "OK."
Now start the virtual machine up again. Once it boots up, login with your main account credentials. To log into the virtual machine using ssh over the network, we need to know its IP address. To get the machine's IP address you can run either the ifconfig command or ip a.
Remote Login with SSHAs you can see in the screenshot above, my virtual machine has ip address 192.168.1.104. Yours is likely something similar. Now open a terminal on your host computer itself. From here we log in to the virtual guest via ssh with the following command: ssh <username>@<VM_Ip Address>. I created an account for user named 'admin1' and the IP address is 192.168.1.104. So I can log in to the virtual machine from a terminal on any other computer sitting on the network with the following command: ssh email@example.com. When you are prompted to add the ssh fingerprint, type "yes." When you are prompted for the virtual machine account's password, supply the proper credentials. And you should get a successful login:
We're now logged in to the virtual machine from the terminal of the host computer. Since I created this VM for a database project, I'll make sure mongodb is up and running: ps aux | grep mongo.
Since mongo is running, we should also be able to simply call the mongo command to get a mongo shell: mongo.