Virtual Machines and Linux
Slides
Introduction
Required Software
Download and install VirtualBox
Download Ubuntu 24.04 ISO file
Linux & Virtual Machines
Brief Introduction to Linux & Unix
Linux is a Unix-like operating system kernel, known for being the most popular kernel in the world. It's widely used in various devices and systems, including Android smartphones, Chromebooks, most routers, servers, and even supercomputers.
Unix-like systems, including Linux, are at the heart of the most popular operating system family in the world. Their architecture and principles have influenced countless other systems, making them a staple in the world of computing.
If you're a computing student, sooner or later, you'll find yourself developing for a Unix-like platform!
Virtual Machine: What? Why?
A virtual machine (VM) is a simulated computer that allows you to run an operating system and applications in a completely isolated environment. You can configure a guest virtual machine with any operating system and settings you want, and use it without affecting your host environment.
Why Use a Virtual Machine?
Virtual machines are incredibly useful for several reasons:
Experimentation: They allow you to test out different operating systems, software, and configurations without the risk of damaging your main system.
Software Compatibility: You can run software that is only compatible with a specific operating system by creating a VM with that OS.
Safe Testing: If you need to experiment with potentially malicious software, a VM provides a safe, isolated environment to do so.
Useful Features of VMs
Isolation: A VM isolates the guest environment from the host, which means you can run buggy or untrusted software with a reasonable level of safety.
Snapshots: VMs can take snapshots, which capture the entire machine's state at a particular point in time. This allows you to make changes, test configurations, or install software and then easily revert to a previous state if something goes wrong.
Disadvantages of Virtual Machines
Performance: VMs are generally slower compared to running an operating system directly on your hardware (bare metal).
Resource Competition: VMs share the host system's resources, such as CPU, memory, and storage, which can impact performance.
Unsuitability for Certain Applications: VMs might not be ideal for resource-intensive applications like games or high-performance computing tasks.
Virtual Machine: Setting up
Why Choose VirtualBox?
We're using VirtualBox because it offers several advantages:
Free and Open-Source Software (FOSS): VirtualBox is completely free and open-source, making it accessible to everyone.
Graphical User Interface (GUI): It comes with a user-friendly graphical interface, making it easier to use, especially for beginners.
Cross-Platform: VirtualBox works on multiple operating systems, including Windows and Linux. There is a developer preview for M1+ Macs, but the performance isn't great.
Setting up your first virtual machine
1. VirtualBox Main UI
Once you open VirtualBox, click on the "Add" button to start creating a new virtual machine.

2. Creating a New VM
Name: Enter "Ubuntu" as the name of your new virtual machine. VirtualBox should automatically detect the type (Linux) and version (Ubuntu) based on the name.

3. Set the Amount of Memory (RAM)
Memory Allocation: Ubuntu requires a minimum of 512 MiB of RAM and recommends 2 GiB. However, as a general rule, do not allocate more than 1/4 of your physical RAM to the virtual machine to ensure that your host system runs smoothly.

4. Create a Virtual Hard Disk
Step 1: Your virtual machine needs a virtual hard disk. Click on "Create" to begin setting it up.
Step 2: Use the default virtual hard disk format for the best performance.

Step 3: Opt for a dynamically allocated disk. This means the virtual hard disk will only use as much space as it currently needs, rather than reserving a large chunk of your storage upfront.

Step 4: Ubuntu recommends a minimum of 10 GiB of storage and 25 GiB for a full installation. For this guide, we'll be using the minimum installation, which will require about 6 GiB.

Back to the Main UI
After setting up the virtual hard disk, return to the main VirtualBox interface and click on "Settings".

Configuring Settings
Storage Settings: Navigate to "Storage", then select the "Empty" slot under "Controller: IDE". Click on the disc icon beside "IDE Secondary Master", and then choose "Choose Virtual Optical Disk File".

Choose Your Ubuntu ISO File
Select the Ubuntu ISO file that you have downloaded earlier.

Final Steps
You're all set up with VirtualBox! You can further customize your settings later if needed. For now, return to the main UI and click "Start" to launch your new virtual machine.
For power users
Skip this part if you're not familiar with working in a command line environment
We can easily do exactly what we just did in a matter of seconds, using the command line interface (cli).
VBoxManage createvm --name "Ubuntu" --ostype Ubuntu24_LTS_64 --register
VBoxManage modifyvm "Ubuntu" --memory 2048 --acpi on --boot1 dvd --nic1 nat
VBoxManage createhd --filename "Ubuntu.vdi" --size 40960
VBoxManage storagectl "Ubuntu" --name "IDE Controller" --add ide --controller PIIX4
VBoxManage storageattach "Ubuntu" --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium "Ubuntu.vdi"
VBoxManage storageattach "Ubuntu" --storagectl "IDE Controller" --port 0 --device 1 --type dvddrive --medium /full/path/to/iso.iso
VBoxManage modifyvm "Ubuntu" --cpus 4
VBoxManage modifyvm "Ubuntu" --vrde on
VBoxHeadless --startvm "Ubuntu"
Installing Ubuntu
Booting Ubuntu
Once your virtual machine starts and boots up, you should see a screen like this:

Step 1: Choose "Install Ubuntu" to begin the installation process.
Choose Keyboard Layout

Step 2: Select your keyboard layout. If you're using a computer purchased in Singapore, the default layout should be "English (US)."
Updates and Other Software

Step 3: Opt for a "Minimal Installation" to save time during the installation process. You can leave the checkboxes unticked for a quicker setup.
Installation Type

Step 4: Choose "Erase disk and install Ubuntu" to proceed with the installation. When the dialogue box appears, click "Continue" to confirm.
Location

Step 5: Ubuntu should automatically detect your location as Singapore. If it doesn't, you can manually select your location.
Setting Up Your Username

Step 6: Enter your name and create a password for your Ubuntu installation.
Sit Back and Relax

Step 7: Now, just sit back and relax while Ubuntu installs. This may take some time, so feel free to take a break while the installation completes.
Guest Additions
Many Linux distributions offer Guest Additions through their package repositories, making the installation process straightforward. To install them:
Open a terminal in your Ubuntu virtual machine.
Run the following commands:
sudo -i apt update apt install virtualbox-guest-utils virtualbox-guest-x11
This will install the necessary utilities and drivers to enable Guest Additions features.
Installing Guest Additions Using the ISO
If you're using an operating system like Windows or another OS that doesn't have Guest Additions available in its package repository, you can install them using a CD image:
In VirtualBox, go to the "Devices" menu.
Select "Insert Guest Additions CD image...".
This will mount the Guest Additions as a virtual CD in your VM.
Follow the on-screen instructions to install the software.
Shared Folders
One cool feature of Guest Additions is shared folders. This provides a useful interface for sharing files between the host machine and virtual machine.
Click on Settings > Shared Folders

Click on the folder icon on the right

The folder path should point to the location on the host machine
Give the folder a folder name, let's say "SharedFolder"
Now in your virtual machine, run the following command
mkdir ~/Shared
sudo mount -t vboxsf SharedFolder ~/Shared/ -o uid=1000,gid=1000
Now if you drag a folder into your shared folder in your host, you should see it appear in your virtual machine as well!
Nifty Tricks with VM
Force Shutdown the VM
Sometimes, you might need to run unstable software on your virtual machine, which could cause the VM to hang or become unresponsive. If this happens, you can force a shutdown:
Step 1: Close the VirtualBox window for your VM.
Step 2: When prompted, choose "Power off the machine." This will force the VM to shut down immediately.
Step 3: You can quickly bring the VM back up by starting it again from the main VirtualBox interface.
Saving Machine State
You don't always need to completely shut down the operating system inside your VM when you're done working:
Step 1: To pause and save the VM's current state, simply close the VM's window.
Step 2: Choose "Save the machine state" from the options. This will pause the VM, allowing you to resume exactly where you left off the next time you start it.
What is a Snapshot?
A snapshot is like a time machine for your VM. It captures the state of your virtual machine at a specific point in time. You can return to this state later if needed, making snapshots very useful for experimentation or testing.
Taking a Snapshot in VirtualBox
To take a snapshot:
Step 1: In the VirtualBox main interface, click on the list icon beside your VM name (e.g., "Ubuntu").
Step 2: Select "Snapshots" to view, take, or manage snapshots.
Restoring a Snapshot in VirtualBox
If you want to restore your VM to a previous snapshot:
Step 1: Ensure your VM is shut down. If necessary, either shut down the OS within the VM or close the VM and select "Power off the machine."
Step 2: In the list of snapshots, select the one you want to restore.
Step 3: Click "Restore" to revert your VM to the selected snapshot.
Use Cases
Running risky programs or commands
SSH with VSCode
Experimenting with low level programs that might break your computer
Last updated