Installing VirtualBox Guest Additions to a Virtual machine

VirtualBox Guest Additions consist of device drivers and system applications that optimize the guest operating system for better performance and usability with the host operating system .

Guest Additions are designed to be installed inside a virtual machine after the guest operating system has been installed. Now we have a basic VM running, we can add the VirtualBox Guest Additions to the VM.

Installing VirtualBox Guest Additions

Before installing the guest additions, we will need to install the Linux kernel headers and the basic developer tools in the virtual machine . These are needed to build the VirtualBox Guest Additions for the virtual machine. On Ubuntu we can easily install these with:

root@ubuntu-focal:/home/vagrant# apt-get install linux-headers-$(uname -r) build-essential dkms 
Reading package lists... Done 
Building dependency tree        
Reading state information... Done 
linux-headers-5.4.0-126-generic is already the newest version (5.4.0-126.142). 
linux-headers-5.4.0-126-generic set to manually installed.
The following additional packages will be installed: 
  binutils binutils-common binutils-x86-64-linux-gnu cpp cpp-9 dctrl-tools dpkg-dev
  fakeroot g++ g++-9 gcc
... snipped ...
Suggested packages:
  binutils-doc cpp-doc gcc-9-locales debtags menu debian-keyring g++-multilib ...
The following NEW packages will be installed: 
  binutils binutils-common binutils-x86-64-linux-gnu build-essential cpp cpp-9 dctrl-tools dkms dpkg-dev fakeroot g++ g++-9 gcc 
... snipped ...
0 upgraded, 43 newly installed, 0 to remove and 0 not upgraded. 
Need to get 45.5 MB of archives. 
After this operation, 203 MB of additional disk space will be used.
Do you want to continue? [Y/n]Y 
Get:1 href="http://archive.ubuntu.com/ubuntu">http://archive.ubuntu.com/ubuntu focal-updates/main amd64 gcc-9-base amd64 9.4.0-1ubuntu1~20.04.1 [19.4 kB]
Get:2 href="http://archive.ubuntu.com/ubuntu">http://archive.ubuntu.com/ubuntu focal/main amd64 libisl22 amd64 0.22.1-1 [592 kB]
... snipped ...
Fetched 45.5 MB in 8s (5496 kB/s) 
Extracting templates from packages: 100% 
Selecting previously unselected package gcc-9-base:amd64. 
(Reading database ... 63963 files and directories currently installed.) 
Preparing to unpack .../00-gcc-9-base_9.4.0-1ubuntu1~20.04.1_amd64.deb ... 
Unpacking gcc-9-base:amd64 (9.4.0-1ubuntu1~20.04.1) ...
... snipped ...
Setting up g++-9 (9.4.0-1ubuntu1~20.04.1) ...
Setting up g++ (4:9.3.0-1ubuntu2) ...
update-alternatives: using /usr/bin/g++ to provide /usr/bin/c++ (c++) in auto mode 
Setting up build-essential (12.8ubuntu1.1) ... 
Processing triggers for man-db (2.9.1-1) ... 
Processing triggers for libc-bin (2.31-0ubuntu9.9) ... 
root@ubuntu-focal:/home/vagrant#

We will also need to install the virtualbox-guest-dkms package as well using the following command:

root@ubuntu-focal:/home/vagrant# apt install virtualbox-guest-dkms
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  virtualbox-guest-dkms
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 676 kB of archives.
After this operation, 7767 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 virtualbox-guest-dkms all 6.1.38-dfsg-3~ubuntu1.20.04.1 [676 kB]
Fetched 676 kB in 0s (3931 kB/s)             
Selecting previously unselected package virtualbox-guest-dkms.
(Reading database ... 69838 files and directories currently installed.)
Preparing to unpack .../virtualbox-guest-dkms_6.1.38-dfsg-3~ubuntu1.20.04.1_all.deb ...
Unpacking virtualbox-guest-dkms (6.1.38-dfsg-3~ubuntu1.20.04.1) ...
Setting up virtualbox-guest-dkms (6.1.38-dfsg-3~ubuntu1.20.04.1) ...
Loading new virtualbox-guest-6.1.38 DKMS files...
Building for 5.4.0-126-generic
Building initial module for 5.4.0-126-generic
Done.

vboxguest.ko:
Running module version sanity check.
 - Original module
 - Installation
   - Installing to /lib/modules/5.4.0-126-generic/updates/dkms/

vboxsf.ko:
Running module version sanity check.
 - Original module
 - Installation
   - Installing to /lib/modules/5.4.0-126-generic/updates/dkms/

vboxvideo.ko:
Running module version sanity check.
 - Original module
 - Installation
   - Installing to /lib/modules/5.4.0-126-generic/updates/dkms/

depmod....

DKMS: install completed.
root@ubuntu-focal:/home/vagrant#

We now need to access to the contents of the VBoxGuestAdditions_6.1.38.iso, so we can see what's in the virtual optical drive, and install the Guest Additions from there. To do this we create a mount point to attach the VBoxGuestAdditions_6.1.38.iso optical drive to.

The lsblk command lists all block devices on the virtual machine:

root@ubuntu-focal:/vagrant# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0    7:0    0 63.2M  1 loop /snap/core20/1623
loop1    7:1    0   48M  1 loop /snap/snapd/17029
loop2    7:2    0 67.8M  1 loop /snap/lxd/22753
sda      8:0    0   40G  0 disk  
└─sda1   8:1    0   40G  0 part /
sr0     11:0    1   61M  0 rom

This tells us that the virtual optical drive is called /dev/sr0

We can create a mount point called /media/cdrom to attach the optical drive to with the following command:

root@ubuntu-focal:/home/vagrant# mkdir /media/cdrom

We can now attach the optical drive to the above mount point with the following command:

root@ubuntu-focal:/vagrant# mount /dev/sr0 /media/cdrom
mount: /media/cdrom: WARNING: device write-protected, mounted read-only.

We can confirm the optical drive is now attached to the /media/cdrom mount point with the following command:

root@ubuntu-focal:/vagrant# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            472M     0  472M   0% /dev
tmpfs            98M  952K   97M   1% /run
/dev/sda1        39G  1.8G   37G   5% /
tmpfs           489M     0  489M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           489M     0  489M   0% /sys/fs/cgroup
/dev/loop0       64M   64M     0 100% /snap/core20/1623
/dev/loop2       68M   68M     0 100% /snap/lxd/22753
/dev/loop1       48M   48M     0 100% /snap/snapd/17029
vagrant         456G   44G  412G  10% /vagrant
tmpfs            98M     0   98M   0% /run/user/1000
/dev/sr0         61M   61M     0 100% /media/cdrom

We can now list the contents of the optical drive from the command line with:

root@ubuntu-focal:/vagrant# ls -l /media/cdrom
total 49573
Feb 20  2020 AUTORUN.INF
Sep  1 14:23 NT3x
Sep  1 14:23 OS2
Sep  1 14:23 TRANS.TBL
Sep  1 13:46 VBoxDarwinAdditions.pkg
Sep  1 13:42 VBoxDarwinAdditionsUninstall.tool
Sep  1 13:43 VBoxLinuxAdditions.run
Sep  1 13:40 VBoxSolarisAdditions.pkg
Sep  1 14:23 VBoxWindowsAdditions-amd64.exe
Sep  1 14:02 VBoxWindowsAdditions-x86.exe
Sep  1 13:45 VBoxWindowsAdditions.exe
Sep  1 13:43 autorun.sh
Sep  1 14:23 cert
Sep  1 13:43 runasroot.sh
Oct  4  2021 windows11-bypass.reg

If we start mc, we can get a better view of the files and directories on the cdrom drive:

We can drop to a command line sub-shell from MC using the CTRL-o key combination - this will also take us back to MC again from the sub-shell.

To compile and install the VirtualBox Guest Additions, we can run the following command:

root@ubuntu-focal:/media/cdrom# ./VBoxLinuxAdditions.run 
Verifying archive integrity... All good.
Uncompressing VirtualBox 6.1.38 Guest Additions for Linux........
VirtualBox Guest Additions installer

This system appears to have a version of the VirtualBox Guest Additions
already installed.  If it is part of the operating system and kept up-to-date,
there is most likely no need to replace it.  If it is not up-to-date, you
should get a notification when you start the system.  If you wish to replace
it with this version, please do not continue with this installation now, but
instead remove the current version first, following the instructions for the
operating system.

If your system simply has the remains of a version of the Additions you could
not remove you should probably continue now, and these will be removed during
installation.

Do you wish to continue? [yes or no]
yes
touch: cannot touch '/var/lib/VBoxGuestAdditions/skip-5.4.0-126-generic': No such file or directory
Copying additional installer modules ...
Installing additional modules ...

/opt/VBoxGuestAdditions-6.1.38/bin/VBoxClient: error while loading shared libraries: libXt.so.6: cannot open shared object file: No such file or directory

/opt/VBoxGuestAdditions-6.1.38/bin/VBoxClient: error while loading shared libraries: libXt.so.6: cannot open shared object file: No such file or directory

VirtualBox Guest Additions: Starting.
VirtualBox Guest Additions: Building the VirtualBox Guest Additions kernel 
modules.  This may take a while.
VirtualBox Guest Additions: To build modules for other installed kernels, run
VirtualBox Guest Additions:   /sbin/rcvboxadd quicksetup <version>
VirtualBox Guest Additions: or
VirtualBox Guest Additions:   /sbin/rcvboxadd quicksetup all

VirtualBox Guest Additions: Building the modules for kernel 5.4.0-126-generic.
update-initramfs: Generating /boot/initrd.img-5.4.0-126-generic
VirtualBox Guest Additions: Running kernel modules will not be replaced until 
the system is restarted
root@ubuntu-focal:/media/cdrom#

We appear to have a missing shared library file called libXt.so.6  - so to find out what package provides this, we need to install the apt-file package, with:

root@ubuntu-focal:/media/cdrom# apt install apt-file
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libapt-pkg-perl libexporter-tiny-perl liblist-moreutils-perl libregexp-assemble-perl
The following NEW packages will be installed:
  apt-file libapt-pkg-perl libexporter-tiny-perl liblist-moreutils-perl
  libregexp-assemble-perl
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Need to get 265 kB of archives.
After this operation, 806 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 libapt-pkg-perl amd64 0.1.36build3 [68.5 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal/main amd64 libexporter-tiny-perl all 1.002001-1 [35.5 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal/main amd64 liblist-moreutils-perl amd64 0.416-1build5 [55.5 kB]
Get:4 http://archive.ubuntu.com/ubuntu focal/universe amd64 libregexp-assemble-perl all 0.36-1 [77.5 kB]
Get:5 http://archive.ubuntu.com/ubuntu focal/universe amd64 apt-file all 3.2.2 [27.6 kB]
Fetched 265 kB in 1s (405 kB/s)
Selecting previously unselected package libapt-pkg-perl.
(Reading database ... 69667 files and directories currently installed.)
Preparing to unpack .../libapt-pkg-perl_0.1.36build3_amd64.deb ...
Unpacking libapt-pkg-perl (0.1.36build3) ...
Selecting previously unselected package libexporter-tiny-perl.
Preparing to unpack .../libexporter-tiny-perl_1.002001-1_all.deb ...
Unpacking libexporter-tiny-perl (1.002001-1) ...
Selecting previously unselected package liblist-moreutils-perl.
Preparing to unpack .../liblist-moreutils-perl_0.416-1build5_amd64.deb ...
Unpacking liblist-moreutils-perl (0.416-1build5) ...
Selecting previously unselected package libregexp-assemble-perl.
Preparing to unpack .../libregexp-assemble-perl_0.36-1_all.deb ...
Unpacking libregexp-assemble-perl (0.36-1) ...
Selecting previously unselected package apt-file.
Preparing to unpack .../apt-file_3.2.2_all.deb ...
Unpacking apt-file (3.2.2) ...
Setting up libapt-pkg-perl (0.1.36build3) ...
Setting up libexporter-tiny-perl (1.002001-1) ...
Setting up libregexp-assemble-perl (0.36-1) ...
Setting up liblist-moreutils-perl (0.416-1build5) ...
Setting up apt-file (3.2.2) ...
The system-wide cache is empty. You may want to run 'apt-file update'
as root to update the cache.
Processing triggers for man-db (2.9.1-1) ...
root@ubuntu-focal:/media/cdrom#

Next, we need to run the apt-file update command to build the system-wide cache for apt-file to use to locate the package that provides the missing shared library:

root@ubuntu-focal:/media/cdrom# apt-file update
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
Get:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]             
Get:4 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]             
Get:5 http://archive.ubuntu.com/ubuntu focal amd64 Contents (deb) [40.9 MB]
Get:6 http://security.ubuntu.com/ubuntu focal-security amd64 Contents (deb) [133 MB]
Get:7 http://archive.ubuntu.com/ubuntu focal-updates amd64 Contents (deb) [145 MB]                                                  
Get:8 http://archive.ubuntu.com/ubuntu focal-backports amd64 Contents (deb) [791 kB]                                                
Fetched 320 MB in 1min 49s (2946 kB/s)                                                                                              
Reading package lists... Done
Building dependency tree       
Reading state information... Done
All packages are up to date.
root@ubuntu-focal:/media/cdrom#

Having done that - we can now ask apt-file to tell us what package provides the missing libXt.so.6 shared library file we need.

root@ubuntu-focal:/media/cdrom# apt-file search libXt.so.6
libxt6: /usr/lib/x86_64-linux-gnu/libXt.so.6
libxt6: /usr/lib/x86_64-linux-gnu/libXt.so.6.0.0

So it appears we are missing the following package - libxt6

Let's install the missing package - and try to compile the GuestAdditions again:

root@ubuntu-focal:/media/cdrom# apt install libxt6 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libice6 libsm6 x11-common
The following NEW packages will be installed:
  libice6 libsm6 libxt6 x11-common
0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded.
Need to get 239 kB of archives.
After this operation, 962 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 x11-common all 1:7.7+19ubuntu14 [22.3 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal/main amd64 libice6 amd64 2:1.0.10-0ubuntu1 [41.0 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal/main amd64 libsm6 amd64 2:1.2.3-1 [16.1 kB]
Get:4 http://archive.ubuntu.com/ubuntu focal/main amd64 libxt6 amd64 1:1.1.5-1 [160 kB]
Fetched 239 kB in 1s (271 kB/s)
Selecting previously unselected package x11-common.
(Reading database ... 69789 files and directories currently installed.)
Preparing to unpack .../x11-common_1%3a7.7+19ubuntu14_all.deb ...
dpkg-query: no packages found matching nux-tools
Unpacking x11-common (1:7.7+19ubuntu14) ...
Selecting previously unselected package libice6:amd64.
Preparing to unpack .../libice6_2%3a1.0.10-0ubuntu1_amd64.deb ...
Unpacking libice6:amd64 (2:1.0.10-0ubuntu1) ...
Selecting previously unselected package libsm6:amd64.
Preparing to unpack .../libsm6_2%3a1.2.3-1_amd64.deb ...
Unpacking libsm6:amd64 (2:1.2.3-1) ...
Selecting previously unselected package libxt6:amd64.
Preparing to unpack .../libxt6_1%3a1.1.5-1_amd64.deb ...
Unpacking libxt6:amd64 (1:1.1.5-1) ...
Setting up x11-common (1:7.7+19ubuntu14) ...
update-rc.d: warning: start and stop actions are no longer supported; falling back to defaults
Setting up libice6:amd64 (2:1.0.10-0ubuntu1) ...
Setting up libsm6:amd64 (2:1.2.3-1) ...
Setting up libxt6:amd64 (1:1.1.5-1) ...
Processing triggers for systemd (245.4-4ubuntu3.18) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.9) ...
root@ubuntu-focal:/media/cdrom#

We've fixed the missing libXt.so.6 shared library file, but there's now something else missing:

root@ubuntu-focal:/media/cdrom# ./VBoxLinuxAdditions.run 
Verifying archive integrity... All good.
Uncompressing VirtualBox 6.1.38 Guest Additions for Linux........
VirtualBox Guest Additions installer

Removing installed version 6.1.38 of VirtualBox Guest Additions...
update-initramfs: Generating /boot/initrd.img-5.4.0-126-generic
Copying additional installer modules ...
Installing additional modules ...

/opt/VBoxGuestAdditions-6.1.38/bin/VBoxClient: error while loading shared libraries: libXmu.so.6: cannot open shared object file: No such file or directory

/opt/VBoxGuestAdditions-6.1.38/bin/VBoxClient: error while loading shared libraries: libXmu.so.6: cannot open shared object file: No such file or directory

VirtualBox Guest Additions: Starting.
VirtualBox Guest Additions: Building the VirtualBox Guest Additions kernel 
modules.  This may take a while.
VirtualBox Guest Additions: To build modules for other installed kernels, run
VirtualBox Guest Additions:   /sbin/rcvboxadd quicksetup <version>
VirtualBox Guest Additions: or
VirtualBox Guest Additions:   /sbin/rcvboxadd quicksetup all

VirtualBox Guest Additions: Building the modules for kernel 5.4.0-126-generic.
update-initramfs: Generating /boot/initrd.img-5.4.0-126-generic
VirtualBox Guest Additions: Running kernel modules will not be replaced until 
the system is restarted
root@ubuntu-focal:/media/cdrom#

Let's see what an apt-file search tells us about what package we need to install to get the libXmu.so.6 shared library:

root@ubuntu-focal:/media/cdrom# apt-file search libXmu.so.6
libxmu6: /usr/lib/x86_64-linux-gnu/libXmu.so.6
libxmu6: /usr/lib/x86_64-linux-gnu/libXmu.so.6.2.0

So it appears we are now missing the following package - libxmu6

Let's install the missing package - and try to compile the GuestAdditions again:

root@ubuntu-focal:/media/cdrom# apt install libxmu6
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  libxmu6
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 45.8 kB of archives.
After this operation, 130 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 libxmu6 amd64 2:1.1.3-0ubuntu1 [45.8 kB]
Fetched 45.8 kB in 0s (119 kB/s)   
Selecting previously unselected package libxmu6:amd64.
(Reading database ... 69833 files and directories currently installed.)
Preparing to unpack .../libxmu6_2%3a1.1.3-0ubuntu1_amd64.deb ...
Unpacking libxmu6:amd64 (2:1.1.3-0ubuntu1) ...
Setting up libxmu6:amd64 (2:1.1.3-0ubuntu1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.9) ...
root@ubuntu-focal:/media/cdrom#

If we now try to compile the GuestAdditions again:

root@ubuntu-focal:/media/cdrom# ./VBoxLinuxAdditions.run 
Verifying archive integrity... All good.
Uncompressing VirtualBox 6.1.38 Guest Additions for Linux........
VirtualBox Guest Additions installer

Removing installed version 6.1.38 of VirtualBox Guest Additions...
update-initramfs: Generating /boot/initrd.img-5.4.0-126-generic
Copying additional installer modules ...
Installing additional modules ...

VirtualBox Guest Additions: Starting.
VirtualBox Guest Additions: Building the VirtualBox Guest Additions kernel 
modules.  This may take a while.
VirtualBox Guest Additions: To build modules for other installed kernels, run
VirtualBox Guest Additions:   /sbin/rcvboxadd quicksetup <version>
VirtualBox Guest Additions: or
VirtualBox Guest Additions:   /sbin/rcvboxadd quicksetup all

VirtualBox Guest Additions: Building the modules for kernel 5.4.0-126-generic.
update-initramfs: Generating /boot/initrd.img-5.4.0-126-generic
VirtualBox Guest Additions: Running kernel modules will not be replaced until 
the system is restarted
root@ubuntu-focal:/media/cdrom#

That seems to have fixed those missing shared libraries now, and we have built the GuestAdditions without any errors.

The next thing to do is reboot the VM and make sure it starts up OK with the GuestAdditions installed.

.