Linux 常用工具之 VirtualBox

Ubuntu 安装 VirtualBox

repo 方式安装 VirtualBox 5.1(推荐)

参考 VirtualBox 5.1.14 for Linux

1
2
3
4
5
6
7
8
9
Debian-based Linux distributions
Add the following line to your /etc/apt/sources.list:
deb http://download.virtualbox.org/virtualbox/debian xenial contrib
According to your distribution, replace 'xenial' by 'vivid', 'utopic', 'trusty', 'raring', 'quantal', 'precise', 'lucid', 'jessie', 'wheezy', or 'squeeze'.
To install VirtualBox, do
sudo apt-get update
sudo apt-get install virtualbox-5.1

安装过程记录如下:

1
2
3
4
5
6
7
8
9
huzhifeng@Ubuntu16041:~$ sudo sh -c "echo 'deb http://download.virtualbox.org/virtualbox/debian xenial contrib' > /etc/apt/sources.list.d/virtualbox.list"
huzhifeng@Ubuntu16041:~$ cat /etc/apt/sources.list.d/virtualbox.list
deb http://download.virtualbox.org/virtualbox/debian xenial contrib
huzhifeng@Ubuntu16041:~$ wget -q https://www.virtualbox.org/download/oracle_vbox_2016.asc -O- | sudo apt-key add -
OK
huzhifeng@Ubuntu16041:~$ wget -q https://www.virtualbox.org/download/oracle_vbox.asc -O- | sudo apt-key add -
OK
huzhifeng@Ubuntu16041:~$ sudo apt-get update
huzhifeng@Ubuntu16041:~$ sudo apt-get install virtualbox-5.1

RPM 方式安装 VirtualBox 5.1

下载 DEB 安装包以及 extpack

VirtualBox 5.1.14 for Linux

1
2
3
4
5
6
huzhifeng@Ubuntu16041:~$ cd Tools/VirtualBox/
huzhifeng@Ubuntu16041:~/Tools/VirtualBox$ wget http://download.virtualbox.org/virtualbox/5.1.12/virtualbox-5.1_5.1.12-112440~Ubuntu~xenial_amd64.deb
huzhifeng@Ubuntu16041:~/Tools/VirtualBox$ wget http://download.virtualbox.org/virtualbox/5.1.12/Oracle_VM_VirtualBox_Extension_Pack-5.1.12-112440.vbox-extpack
huzhifeng@Ubuntu16041:~/Tools/VirtualBox$ md5sum virtualbox-5.1_5.1.12-112440-Ubuntu-xenial_amd64.deb Oracle_VM_VirtualBox_Extension_Pack-5.1.12-112440.vbox-extpack
f058b0a75bbd9f5085eae059b3ca53c0 virtualbox-5.1_5.1.12-112440-Ubuntu-xenial_amd64.deb
0402b1795e7b0a84da68974fd880cd3b Oracle_VM_VirtualBox_Extension_Pack-5.1.12-112440.vbox-extpack

安装 DEB 包

1
huzhifeng@Ubuntu16041:~/Tools/VirtualBox$ sudo dpkg -i virtualbox-5.1_5.1.12-112440-Ubuntu-xenial_amd64.deb

安装 extpack

双击”Oracle_VM_VirtualBox_Extension_Pack-5.1.12-112440.vbox-extpack”自动安装

创建桌面快捷图标

1
2
3
4
5
6
7
8
9
10
11
12
13
14
huzhifeng@Ubuntu16041:~/Tools/VirtualBox$ dpkg-query --listfiles virtualbox-5.1 | grep "\.desktop"
/usr/share/applications/virtualbox.desktop
huzhifeng@Ubuntu16041:~/Tools/VirtualBox$ cp /usr/share/applications/virtualbox.desktop ~/Desktop/
huzhifeng@Ubuntu16041:~/Tools/VirtualBox$ vim ~/Desktop/virtualbox.desktop
huzhifeng@Ubuntu16041:~/Tools/VirtualBox$ chmod a+x ~/Desktop/virtualbox.desktop
huzhifeng@Ubuntu16041:~/Tools/VirtualBox$ cat ~/Desktop/virtualbox.desktop
[Desktop Entry]
Version=1.0
Name=Oracle VM VirtualBox
Type=Application
Exec=VirtualBox %U
Icon=virtualbox
Terminal=false
huzhifeng@Ubuntu16041:~/Tools/VirtualBox$

CentOS 安装 VirtualBox

可以创建 repo 进行安装,也可以直接下载 RPM 安装包

repo 方式安装 VirtualBox-5.1

参考 VirtualBox 5.1.14 for Linux

1
2
3
Users of Oracle Linux 5, 6 and 7 can use the public yum repository and enable the el5_addons (OEL5), the ol6_addons (OL6) or the ol7_addons (OL7). If a public-yum repo file already exists in /etc/yum.repos.d, do not overwrite it but just enable the addons! After that, do
yum install VirtualBox-5.1

安装过程记录如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[root@CentOS72 ~]# cd /etc/yum.repos.d/
[root@CentOS72 yum.repos.d]# wget http://download.virtualbox.org/virtualbox/rpm/el/virtualbox.repo
[root@CentOS72 yum.repos.d]# cat virtualbox.repo
[virtualbox]
name=Oracle Linux / RHEL / CentOS-$releasever / $basearch - VirtualBox
baseurl=http://download.virtualbox.org/virtualbox/rpm/el/$releasever/$basearch
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://www.virtualbox.org/download/oracle_vbox.asc
[root@CentOS72 yum.repos.d]# yum search virtualbox
VirtualBox-4.3.x86_64 : Oracle VM VirtualBox
VirtualBox-5.0.x86_64 : Oracle VM VirtualBox
VirtualBox-5.1.x86_64 : Oracle VM VirtualBox
[root@CentOS72 yum.repos.d]# yum install -y VirtualBox-5.1
Running transaction
Installing : VirtualBox-5.1-5.1.14_112924_el7-1.x86_64 1/1
Creating group 'vboxusers'. VM users must be member of that group!
vboxdrv.sh: failed: Look at /var/log/vbox-install.log to find out what went wrong.
This system is not currently set up to build kernel modules (system extensions).
Running the following commands should set the system up correctly:
yum install gcc make
yum install kernel-devel-3.10.0-514.6.1.el7.x86_64
(The last command may fail if your system is not fully updated.)
yum install kernel-devel
There were problems setting up VirtualBox. To re-start the set-up process, run
/sbin/vboxconfig
as root.
Verifying : VirtualBox-5.1-5.1.14_112924_el7-1.x86_64 1/1
Installed:
VirtualBox-5.1.x86_64 0:5.1.14_112924_el7-1
Complete!
[root@CentOS72 yum.repos.d]#

注意:虽然安装成功了,但是也有错误提示以及相应的修复方案,现在启动 virtualbox 会提示内核模块 vboxdrv 没有加载
[root@CentOS72 yum.repos.d]# virtualbox
WARNING: The vboxdrv kernel module is not loaded. Either there is no module
available for the current kernel (3.10.0-514.6.1.el7.x86_64) or it failed to
load. Please recompile the kernel module and install it by

  sudo /sbin/vboxconfig

You will not be able to start VMs until this problem is fixed.

[root@CentOS72 yum.repos.d]# cat /var/log/vbox-install.log
Makefile:185: * Error: unable to find the sources of your current Linux kernel. Specify KERN_DIR= and run Make again. Stop.
[root@CentOS72 yum.repos.d]#

1
根据前面提示中提供的修复方案,安装内核开发包,然后重新执行`/sbin/vboxconfig`即可

[root@CentOS72 yum.repos.d]# yum install gcc make kernel-devel
[root@CentOS72 yum.repos.d]# rpm -q gcc make kernel-devel
gcc-4.8.5-11.el7.x86_64
make-3.82-23.el7.x86_64
kernel-devel-3.10.0-514.6.1.el7.x86_64
kernel-devel-3.10.0-514.6.1.el7.x86_64
kernel-devel-3.10.0-514.6.2.el7.x86_64
[root@CentOS72 yum.repos.d]# uname -a
Linux CentOS72 3.10.0-514.6.1.el7.x86_64 #1 SMP Wed Jan 18 13:06:36 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@CentOS72 yum.repos.d]# /sbin/vboxconfig
vboxdrv.sh: Building VirtualBox kernel modules.
vboxdrv.sh: Starting VirtualBox services.
[root@CentOS72 yum.repos.d]#

1
2
3
### RPM 方式安装 VirtualBox-5.1
安装过程记录如下:

[huzhifeng@CentOS72 Downloads]$ wget http://download.virtualbox.org/virtualbox/5.1.14/VirtualBox-5.1-5.1.14_112924_el7-1.x86_64.rpm
[huzhifeng@CentOS72 Downloads]$ sudo rpm -Uvh VirtualBox-5.1-5.1.14_112924_el7-1.x86_64.rpm
warning: VirtualBox-5.1-5.1.14_112924_el7-1.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 98ab5139: NOKEY
error: Failed dependencies:
libSDL-1.2.so.0()(64bit) is needed by VirtualBox-5.1-5.1.14_112924_el7-1.x86_64
[huzhifeng@CentOS72 Downloads]$ yum provides libSDL-1.2.so.0
SDL-1.2.15-14.el7.i686 : A cross-platform multimedia library
Repo : base
Matched from:
Provides : libSDL-1.2.so.0
[huzhifeng@CentOS72 Downloads]$ sudo yum install SDL
[huzhifeng@CentOS72 Downloads]$ sudo rpm -Uvh VirtualBox-5.1-5.1.14_112924_el7-1.x86_64.rpm
warning: VirtualBox-5.1-5.1.14_112924_el7-1.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 98ab5139: NOKEY
Preparing… ################################# [100%]
Updating / installing…
1:VirtualBox-5.1-5.1.14_112924_el7-################################# [100%]

Creating group ‘vboxusers’. VM users must be member of that group!

vboxdrv.sh: failed: Look at /var/log/vbox-install.log to find out what went wrong.
This system is not currently set up to build kernel modules (system extensions).
Running the following commands should set the system up correctly:

yum install gcc make
yum install kernel-devel-3.10.0-514.6.1.el7.x86_64
(The last command may fail if your system is not fully updated.)
yum install kernel-devel

There were problems setting up VirtualBox. To re-start the set-up process, run
/sbin/vboxconfig
as root.
[huzhifeng@CentOS72 Downloads]$ sudo yum install gcc make kernel-devel
Installed:
gcc.x86_64 0:4.8.5-11.el7 kernel-devel.x86_64 0:3.10.0-514.6.1.el7

Dependency Installed:
cpp.x86_64 0:4.8.5-11.el7 glibc-devel.x86_64 0:2.17-157.el7_3.1 glibc-headers.x86_64 0:2.17-157.el7_3.1 kernel-headers.x86_64 0:3.10.0-514.6.1.el7 libmpc.x86_64 0:1.0.1-3.el7

Complete!
[huzhifeng@CentOS72 Downloads]$ sudo /sbin/vboxconfig
[huzhifeng@CentOS72 Downloads]$ sudo /sbin/vboxconfig
vboxdrv.sh: Building VirtualBox kernel modules.
vboxdrv.sh: Starting VirtualBox services.
[huzhifeng@CentOS72 Downloads]$ virtualbox

1
2
3
## Fedora 23 安装 VirtualBox
下载 RPM 安装包

[huzhifeng@Fedora23 Downloads]$ wget http://download.virtualbox.org/virtualbox/5.1.8/VirtualBox-5.1-5.1.8_111374_fedora22-1.x86_64.rpm
[huzhifeng@Fedora23 Downloads]$ wget http://download.virtualbox.org/virtualbox/5.1.8/Oracle_VM_VirtualBox_Extension_Pack-5.1.8-111374.vbox-extpack
[huzhifeng@Fedora23 Downloads]$ sudo dnf install qt5-qtx11extras compat-libvpx1 SDL
[huzhifeng@Fedora23 Downloads]$ sudo rpm -ivh VirtualBox-5.1-5.1.8_111374_fedora22-1.x86_64.rpm
warning: VirtualBox-5.1-5.1.8_111374_fedora22-1.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 98ab5139: NOKEY
Preparing… ################################# [100%]
Updating / installing…
1:VirtualBox-5.1-5.1.8_111374_fedor################################# [100%]

Creating group ‘vboxusers’. VM users must be member of that group!

vboxdrv.sh: failed: Look at /var/log/vbox-install.log to find out what went wrong.
This system is not currently set up to build kernel modules (system extensions).
Running the following commands should set the system up correctly:

yum install gcc make
yum install kernel-core-devel-4.7.7-100.fc23.x86_64
(The last command may fail if your system is not fully updated.)
yum install kernel-core-devel

There were problems setting up VirtualBox. To re-start the set-up process, run
/sbin/vboxconfig
as root.
[huzhifeng@Fedora23 Downloads]$ cat /var/log/vbox-install.log
Makefile:185: * Error: unable to find the sources of your current Linux kernel. Specify KERN_DIR= and run Make again. Stop.
[huzhifeng@Fedora23 Downloads]$ sudo dnf install gcc make kernel-core-devel
[huzhifeng@Fedora23 Downloads]$ sudo /sbin/vboxconfig
vboxdrv.sh: Building VirtualBox kernel modules.
vboxdrv.sh: Starting VirtualBox services.
[huzhifeng@Fedora23 Downloads]$

1
2
3
4
5
6
双击 "Oracle_VM_VirtualBox_Extension_Pack-5.1.8-111374.vbox-extpack" 安装扩展包
## VirtualBox 其它技巧
### 修改 VM 路径
默认的 VM 存储目录是`$HOME/VirtualBox VMs`
详见[10.1. Where VirtualBox stores its files](https://www.virtualbox.org/manual/ch10.html#vboxconfigdata)

huzhifeng@Ubuntu16041:~$ vboxmanage list systemproperties | grep “Default machine folder:”
Default machine folder: /home/huzhifeng/VirtualBox VMs
huzhifeng@Ubuntu16041:~$ sudo vboxmanage list systemproperties | grep “Default machine folder:”
Default machine folder: /root/VirtualBox VMs
huzhifeng@Ubuntu16041:~$

1
2
3
可以通过命令"vboxmanage setproperty machinefolder /path/to/directory/"进行修改(注:只影响当前用户)
参考[How to change VirtualBox default VM location in command line](http://askubuntu.com/questions/800824/how-to-change-virtualbox-default-vm-location-in-command-line)
[8.30. VBoxManage setproperty](https://www.virtualbox.org/manual/ch08.html#vboxmanage-setproperty)

huzhifeng@Ubuntu16041:~$ mkdir -p /home/huzhifeng/VBox
huzhifeng@Ubuntu16041:~$ vboxmanage setproperty machinefolder /home/huzhifeng/VBox
huzhifeng@Ubuntu16041:~$ vboxmanage list systemproperties | grep “Default machine folder:”
Default machine folder: /home/huzhifeng/VBox
huzhifeng@Ubuntu16041:~$ sudo vboxmanage list systemproperties | grep “Default machine folder:”
Default machine folder: /root/VirtualBox VMs
huzhifeng@Ubuntu16041:~$

1
2
3
### USB 重定向
有时想把 U 盘或者其它 USB 设备穿透给虚拟机用,普通用户一般是没有权限的,需要加入到组`vboxusers`,然后重启就可以生效了。

huzhifeng@Ubuntu16041:~$ sudo usermod -a -G vboxusers huzhifeng
huzhifeng@Ubuntu16041:~$ id huzhifeng
uid=1000(huzhifeng) gid=1000(huzhifeng) groups=1000(huzhifeng),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),128(sambashare),129(vboxusers)
huzhifeng@Ubuntu16041:~$ reboot
huzhifeng@Ubuntu16041:~$ VBoxManage list extpacks
Extension Packs: 1
Pack no. 0: Oracle VM VirtualBox Extension Pack
Version: 5.1.12
Revision: 112440
Edition:
Description: USB 2.0 and USB 3.0 Host Controller, Host Webcam, VirtualBox RDP, PXE ROM, Disk Encryption, NVMe.
VRDE Module: VBoxVRDP
Usable: true
Why unusable:
huzhifeng@Ubuntu16041:~$ VBoxManage list usbhost
Host USB Devices:

UUID: 084794d0-1f3b-4212-805f-2b51ea09cffb
VendorId: 0x2207 (2207)
ProductId: 0x310b (310B)
Revision: 1.0 (0100)
Port: 12
USB version/speed: 2/High
Address: sysfs:/sys/devices/pci0000:00/0000:00:14.0/usb1/1-13//device:/dev/vboxusb/001/020
Current State: Available

UUID: fddf0527-bad1-45f9-b3e1-b0046a44029f
VendorId: 0x093a (093A)
ProductId: 0x2510 (2510)
Revision: 1.0 (0100)
Port: 11
USB version/speed: 1/Low
Manufacturer: PixArt
Product: USB Optical Mouse
Address: sysfs:/sys/devices/pci0000:00/0000:00:14.0/usb1/1-12//device:/dev/vboxusb/001/002
Current State: Busy

huzhifeng@Ubuntu16041:~$
```

无法安装 64 位操作系统

VirtualBox安装64位系统CentOS会遇到的问题
VirtualBox 本身不带 64 位支持,它的 64 位支持依赖于通过 CPU 虚拟技术把 CPU 的 64 位指令直接映射过去。
所以,要支持 64 位必须:

  1. 你的 CPU 支持 64 位。
  2. 你的 CPU 支持虚拟化,并且你的 BIOS 支持把 CPU 虚拟化功能开启。
    3 .你的 VirtualBox 是 2.0 以上(2.0以上才支持 64 位)。

解决方案:
在主板的高级 BIOS 参数设定中我们可以找到 CPU 的参数设定选项,其中 AMD 处理器的相关选项为 “Virtualization”,Intel 处理器为 “Intel(R) Virtualization Technology”。将相关选项设置为 “Enabled” 即可开启处理器的虚拟化技术。

EOF