As a quick start of trying Linux, I set up a virtual machine (VM) with Ubuntu installed.
Environment
- macOS Big Sur, Version 11.5.2.
- VirtualBox 6.1.30.
Install VirtualBox
VirtualBox is an open-source virtual machine manager. It can be downloaded from: https://www.virtualbox.org/wiki/Downloads.
After the installation, unblock it in the macOS setting to avoid the error of “Kernel driver not installed (rc=-1908)” when starting a VM:
- Open macOS
System Preferences
>Security & Privacy
>General
. - Unlock the setting via
Click the lock to make changes
. - Allow apps downloaded from:
App Store and identified developers
. - Click
Allow
to unblockSystem software from developer "Oracle, America Inc." was blocked from loading
. - Restart macOS.
Create a new VM
Open VirtualBox and then follow the steps below:
- Open
Machine
and then clickNew
.Name
: (name of the VM)Machine Folder
: (where the VM will reside)Type
: LinuxVersion
: Ubuntu 64-bit
- Select
Memory size
: (e.g., 10240 MB). - Config
Hard disk
.- Select
Create a virtual hard disk now
. - For
Hard disk file type
, selectVDI (VirtualBox Disk Image)
. - For
Storage on physical hard disk
, selectDynamically allocated
. - Select
File location and size
: (e.g., 100GB).
- Select
- Click
Create
to create the VM.
Install Ubuntu Server
Download Ubuntu from https://ubuntu.com/download/server. We use 20.04.2 , the latest LTS (long term support) release.
To attach the ISO file to the VM, configure VirtualBox as below:
- Open
Settings
and then selectStorage
. - Under the
Storage Devices
section, select theEmpty
item. - Under the
Attributes
section, click the disk icon and select theChoose a disk file
option. - Open the ISO file.
Start the VM in the Normal Start
mode to proceed with the Ubuntu installation.
In case of the error: “Failed to open a session for the virtual machine”, kill
the VirtualBox process (via Activity Monitor
by selecting the virtualBox
process and then clicking the Stop
button) and then start VirtualBox again.
Install Ubuntu Desktop
The Ubuntu Desktop package is required for accessing the GUI (Graphical User Interface). One way to get the GUI is to use the ISO of Ubuntu Desktop for the Ubuntu installation on the VM. Since we already got Ubuntu Server installed, we can just install the Ubuntu Desktop package.
$ sudo apt-get update
$ sudo apt-get install ubuntu-desktop
Restart the VM in the “Normal Start” mode should direct it to the GUI.
GNOME is the default GUI for most Ubuntu installations. Now we should be able to check the version of GNOME as well.
$ gnome-shell --version
GNOME Shell 3.36.9
Access to the VM
One convenient way of accessing the VM is to use network port forwarding.
- Open
Settings
and then selectNetwork
. - Under
Adapter 1
, unfold theAdvanced
option and then clickPort Forwarding
. - Add the configuration (some examples as below).
Name | Protocol | Host IP | Host Port | Guest IP | Guest Port |
---|---|---|---|---|---|
ssh | tcp | 127.0.0.1 | 2222 | 10.0.2.15 | 22 |
hugo | tcp | 127.0.0.1 | 1313 | 10.0.2.15 | 1313 |
With the above network port forwarding, the VM can be accessed via:
$ ssh username@localhost -p 2222
OpenSSH provides a way to record such SSH option for each remote machine through
a per-user configuration located at ~/.ssh/config
.
A new entry can be added for this VM:
Host vm
HostName localhost
User username
Port 2222
Then the VM can be accessed by:
$ ssh vm
To avoid typing the password every time, we can use SSH keys for authenticating hosts and users.
-
Create an SSH key pair on the host machine. I leave all prompts empty (i.e., using the default location to save the key and ignoring the passphrase).
$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/username/.ssh/id_rsa): Created directory '/home/username/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again:
-
From the host machine, copy its public key to the VM.
ssh-copy-id -i ~/.ssh/id_rsa.pub username@vm