VirtualBox with Linux Guest:
Fix Display Resolution and Bidirectional Clipboard

Posted:

The Issue

When I install a VirtualBox Linux guest virtual machine on a Windows host, I often have two annoying issues:

  1. The guest display is locked in at a lower resolution, refusing to match the host's higher resolution.  The menu option “Auto-resize Guest Display” is disabled.
  2. The clipboard won't communicate between the host and the guest, even though I have enabled the bidirectional clipboard.

I set out initially to solve the display issue, and after solving that I discovered that it also solved the clipboard issue!  I came up with this solution originally for Debian systems, but have also used it with Ubuntu, so it will probably work for most varieties of Linux.

Update The System

Boot up your Linux guest and open a terminal.

Before proceeding, make sure your system is up-to-date:

> sudo apt-get update

If any updates were made, it would be a good idea to reboot the guest before continuing on to the next step, in order to make sure the entire system has refreshed, otherwise you might get some temporary errors that will send you on a wild goose chase.

The Guest Additions CD

From the VirtualBox Menu of your virtual machine's window, select “Devices” | “Insert Guest Additions CD image...”  This should mount the CD image.  If an autorun prompt appears, close it, because we're doing this from the command line.  (If you get a error message stating, “Unable to insert the virtual optical disk,” most likely you already mounted the Guest Additions CD image and can ignore the error.)

Change to the directory of the mount.  If you're not sure where that is, enter the following to view a list of all mounted devices, filtered for optical drives:

> cat /etc/mtab|grep 'media'

which might yield something like

/dev/sr0 /media/cdrom0 iso9660 ro,nosuid,nodev,noexec,relatime 0 0

or

/dev/sr0 /media/username/VBox_GAs_6.1.22 iso9660 ro,nosuid,nodev,noexec,relatime...

The path beginning with /media/ is the CD image.  If you don't see a line like that, or if you do but the location appears to be empty, it may be because the system for some bizarre reason won't actually mount the CD image until you actively access it by other means.

Access the CD image via the file explorer (double-click its icon on the desktop if there is one) and then again try cat /etc/mtab|grep 'media'.  You should now be able to find a line containing “cdrom.”

If you're trying to install Guest Additions on a system that doesn't have a graphical interface, use the following to manually mount the disc image:

> sudo mkdir --p /media/cdrom
> sudo mount -t auto /dev/cdrom /media/cdrom/

Output may look something like

mount: block device /dev/sr0 is write-protected, mounting read-only

You should now be able to find the mounted CD-ROM via cat /etc/mtab|grep 'media'.

In one example, the line is:

/dev/sr0 /media/cdrom0 iso9660 ro,nosuid,nodev,noexec,relatime 0 0

...and our target directory is the second path listed on that line, which in this case is /media/cdrom0.  Switch to the CD-ROM's directory:

> cd /media/cdrom0

Alternatively, you may be able to open the disc in a graphical file browser, then right-click its contents and click on “Open in Termanl”.

Install the Guest Additions

List the directory contents:

> ls -la

and you should see something like this:

total 51403
dr-xr-xr-x 6 root root     2048 Apr 18 03:02 .
drwxr-xr-x 3 root root     4096 Sep 30  2014 ..
dr-xr-xr-x 2 root root     2048 Apr 18 03:02 32Bit
dr-xr-xr-x 2 root root     2048 Apr 18 03:02 64Bit
-r-xr-xr-x 1 root root      647 Aug 16  2016 AUTORUN.INF
-r-xr-xr-x 1 root root     6381 Apr 18 03:52 autorun.sh
dr-xr-xr-x 2 root root     2048 Apr 18 03:02 cert
dr-xr-xr-x 2 root root     4096 Apr 18 03:02 OS2
-r-xr-xr-x 1 root root     4824 Apr 18 03:52 runasroot.sh
-r-xr-xr-x 1 root root  8129998 Apr 18 03:53 VBoxLinuxAdditions.run
-r-xr-xr-x 1 root root 17771008 Apr 18 03:54 VBoxSolarisAdditions.pkg
-r-xr-xr-x 1 root root 16399216 Apr 18 04:01 VBoxWindowsAdditions-amd64.exe
-r-xr-xr-x 1 root root   268496 Apr 18 03:53 VBoxWindowsAdditions.exe
-r-xr-xr-x 1 root root 10037640 Apr 18 03:54 VBoxWindowsAdditions-x86.exe

Run VBoxLinuxAdditions.run:

> sudo sh VBoxLinuxAdditions.run

If all goes well, you should see something like the following:

Verifying archive integrity... All good.
Uncompressing VirtualBox 6.1.34 Guest Additions for Linux........
Virtualbox Guest Additions installer
Copying additional installer 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.10.0.15-amd64.
update-initramfs: Generating /boot/initrd.img-5.10.0-15-amd64
VirtualBox Guest Additions: Running kernel modules will not be replaced until the system is restarted.

After VBoxLinuxAdditions.run successfully finishes running, shut down and restart your guest.  After you log into your profile on the guest, its display should now match the host's, and the bidirectional clipboard should work.  (You may have to restart twice for this to work.)

If you encounter any errors, keep reading, and I'll address the ones I've experienced[1].  If you saw “Kernel headers not found for target kernel,” that might not have prevented you from installing the Guest Additions, but if it bothers you, I also cover that below.

Note that each time you update VirtualBox, it would be wise to also update, and maybe even upgrade, your guest's packages.  In any case, you should run VBoxLinuxAdditions.run again.  (In one case I experienced, upgrading VirtualBox caused the titular issues of this article to manifest, and I couldn't get VBoxLinuxAdditions.run to work until after I upgraded the guest.)

Error In MD5 Checksums

If you see something like “Verifying archive integrity...Error in MD5 checksums” it might just be that the system hasn't been refreshed after the update.  Make sure that you've updated everything and let the updates complete, remove any mounted discs from the virtual optical disk, reboot the guest, then try again.  (Start over at “The Guest Additions CD.”)  If running VBoxLinuxAdditions.run literally does nothing (quits without any messages[2]) it could be that there's an issue with the disc mount.  Try shutting down you guest completely and exiting the VirtualBox launcher and any other VirtualBox guests.  (Maybe even reboot your host.)  Restart your guest and try again.

Kernel Headers Not Found

By most issues I've encountered there has also been the message “Kernel headers not found for target kenerl” or “This system is currently not set up to build kernel modules,” or the advice to view a series of error logs that lead to something like “unable to find the sources of your current Linux kernel.”  This can be resolved by running the following:

> sudo apt-get install linux-headers-`uname -r`

Installing the following additional packages has also been recommended for these situations.  This might not always be necessary for the current issue, but this has actually been proven to also solve other unrelated issues that may crop up later when installing other services:

> sudo apt-get install build-essential gcc make perl dkms

If you have not already successfully installed the Guest Additions, start over at “Install The Guest Additions.”

Known Issue With Mesa

On an old Sony Vaio running Windows Vista 32-bit, I got the following error:

Verifying archive integrity... All good.
Uncompressing VirtualBox 5.1.20 Guest Additions for Linux...........
VirtualBox Guest Additions installer
Removing installed version 5.1.20 of VirtualBox Guest Additions...
vboxadd.sh: Stopping VirtualBox Additions.
Copying additional installer modules ...
Installing additional modules ...
vboxadd.sh: Starting the VirtualBox Guest Additions.
This system appears to be running a version of Mesa with a known problem
which will prevent VirtualBox 3D pass-through from working.  See
  https://bugs.freedesktop.org/show_bug.cgi?id=26663
The following, run as root should fix this, though you will have to run it
again if the system version of Mesa is updated:
  strip -R .note.ABI-tag /usr/lib/i386-linux-gnu/libGL.so.1
  ldconfig

Note that in my case, I am advised regarding a known issue with Mesa.  If you see something like that, run the commands advised by program output, which in my case were:

> sudo strip -R .note.ABI-tag /usr/lib/i386-linux-gnu/libGL.so.1
> sudo ldconfig

Start over at “Install The Guest Additions.”

Bidirectional Clipboard Still Won't Work

If you successfully installed the Guest Additions, but the bidirectional cipboard still won't work, run VBoxLinuxAdditions.run again and restart.

Screen Still Won't Maximize

One time I created a new virtual machine and I couldn't get the guest to increase its resolution until I told the emulator to go fullscreen (go emulator menu View -> Full-screen Mode or press Host+F).

Screen Goes Blank

Once when installing a guest running Ubuntu 20, after the first time I maximized the window, every time I logged in the screen would go blank and stay that way.  (I could still reach the terminal by pressing Ctrl+Alt+F5, but the desktop never loaded.)  This seems to be a known issue no issue with no official fix at the time of this writing (2021.05.14), but I found that resizing the emulator window by slowly dragging its corner before maximizing seemed to somewhat bypass this issue.

  1. I originally wrote this article to resolve issues I encountered on a Sony Vaio laptop, then updated it to resolve issues I experienced with an HP Pavilion (2018.01.21) laptop, and with a Windows 10 desktop (2020.02.29).  I've since continued to update this article as I still use it as a guide when setting up new development environments.
  2. I had this issue when upgrading VirtualBox from 5.2 to 6.0.