Installing Virtualbox 4.1 on CentOS 6.2

January 9, 2012

This article will show you how to install VirtualBox 4.1 on a fresh installation of CentOS 6.2 Linux. In the tutorial below VirtualBox is installed immediately after having installed CentOS 6.2 (desktop setup) and rebooting it after having applied all the latest OS updates.

The following steps will need to be done with the root account.


Installation

Change to the /etc/yum.repos.d/ directory and download the VirtualBox repo file into your yum repository. With this you'll be able to install VirtualBox through yum and updates for VirtualBox will also be included whenever you run the yum update command:

[root@Linux ]# cd /etc/yum.repos.d/
[root@Linux ]# wget http://download.virtualbox.org/virtualbox/rpm/rhel/virtualbox.repo

Take a look at the contents of the downloaded repo file to confirm that gpgcheck is enabled (gpgcheck=1) and to verify the location of the gpg key:

[root@Linux ]# cat virtualbox.repo
[virtualbox]
name=RHEL/CentOS-$releasever / $basearch - VirtualBox
baseurl=http://download.virtualbox.org/virtualbox/rpm/rhel/$releasever/$basearch
enabled=1
gpgcheck=1
gpgkey=http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc

The following steps are optional: Verify the fingerprint of the gpg key in order confirm that the key downloaded belongs to virtualbox.org. In the command below the character surrounded by a dashes before the pipe is an uppercase letter "O" (not zero):

[root@Linux ]# wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | gpg --quiet --with-fingerprint
pub  1024D/98AB5139 2010-05-18 Oracle Corporation (VirtualBox archive signing key) <info@virtualbox.org>
      Key fingerprint = 7B0F AB3A 13B9 0743 5925  D9C9 5442 2A4B 98AB 5139
sub  2048g/281DDC4B 2010-05-18

Go to http://pgp.mit.edu/ and in the search string enter virtualbox.org, and checkmark "Show PGP fingerprints for keys". Confirm that the key fingerprint matches the one above for the addresses info@virtualbox.org:

Type bits/keyID     Date       User ID

pub  1024D/98AB5139 2010-05-18 Oracle Corporation (VirtualBox archive signing key) <info@virtualbox.org>
	 Fingerprint=7B0F AB3A 13B9 0743 5925  D9C9 5442 2A4B 98AB 5139 

pub  1024D/6DFBCBAE 2008-07-14 Sun Microsystems, Inc. (xVM VirtualBox archive signing key) <info@virtualbox.org>
	 Fingerprint=AF45 1228 01DA D613 29EF  9570 DCF9 F87B 6DFB CBAE 

As you can see there are two keys found for info@virtualbox.org with the most recent key being the one that matches. Also notice that the key ID (98AB5139) shown in the output from pgp.mit.edu matches the key ID shown in the previous command.

Another optional step is to review the information of the VirtualBox package before proceeding with the installation (this will confirm the minor version):

[root@Linux ]# yum info VirtualBox-4.1
Available Packages
Name        : VirtualBox-4.1
Arch        : i686
Version     : 4.1.8_75467_rhel6
Release     : 1
Size        : 58 M
Repo        : virtualbox
Summary     : Oracle VM VirtualBox
URL         : http://www.virtualbox.org/
License     : GPLv2
Description : VirtualBox is a powerful PC virtualization solution allowing
            : you to run a wide range of PC operating systems on your Linux
            : system. This includes Windows, Linux, FreeBSD, DOS, OpenBSD
            : and others. VirtualBox comes with a broad feature set and
            : excellent performance, making it the premier virtualization
            : software solution on the market.

Proceed with installing VirtualBox (the command below is case sensitive). When prompted about the GPG key enter Y as long as the key ID matches the one above:

[root@Linux ]# yum install VirtualBox-4.1
Setting up Install Process
.
<snip>
.
Downloading Packages:
VirtualBox-4.1-4.1.8_75467_rhel6-1.i686.rpm              |  58 MB     01:08     
warning: rpmts_HdrFromFdno: Header V4 DSA/SHA1 Signature, key ID 98ab5139: NOKEY
Retrieving key from http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc
Importing GPG key 0x98AB5139:
 Userid: "Oracle Corporation (VirtualBox archive signing key) <info@virtualbox.org>"
 From  : http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc
Is this ok [y/N]: y
.
<snip>
.
Creating group 'vboxusers'. VM users must be member of that group!

No precompiled module for this kernel found -- trying to build one. Messages
emitted during module compilation will be logged to /var/log/vbox-install.log.

Stopping VirtualBox kernel modules [  OK  ]
Uninstalling old VirtualBox DKMS kernel modules [  OK  ]
Trying to register the VirtualBox kernel modules using DKMS [FAILED]
  (Failed, trying without DKMS)
Recompiling VirtualBox kernel modules [FAILED]
  (Look at /var/log/vbox-install.log to find out what went wrong)

Installed:
  VirtualBox-4.1.i686 0:4.1.8_75467_rhel6-1                                     

Complete!

(note that the second failed entry above shows that a problem which you might not experience on your system did occur for us. We'll address this shortly)

Finally don't forget to add your user account to the newly created vboxusers group, where "username" is the Linux user that you are adding to this group:

[root@Linux ]# /usr/sbin/usermod -a -G vboxusers username

Problems which some may encounter

Although this problem may not occur for your environment, in ours we can see from the output above that something failed and we are advised to examine /var/log/vbox-install.log to see what went wrong. In our case it tells us the following:

"unable to find the sources of your current Linux kernel. Specify KERN_DIR=<directory> and run Make again."

We were able to fix the issue by completing the following additional steps (if you don't experience this problem you can skip to the end).

Install the following three packages (kernel-headers, kernel-devel, gcc):

[root@Linux ]# yum install kernel-headers kernel-devel gcc

Your system will install the packages above if they aren't installed already, plus the package dependencies. Next confirm the version of your current kernel release:

[root@Linux ]# uname -r
2.6.32-220.2.1.el6.i686

Check /usr/src/kernels/ to confirm that a directory for the version of your current kernel release exists (this directory should exist after the installation of the packages above):

ls /usr/src/kernels/
2.6.32-220.2.1.el6.i686

Above we see a match for "2.6.32-220.2.1.el6.i686". Specify and export the KERN_DIR parameter making sure to match the release version with whichever one is current for your system:

[root@Linux ]# KERN_DIR=/usr/src/kernels/2.6.32-220.2.1.el6.i686
[root@Linux ]# export KERN_DIR

Echo KERN_DIR to confirm that you've set it up properly:

[root@Linux ]# echo $KERN_DIR
/usr/src/kernels/2.6.32-220.2.1.el6.i686

Finally re-run vboxdrv setup. This step may take a while to complete:

[root@Linux ]# /etc/init.d/vboxdrv setup
Stopping VirtualBox kernel modules                         [  OK  ]
Uninstalling old VirtualBox DKMS kernel modules            [  OK  ]
Removing old VirtualBox pci kernel module                  [  OK  ]
Removing old VirtualBox netadp kernel module               [  OK  ]
Removing old VirtualBox netflt kernel module               [  OK  ]
Removing old VirtualBox kernel module                      [  OK  ]
Trying to register the VirtualBox kernel modules using DKMS[FAILED]
  (Failed, trying without DKMS)
Recompiling VirtualBox kernel modules                      [  OK  ]
Starting VirtualBox kernel modules                         [  OK  ]

(the FAILED entry above for DKMS does not matter since recompiling the VirtualBox kernel modules succeeded).

Lastly don't forget to add your user account to the vboxusers group if you haven't done so already:

[root@Linux ]# /usr/sbin/usermod -a -G vboxusers username

Success

Through the CentOS GUI you can start VirtualBox by clicking on Applications | System Tools | Oracle VM VirtualBox. If the first time when creating a new guest OS you get an error saying that it failed to access the USB subsystem, as a test try to access the /dev/vboxusb directory. If you get a message saying permission denied yet you are in the vboxusers group and the permission for /dev/vboxusb is correct (drwxr-x--- with ownership root:vboxusers), open a terminal window and enter the "id" command. If in the output you don't see the group vboxusers appear, log out and log back into CentOS for the group membership changes to take effect.

Read the security guide section of the VirtualBox documentation, notably the risk of enabling 3D graphics via the guest additions if you are going to be using VirtualBox for testing malware:

"Untrusted guest systems should not be allowed to use VirtualBox's 3D acceleration features, just as untrusted host software should not be allowed to use 3D acceleration. Drivers for 3D hardware are generally too complex to be made properly secure and any software which is allowed to access them may be able to compromise the operating system running them. In addition, enabling 3D acceleration gives the guest direct access to a large body of additional program code in the VirtualBox host process which it might conceivably be able to use to crash the virtual machine."

In CentOS 5 you could perform RPM rollbacks in the event that an update caused a conflict with VirtualBox, by editing /etc/yum.conf and adding the line tsflags=repackage to it, and also editing /etc/rpm/macros and adding %_repackage_all_erasures 1 in it. In CentOS 6 this option no longer works. Instead you must use the "yum history" commands to do RPM rollbacks. The yum history databases are normally found in /var/lib/yum/history/.

Although this article isn't meant to be a tutorial on how to use VirtualBox, the screen capture below shows what you need to do in order to use an ISO file instead of a CD or DVD-ROM for building a new guest operating system.

If you wish to control VirtualBox through the command line, use the extensive vboxmanage command (simply enter the command with no parameters to see its options, although you'll likely need to skim through chapter 8 of the VirtualBox user manual). If you wish to remotely control VirtualBox through a GUI, some options include setting up vncserver or nx (preferably tunnelled over ssh) on CentOS. There are also some third party software made specifically for providing a remote GUI for VirtualBox (remotebox, vboxremote, phpvirtualbox -- note that we cannot vouch for any of this software including whether or not they are kept up to date with the latest version of VirtualBox). Lastly you can use the vboxheadless command in order to remotely display the VMs over RDP.

Lastly note that whenever you install kernel updates you'll need to rerun the command /etc/init.d/vboxdrv setup