Virtual Box (Mac OS X / Linux)

Virtual Box > Tools > Network

When learning about new systems, applications or trying new settings I find the best way to learn is with a hands-on approach. However, learning hands-on doesn’t always end well and the last thing anyone wants is to have to reinstall their computers operating system (host OS) after it was destroyed due to misconfiguration of a new system.

In steps virtualisation. Virtualisation has been around for quite sometime, and is kind of like the parent of containerisation. Virtualisation allows for a computer (e.g. my MacBook) to run another computer (e.g. a Linux computer running Ubuntu) within a piece of software which creates a virtual machine (VM). This allows for systems, operating systems, applications and various settings to be tested safely without impact on the regular computer (i.e. my MacBook).

There are a few different pieces of software that offer virtualisation from various providers (some free, some not free), however I’m going to be discussing my current favourite which is Virtual Box from Oracle (https://www.virtualbox.org).

Note: There is another piece of software called Vagrant that can be used besides Virtual Box to automate certain processes and can even bring Ansible into play.

Virtual Box Graphical User Interface (GUI)

Virtual Box offers a GUI to help create, run and manage virtual machines. The options for creating a machine (New), modifying a machine (Settings) or starting a machine (Start) are clear on the menu. Once created the virtual machine is listed on the left side of the GUI.

Virtual Box GUI
Virtual Box GUI

Creating a virtual machine involves giving it a name, a location to save the virtual machine to and letting Virtual Box know what type (e.g. Linux, Windows) the box is and what version (e.g. Ubuntu, Debian, Xandros) of operating system it is running. I’ve listed some links to operating systems (OS) below, the links include the ISO files for download and the recommended minimum spec (RAM, hard disk space etc.).

Naming the virtual box
Naming the virtual box

Virtual Box will then ask for how much RAM the virtual machine should have. My MacBook has 8GB of RAM and Virtual Box will allow me to allocate that to the virtual machine however this is really not recommended (which is why the slider is colour coded). Your computer has to run it’s regular operating system (in my case Mac OS X), any applications you have open (e.g. Virtual Box, a web browser, email client) and this uses RAM. If you’ve also assigned that RAM to the operating system in the virtual box then your computer is going to start hitting issues. My personal advice, try not to allocate more than half your computers RAM to the virtual box, and if you are running multiple virtual boxes remember to check RAM allocation between them (i.e. don’t have 2 virtual boxes running with 4GB RAM each on a computer than has 8GB RAM).

Giving the virtual box some RAM
Giving the virtual box some RAM

Next up is hard disk. The hard disk of the virtual machine is stored as a file and uses up space on your computers storage drive, so first thing to note is: Don’t allocate more space to the virtual hard disk than you have available. If you already have a virtual box hard disk file you can use that here (i.e. from a previous virtual box).

Adding a hard disk to the virtual box
Adding a hard disk to the virtual box

There are two options for the type of hard disk:

Dynamically allocated – starts off small and increases to the allowed limit as the Virtual Box uses the space. For example, a 20GB disk may allocation may only use 12GB for the operating system and some files, which means the file will only be 12GB but has the option to grow to 20GB as the virtual box requires. The file does not shrink though, so freeing up space in the virtual box will not make the file smaller.

Fixed size – If you’ve allocated 20GB to the disk then the file is 20GB straight away. This uses more space in the first instance than the dynamically allocated option but is often faster when in use as no resizing has to take place.

Choosing the virtual box hard disk type
Choosing the virtual box hard disk type

With the virtual box created it will need an operating system installing. Click “Settings”, and then “Storage” and choose the empty CD (normally on the IDE Controller). Put a tick in the “Live CD/DVD” and click the CD icon to the left of the menu. If you’ve recently used an ISO it will be listed here, if not click “Choose Virtual Optical Disk File…” and browse to where your ISO is on your computer.

Virtual Box booting from an ISO
Virtual Box booting from an ISO

Remember to “Remove Disk from Virtual Drive” and untick Live CD/DVD once the operating system is installed.

Virtual Networking

Virtual Box also has options for virtual networking, so you can virtual machines talking to each other over a virtual network. Great if you want to get hands on with tools included in security operating systems like Kali. Within the GUI click “Tools” and “Network”.

Virtual Box > Tools > Network
Virtual Box > Tools > Network

Then create a new network with the settings that you want, or let Virtual Box automatically create the network for you. The virtual network can also act as a DHCP server to automatically assign IP addresses to devices if you want.

Creating a virtual box virtual network
Creating a virtual box virtual network

With the virtual network defined, go to the “settings” of a virtual machine and click “network”. Change the network adapter to “Host-only Adapter” and then the virtual machine will only be able to communicate with the host and any other virtual machines on the virtual network.

virtual network adapter settings
virtual network adapter settings

The virtual boxes can have up to 4 network adapters, so you could have them talk to 4 different networks, or host only networks as well as NAT’ed networks.

Virtual Box Command Line Interface (CLI)

GUIs are cool, but a CLI generally allows for commands to be carried out quicker and can be scripted to carry out tasks that would take a multiple clicks in a GUI. Some of the basic commands for Virtual Box are:

List VMs
VBoxManage list vms

List Running VMs
VBoxManage list runningvms

List VMs with more details
VBoxManage list -l vms
VBoxManage list -l runningvms

MANAGEMENT:

–type can be gui , headless or separate

If you want to run the VM in the background (i.e. services are running on the VM or you are SSH’ing into it so don’t need to see the VM operating system) then “headless” is the option you want.

Start VM (Headless)
VBoxManage startvm “VM NAME” –type headless

Power off VM
VBoxManage controlvm “VM NAME” poweroff

Pause VM (Headless)
VBoxManage controlvm “VM NAME” pause –type headless

Resume VM (Headless)
VBoxManage controlvm “VM NAME” resume –type headless

Creating a virtual box via CLI

First off, run the command:

VBoxManage list ostypes

This will output all the different operating system types that the virtual box can be. I’m looking for Ubuntu and it is going to be 64-bit, so Ubuntu_64 is the ID I will need.

geektechstuff_virtual_box_cli_0
Virtual Box List of OS types

The next command registers the virtual box:

VBoxManage createvm -name GeekTestStuffCLIBox –ostype Ubuntu_64 –register

geektechstuff_virtual_box_cli_1
Creating a virtual box via CLI

Note: GeekTestStuffCLIBox and Ubuntu_64 should be swopped with the values of your choice, i.e. a name and the OS type.

If successful VBoxManage will output a UUID for the virtual box and let you know where the virtual boxes settings file (.vbox file) is stored. The virtual box will also show in the GUI at this point, just incase you want to manage it in the GUI.

geektechstuff_virtual_box_cli_2
The CLI virtual box will also show in the GUI

However, I am going to continue setting the new virtual box up via the CLI. The next command is:

VBoxManage showvminfo GeekTestStuffCLIBox

(Replace GeekTestStuffCLIBox with the name of your virtual box!)

A long list should now show indicating the current virtual boxes settings. It might be outputting the list to a text file using:

VBoxManage showvminfo GeekTestStuffCLIBox >> options.txt

VBoxManage showvminfo
VBoxManage showvminfo
VBoxManage showvminfo
VBoxManage showvminfo

The above output shows that my CLI virtual box has 128MB RAM (aka “Memory Size”), 1 CPU enabled (“Number of CPUS”) and the video RAM set to 8MB (“VRAM size”).

Let’s give the virtual box a little more power.

VBoxManage modifyvm GeekTestStuffCLIBox –cpus 2 –memory 4096 –vram 64

VBoxManage modifyvm GeekTestStuffCLIBox --cpus 2 --memory 4096 --vram 64
VBoxManage modifyvm GeekTestStuffCLIBox –cpus 2 –memory 4096 –vram 64

This command ups the number of CPUs to 2, increases the amount of RAM to 4096MB and the amount of VRAM to 64MB. Now we need to configure some storage for the virtual box.

VBoxManage createmedium disk –filename geektechstuffdisk –size 15000

geektechstuff_virtual_box_cli_6

–filename is the name and location you want to give / save the disk file to

–size is the size of the disk in megabytes (MB). 1GB is 1024 MB.

–variant can be added to change the disk type to either standard (which is dynamic from the earlier discussion) or fixed.

With the disk image created it next needs to be added to the virtual box and to this the virtual box needs a SATA controller. This is done with the command:

VBoxManage storagectl GeekTestStuffCLIBox –name “SATA Controller” –add sata –bootable on

VBoxManage storagectl GeekTestStuffCLIBox --name "SATA Controller" --add sata --bootable on
VBoxManage storagectl GeekTestStuffCLIBox –name “SATA Controller” –add sata –bootable on

And then to add the disk file to the controller:

VBoxManage storageattach GeekTestStuffCLIBox –storagectl “SATA Controller”  –port 0 –device 0 –type hdd –medium geektechstuffdisk.vdi

VBoxManage storageattach GeekTestStuffCLIBox --storagectl "SATA Controller"  --port 0 --device 0 --type hdd --medium geektechstuffdisk.vdi
VBoxManage storageattach GeekTestStuffCLIBox –storagectl “SATA Controller”  –port 0 –device 0 –type hdd –medium geektechstuffdisk.vdi

We know have a virtual box with 2 processors, 4GB RAM, 64MB VRAM and a (roughly) 15GB hard disk drive.

But what about installing the OS? For this we need to add a virtual CD/DVD drive using:

VBoxManage storagectl GeekTestStuffCLIBox –name “IDE Controller” –add ide

VBoxManage storagectl GeekTestStuffCLIBox --name "IDE Controller" --add ide
VBoxManage storagectl GeekTestStuffCLIBox –name “IDE Controller” –add ide

VBoxManage storageattach GeekTestStuffCLIBox –storagectl “IDE Controller” –port 0  –device 0 –type dvddrive –medium “/users/gary/Downloads/ubuntu.iso”

VBoxManage storageattach GeekTestStuffCLIBox --storagectl "IDE Controller" --port 0  --device 0 --type dvddrive --medium "/users/gary/Downloads/ubuntu.iso"
VBoxManage storageattach GeekTestStuffCLIBox –storagectl “IDE Controller” –port 0  –device 0 –type dvddrive –medium “/users/gary/Downloads/ubuntu.iso”

To remove the ISO from the drive:

VBoxManage storageattach GeekTestStuffCLIBox –storagectl “IDE Controller” –port 0  –device 0 –type dvddrive –medium none

Then the virtual box is ready for powering on:

VBoxManage startvm GeekTestStuffCLIBox

geektechstuff_virtual_box_cli_11
VBoxManage startvm GeekTestStuffCLIBox

 

Operating Systems

After an operating system (OS) for your virtual box? If the OS is available as an ISO then Virtual Box can run it as a live CD. There are lots of them available, too many for me to list but here is a small sample:

Note: The Windows OS ISOs are either evaluations or require a license. 

Ubuntu (Linux)

https://ubuntu.com/download

Kali (Linux)

https://www.kali.org/downloads/ for an ISO or https://www.offensive-security.com/kali-linux-vm-vmware-virtualbox-image-download/ for a pre-built image.

CentOS (Linux)

https://www.centos.org/download/

Mint (Linux)

https://linuxmint.com/download.php

Raspberry Pi Desktop (Linux)

https://www.raspberrypi.org/downloads/raspberry-pi-desktop/

Windows 10 (Windows)

https://www.microsoft.com/en-gb/software-download/windows10ISO

Windows Server 2019 (Windows)

https://www.microsoft.com/en-us/evalcenter/evaluate-windows-server-2019?filetype=ISO

If you don’t have an internet connection that is suitable for downloading an operating system ISO then check out the cover discs (by purchasing the magazine) found on the front of computing magazines such as Linux Format as these generally contain different operating systems each month.

One thought on “Virtual Box (Mac OS X / Linux)

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.