VirtualBox with Linux Guest:
Fix Display Resolution and Bidirectional Clipboard

Posted:

Whenever I installed a Debian guest virtual machine on a laptop, I always had two annoying issues:

  1. The guest display was locked in at 1024x768, refusing to match the host's resolution of 1280x800 or 1920x1080.  The menu option “Auto-resize Guest Display” was disabled.
  2. The clipboard wouldn't communicate between the host and the guest, even though I had 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!  This solution will probably work for other builds of Linux (but I haven't verified this).

Boot up your Linux guest and open a terminal.  You will need root permissions, so either log in as root or be prepared to enter your superuser password.

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

> 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.

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.

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:

> cat /etc/mtab

Sample output:

rootfs / rootfs rw 0 0
sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0
proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
udev /dev devtmpfs rw,relatime,size=10240k,nr_inodes=63065,mode=755 0 0
devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0
tmpfs /run tmpfs rw,nosuid,noexec,relatime,size=51480k,mode=755 0 0
/dev/disk/by-uuid/8669a806-d2f9-4e02-bfb7-54c17503f2d9 / ext4 rw,relatime,errors=remount-ro,user_xattr,barrier=1,data=ordered 0 0
tmpfs /run/lock tmpfs rw,nosuid,nodev,noexec,relatime,size=5120k 0 0
tmpfs /run/shm tmpfs rw,nosuid,nodev,noexec,relatime,size=181380k 0 0
rpc_pipefs /var/lib/nfs/rpc_pipefs rpc_pipefs rw,relatime 0 0
binfmt_misc /proc/sys/fs/binfmt_misc binfmt_misc rw,nosuid,nodev,noexec,relatime 0 0
/dev/sr0 /media/cdrom0 iso9660 ro,nosuid,nodev,noexec,relatime 0 0

Or to make that easier to read by pulling out just the line we're looking for, use

> cat /etc/mtab|grep 'cdrom'

which in this case should yield

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

If you don't see a line containing “cdrom,” it may be because the system for some bizarre reason won't actually mount the CD image until you actively access it.

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

In this example, the line is:

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

...and our target directory is /media/cdrom0.  Switch to the CD-ROM's directory:

> cd /media/cdrom0

Then 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:

> sh VBoxLinuxAdditions.run

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 “Insert Guest Additions CD image...”)

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

Verifying archive integrity... All good.
Uncompressing VirtualBox 5.1.20 Guest Additions for Linux...........
VirtualBox Guest Additions installer
You appear to have a version of the VirtualBox Guest Additions
on your system which was installed from a different source or using a
different type of installer.  If you installed it from a package from your
Linux distribution or if it is a default part of the system then we strongly
recommend that you cancel this installation and remove it properly before
installing this version.  If this is simply an older or a damaged
installation you may safely proceed.

Do you wish to continue anyway? [yes or no]

Enter

> yes

At this point there's a good chance you'll encounter some errors preventing you from proceeding.  I'll address the ones I've experienced[1].

On an old Sony Vaio running Windows Vista 32-bit, I got the following error.  (If you don't have this problem, skip ahead.)

Copying additional installer modules ...
Installing additional modules ...
vboxadd.sh: Starting the VirtualBox Guest Additions.
Failed to set up service vboxadd, please check the log file
/var/log/VBoxGuestAdditions.log for details.
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

View the log file:

> tail /var/log/VBoxGuestAdditions.log

You might see something like this:

vboxadd-x11: unrecognized service
vboxvfs: unrecognized service
vboxadd-timesync: unrecognized service
vboxadd-service: unrecognized service
vboxadd: unrecognized service
vboxadd.sh: failed: Configuration file /var/lib/VBoxGuestAdditions/config not found.
vboxadd.sh: failed: Look at /var/log/vboxadd-install.log to find out what went wrong.
vboxadd.sh: failed: Look at /var/log/vboxadd-install.log to find out what went wrong.
vboxadd.sh: failed: modprobe vboxguest failed.

...telling you to look at yet another log file:

> tail /var/log/vboxadd-install.log

which might look like this:

/tmp/vbox.0/Makefile.include.header:112: *** Error: unable to find the sources of your current Linux kernel. Specify KERN_DIR= and run Make again.  Stop.
Creating user for the Guest Additions.
Creating udev rule for the Guest Additions kernel module.

It looks like we're supposed to manually define KERN_DIR, but we're not, so don't worry about that.  The problem here is that the script is unable to find information about your build, somewhere like /lib/modules/[kernel_version_number]/build, which the script should automatically use to define KERN_DIR, which in my case was /lib/modules/3.2.0-4-486/build.  My problem was that /lib/modules/3.2.0-4-486/build did not exist.  You can confirm that this is your problem too by viewing /lib/modules/[kernel_version_number] and seeing whether or not it contains “build”:

> ls -la /lib/modules/`uname -r`/build

If the directory doesn't exist, you'll see something like this:

ls: cannot access /lib/modules/3.2.0-4-486/build: No such file or directory

To resolve this issue, enter

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

To confirm this was successful, view /lib/modules/[kernel_version_number]/build:

> ls -la /lib/modules/`uname -r`/build

You will successfully see a directory listing.

Installing the following additional packages has also been recommended for this situation, but you might get away with skipping this step:

> apt-get install dkms build-essential

Run VBoxLinuxAdditions.run again:

> sh VBoxLinuxAdditions.run

Program output:

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:

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

On an HP Pavilian running Windows 10 64-bit, VBoxLinuxAdditions.run gave me this message telling me to install additional packages.  (If you don't have this issue, you can skip ahead.)

This system is currently not set up to build kernel modules.
Please install the gcc make perl packages from your distribution.
Please install the Linux kernel "header" files matching the current kernel
for adding new hardware support to the system.
The distribution packages containing the headers are probably:
    linux-headers-amd64 linux-headers-4.9.0-4-amd64

Install these missing packages:

> apt-get install linux-headers-`uname -r`
> apt-get install build-essential

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

  1. I originally wrote this article to resolve issues I encountered on a Sony Vaio, then updated it (2018.01.21) to resolve issues I experienced with an HP Pavilion.