6.2 Setting up a simple virtual machine
Let’s create our first virtual machine.
Host operating system
The following commands should work on MacOS, Linux, or Windows 104
First create a directory within which we will create our virtual machine and make this our current working directory.
1mkdir vbclass 2cd vbclass
Now open a command line terminal and download the installation media we need to build our server. Since we are building a Debian server we download a Debian installation ISO.
1curl -O https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/\ 2 debian-10.9.0-amd64-netinst.iso -L
curl is generally available as a default command line tool nowadays but you may need to install it on your host (or just download the ISO via your browser).
Now we run a series of VBoxManage commands to setup the virtual hardware for out virtual machine.
1VBoxManage createvm --name "vbdemo" --register --basefolder "$(pwd)" 2VBoxManage createhd --filename vbdemo/vbdemo.vdi --size 20000 3VBoxManage storagectl "vbdemo" --name "SATA Controller" --add sata 4VBoxManage storageattach "vbdemo" --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium vbdemo/vbdemo.vdi 5VBoxManage storageattach "vbdemo" --storagectl "SATA Controller" --port 1 --device 0 --type dvddrive --medium ./debian-10.9.0-amd64-netinst.iso 6VBoxManage modifyvm "vbdemo" --memory 8192 7VBoxManage modifyvm "vbdemo" --nic1 bridged --bridgeadapter1 en1 8VBoxManage modifyvm "vbdemo" --ostype Debian
We start by creating the base VM settings file and registering the VM with the VirtualBox library (line 1). The settings file is an XML file called vbdemo.vbox in a vbdemo subdirectory for the current working directory. You will also notice that looking in the VirtualBox library that the new VM is registered.
Once this base VM settings file is available we can start adding custom hardware to our virtual machine.
Line 2 creates a virtual disk 20GB in size in the same directory as the settings file. This new disk is empty when created.
Line 3 creates a SATA storage controller in our new VM and then lines 4 and 5 associate disks with this controller. Line 4 associates the virtual disk we just created in line 2 and line 5 associates the Debian installation ISO downloaded previously. Line 5 is the equivalent of loading a Debian installation CD into a CD drive attached to the virtual machine (note the --type dvddrive option on line 5).
Line 6 gives our memory a boost to 8GB, you may need to modify this to suit your host machine (it’s generally a bad idea to assign more virtual memory to a virtual machine than you have physical memory on your host machine).
Line 7 configures a network interface card (nic) on the virtual machine. In this set up we are simply creating a bridged network interface, which means the virtual machine will we network device en1 as if it were attached to your local network.
Line 8 tells VirtualBox to expect a Debian operating system on this virtual machine.
After running these commands we have the following situation.
- A directory containing a virtual machine setting file (vbdemo/vbdemo.vbox) and a virtual disk (vbdemo/vbdemo.vdi).
- The settings file has been modified so that the virtual machine:
- has 8 GB RAM
- a SATA controler with two drives attached:
- the virtual 20GB hard drive (vbdemo/vbdemo.vdi)
- a DVD drive containing the Debian installer (./debian-10.6. 0-amd64-netinst.iso)
- a network interface card (en1) ‘wired’ to your host computer’s network
When we start this virtual machine the hardware will present as specified in the settings file and, as the hard drive has nothing installed on it, the system will boot from the virtual DVD drive, beginning the Debian installer.
Start the virtual machine.
1VBoxHeadless --startvm "vbdemo" &
This will start the virtual machine ‘headless’, without a virtual display attached. This may seem odd, but in future we will access our virtual machines through SSH so none of the machines will be configured to use a display other than its console. This is the situation you are most likely to encounter professionally.
Every machine has a console. The console is a screen and keyboard directly attached to the machine. In a data centre these screens and keyboards are typically physical devices installed into the racks holding the servers and they are used by data centre personnel to monitor and control servers in the rack. Data centres are also commonly configured to allow remote access to consoles in addition to the physical console devices, saving personnel from needing to enter the physical data centre to access the console. In virtual environments (such as ours) the console is accessed virtually.
- Using the VirtualBox library GUI.
- Using the Remote Desktop Protocol (RDP)5 which VirtualBox makes available on port 3389.
Once on the virtual machine’s console you will see the Debian installer and you can walk through the installation steps as you would for a physical machine.
At the end of the installation you will be prompted to remove the installation CD and reboot. The Debian installation ISO mounted in the virtual DVD drive will be automatically ‘ejected’ as the virtual machine reboots, so just select to reboot the virtual machine and you will see for virtual machine restart into a Debian console prompting you for the username and password to login.