How do I install VirtualBox guest additions for CentOS 7?

All we need is an easy explanation of the problem, so here it is.

How? It gives an error.

[[email protected] mnt]$ sudo ./VBoxLinuxAdditions.run
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.1.16 Guest Additions for Linux...........
VirtualBox Guest Additions installer
Removing installed version 5.1.16 of VirtualBox Guest Additions...
Copying additional installer modules ...
Installing additional modules ...
vboxadd.sh: Building Guest Additions kernel modules.
Failed to set up service vboxadd, please check the log file
/var/log/VBoxGuestAdditions.log for details.

[[email protected] mnt]$ cat /var/log/VBoxGuestAdditions.log
vboxadd.sh: failed: Look at /var/log/vboxadd-install.log to find out what went wrong.
vboxadd.sh: failed: Please check that you have gcc, make, the header files for your Linux kernel and possibly perl installed..

[[email protected] mnt]$ cat /var/log/vboxadd-install.log
/tmp/vbox.0/Makefile.include.header:97: *** Error: unable to find the sources of your current Linux kernel. Specify KERN_DIR=<directory> and run Make again.  Stop.
Creating user for the Guest Additions.
Creating udev rule for the Guest Additions kernel module.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

vagrant init centos/7
vagrant up; vagrant halt
  • Add a CDROM to the virtual machine and select the VBoxGuestAdditions.iso file to insert in the drive.

virtual box guest additions

  • Manually add a shared folder in VirtualBox with name vagrant and path to the directory of your Vagrantfile. Vagrant doesn’t set up /vagrant properly.

virtualbox shared folder

  • Edit Vagrantfile and add this to the bottom to provision the shared folder each time you start.

    config.vm.provision "shell", run: "always", inline: <<-SHELL
        mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` vagrant /vagrant
    SHELL
    
  • Continue with

    vagrant up
    vagrant ssh
    sudo yum update
    sudo yum install kernel-devel gcc
    exit
    vagrant halt; vagrant up; vagrant ssh
    export KERN_DIR=/usr/src/kernels/`uname -r`
    sudo mount /dev/sr0/ /mnt
    cd /mnt
    sudo ./VBoxLinuxAdditions.run
    

Method 2

$ cat /var/log/vboxadd-install.log
/tmp/vbox.0/Makefile.include.header:97: *** Error: unable to find the \\
sources of your current Linux kernel. \\
Specify KERN_DIR=<directory> and run Make again.  Stop.

Your install.log file is very clear: you need to install the headers,

sudo yum update
sudo yum install kernel-headers kernel-devel

and then you may re-install VirtualBox.

Method 3

I needed to install kernel-header and kernel-devel. plus gcc to install virtualbox guest additions.

    sudo yum update
    sudo yum install kernel-headers kernel-devel
    sudo yum install gcc*

Method 4

VirtualBox Guest Additions helper script for CentOS 7

I wrote this little helper script that does this to a fresh VM and corrects the error messsage by taking advantage of the fact that when the run script fails, it leaves a few helper binaries intact:

VBoxGuestInstall.sh

#!/bin/bash

# reference: https://www.if-not-true-then-false.com/2010/install-virtualbox-guest-additions-on-fedora-centos-red-hat-rhel/
# though this document is invalid, it does provide the dependencies and the epel repository needed.
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install gcc kms make bzip2 perl

# get rid of the currently running kernel packages and install the new ones...
yum remove kernel*`uname -r`
yum install kernel*

NEW_UNAME=$(ls /usr/src/kernels/ | grep 'el7.x86_64')
# this command is to ensure the kernel's .config reaches the source directory the 
# modules will be building against.
cp -f /boot/config-$NEW_UNAME /usr/src/kernels/$NEW_UNAME/.config

mkdir /media/VirtualBoxGuestAdditions
mount /dev/sr0 /media/VirtualBoxGuestAdditions

# this will fail, but your helper binaries will be installed
/media/VirtualBoxGuestAdditions/VBoxLinuxAdditions.run
# so we will now direct the helper binary to build for the kernel we want instead of
# the running one invalidly returned by `uname -r` inside of VirtualBox's .run binary.
/sbin/rcvboxadd quicksetup $NEW_UNAME

Moving the script

It targets the kernel you want instead of the running one, after installing all of the dependencies. Run a simple http server on localhost on your host and you can grab it over the network adapter inside of the VM. Nifty little trick before you get the guest additions installed. I use bitnami because I just had it lying around, but simplehttpd or something very similar will work, anything that can serve files:

cd ~/
wget 'http://host_machine_ip_goes_here:80/VBoxSetup.sh'
chmod +x ./VBoxSetup.sh
sudo ./VBoxSetup.sh

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply