1. 程式人生 > >VirtualBox虛擬機器CentOS安裝增強功能Guest Additions

VirtualBox虛擬機器CentOS安裝增強功能Guest Additions

沒有安裝Virtual Box增強功能Guest Additions的虛擬機器操作很不方便,比如:

  • 虛擬機器的解析度最大隻有1024x768;
  • 滑鼠被虛擬機器捕獲後要按右Ctrl鍵才能釋放,否則只能再虛擬機器裡移動;
  • 剪貼簿不能共享

安裝Guest Additions可以解決上述所有問題。
以CentOS 7.3虛擬機器為例,如果安裝CentOS時沒有選擇Development Tools組,需要先安裝kernel-devel和gcc
$ sudo yum install -y kernel-devel gcc

通過VirtualBox的 裝置 | 安裝增強功能... 選單可以載入VBoxGuestAdditions.iso,可以直接點選Run安裝或者執行
$ cd /run/media/centos/VBOXADDITIONS_5.1.22_115126


$ sudo sh ./VBoxLinuxAdditions.run

有時安裝會出錯

Verifying archive integrity... All good.
Uncompressing VirtualBox 5.1.22 Guest Additions for Linux...........
VirtualBox Guest Additions installer
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.

查詢log發現是找不到和Linux核心匹配的原始檔
$ cat /var/log/VBoxGuestAdditions.log

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.

$ cat /var/log/vboxadd-install.log

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

查詢kernel-devel版本是3.10.0-514.26.2.el7.x86_64,而Linux核心版本是3.10.0-514.el7.x86_64,確實不一致。如果安裝CentOS時選擇了Development Tools組,kernel-devel版本和Linux核心版本是匹配的,則不會出現這個問題。
$ rpm -qa | grep kernel-devel

kernel-devel-3.10.0-514.26.2.el7.x86_64

$ uname -r

3.10.0-514.el7.x86_64

可以使用下面兩個命令中的一個安裝和Linux核心版本匹配的kernel-devel
$ sudo yum install -y "kernel-devel-uname-r == $(uname -r)"
$ sudo yum install -y kernel-devel-3.10.0-514.el7
然後再次安裝Guest Additions成功
$ cd /run/media/centos/VBOXADDITIONS_5.1.22_115126
$ sudo sh ./VBoxLinuxAdditions.run

Verifying archive integrity... All good.
Uncompressing VirtualBox 5.1.22 Guest Additions for Linux...........
VirtualBox Guest Additions installer
Removing installed version 5.1.22 of VirtualBox Guest Additions...
Copying additional installer modules ...
Installing additional modules ...
vboxadd.sh: Starting the VirtualBox Guest Additions.