VirtualBox虚拟机CentOS安装增强功能Guest Additions

时间:2024-02-20 09:28:36

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.