VirtualBox 完整使用教程

Oracle VM VirtualBox 是一款功能强大的开源虚拟化软件,可在 Windows、Linux、macOS 上运行,支持创建和运行 x86 和 AMD64/Intel64 虚拟机的免费Hypervisor。


目录

  1. VirtualBox 简介
  2. 安装与配置
  3. 创建虚拟机
  4. 虚拟机设置
  5. 网络配置
  6. 存储与快照
  7. 共享功能
  8. 高级功能
  9. 常见问题与解决方案

1. VirtualBox 简介

1.1 主要特性

mindmap root((VirtualBox)) 平台支持 Windows 主机 Linux 主机 macOS 主机 Solaris 主机 客户机支持 Windows 系列 Linux 发行版 macOS Solaris FreeBSD 其他 OS 核心功能 硬件虚拟化 快照管理 克隆功能 共享文件夹 无缝窗口模式 虚拟机组网
特性 说明
跨平台 支持 Windows/Linux/macOS/Solaris
免费开源 GPL 许可证,免费使用
硬件虚拟化 支持 VT-x/AMD-V
嵌套分页 支持二级地址转换
共享剪贴板 主机与虚拟机之间复制粘贴
拖放 支持文件拖放
快照 保存/恢复虚拟机状态
克隆 完整克隆或链接克隆
虚拟网络 NAT/桥接/Host-Only/内部网络

1.2 核心组件

flowchart TD A[VirtualBox] --> B[VirtualBox GUI] A --> C[VBoxManage] A --> D[Extension Pack] A --> E[Guest Additions] B --> B1[图形管理界面] C --> C1[命令行工具] D --> D1[增强功能包<br/>USB3.0/远程桌面/RDP] E --> E1[客户机增强功能<br/>共享文件夹/共享剪贴板] subgraph 虚拟机 F[虚拟硬件] G[虚拟磁盘] H[虚拟网络] end A --> F A --> G A --> H
组件 命令/文件 说明
GUI virtualbox 图形化管理界面
CLI VBoxManage 命令行管理工具
Extension .vbox-extpack 扩展包(需手动安装)
Guest Additions 镜像文件 客户机增强功能

2. 安装与配置

2.1 系统要求

flowchart LR A[系统要求] --> B[硬件] A --> C[软件] A --> D[操作系统] B --> B1[VT-x/AMD-V 支持] B --> B2[PAE/NX 支持] B --> B3[至少 4GB 内存] B --> B4[20GB+ 可用磁盘] C --> C1[最新版 VirtualBox] C --> C2[Extension Pack] C --> C3[Guest Additions] D --> D1[Windows 7+] D --> D2[Ubuntu 12.04+] D --> D3[macOS 10.13+]
要求 最低配置 推荐配置
CPU 单核 多核
内存 2GB 4GB+
磁盘 10GB 50GB+
BIOS 开启虚拟化 VT-x/AMD-V 已启用

2.2 安装步骤

flowchart TD A[下载 VirtualBox] --> B[运行安装程序] B --> C{选择安装位置} C -->|默认| D[下一步] C -->|自定义| E[选择其他路径] E --> D D --> F[安装驱动] F --> G[完成安装] G --> H[安装 Extension Pack] H --> I[安装 Guest Additions]

安装后检查:

# 检查 BIOS 虚拟化是否开启(Windows)
systeminfo | findstr /C:"Hyper-V"

# 或使用 CPU-Z 工具查看

2.3 VBoxManage 命令行

# 路径(添加到环境变量)
D:\VirtualBox\VBoxManage.exe

# 基本信息
VBoxManage --version          # 查看版本
VBoxManage list --help        # 查看所有列表命令

# 虚拟机列表
VBoxManage list vms            # 所有虚拟机
VBoxManage list runningvms     # 运行中的虚拟机
VBoxManage list systemproperties   # 系统属性

# 详细查看某个虚拟机
VBoxManage showvminfo <vm-name>

2.4 Extension Pack 安装

flowchart LR A[下载 Extension Pack] --> B[双击 .vbox-extpack 文件] A --> C[或者用命令行] C --> D[VBoxManage extpack install <pack.vbox-extpack>] B --> E[接受许可证] D --> E E --> F[安装完成]

安装命令:

VBoxManage extpack install --replace <extension_pack_path>

卸载命令:

VBoxManage extpack uninstall <extension_name>

3. 创建虚拟机

3.1 创建流程

flowchart TD A[新建虚拟机] --> B[输入名称] B --> C[选择类型] C --> D[选择版本] D --> E[分配内存] E --> F[创建虚拟硬盘] F --> G[选择硬盘类型] G --> H[分配硬盘大小] H --> I[完成创建]

3.2 使用 GUI 创建

步骤:

  1. 点击「新建」
  2. 输入虚拟机名称
  3. 选择操作系统类型和版本
  4. 分配内存大小
  5. 创建虚拟硬盘
  6. 选择硬盘类型(VDI/VHD/VMDK)
  7. 选择分配方式(动态分配/固定大小)
  8. 设置硬盘大小
  9. 完成

3.3 使用 VBoxManage 创建

# 1. 创建虚拟机
VBoxManage createvm --name "Ubuntu22" --ostype "Ubuntu_64" --register

# 2. 设置内存和CPU
VBoxManage modifyvm "Ubuntu22" --memory 2048 --cpus 2

# 3. 创建虚拟硬盘
VBoxManage createmedium disk --filename "D:\VMs\Ubuntu22\disk.vdi" --size 51200 --format VDI --variant Standard

# 4. 添加硬盘到虚拟机
VBoxManage storageattach "Ubuntu22" --storagectl "SATA" --port 0 --device 0 --type hdd --medium "D:\VMs\Ubuntu22\disk.vdi"

# 5. 添加光驱(用于安装系统)
VBoxManage storageattach "Ubuntu22" --storagectl "SATA" --port 1 --device 0 --type dvddrive --medium "D:\ISOs\ubuntu-22.04.iso"

3.4 操作系统类型

类型 版本示例
Windows Windows 10, Windows 11, Windows 7, Windows XP
Linux Ubuntu, Debian, Fedora, CentOS, Red Hat
MacOS macOS 10.13, macOS 10.14
Solaris Solaris 11
BSD FreeBSD, OpenBSD
其他 DOS, Windows 3.x, Linux 2.2/2.4/2.6

4. 虚拟机设置

4.1 系统设置

flowchart TD A[系统设置] --> B[主板] A --> C[处理器] A --> D[加速] B --> B1[内存大小] B --> B2[启动顺序] B --> B3[芯片组] B --> B4[指点设备] C --> C1[CPU 数量] C --> C2[CPU 执行限制] C --> C3[PAE/NX] C --> C4[虚拟化引擎] D --> D1[VT-x/AMD-V] D --> D2[嵌套分页] D --> D3[PAE]

VBoxManage 修改示例:

# 内存
VBoxManage modifyvm "Ubuntu22" --memory 4096

# CPU
VBoxManage modifyvm "Ubuntu22" --cpus 4
VBoxManage modifyvm "Ubuntu22" --cpuexecutioncap 80

# 启动顺序
VBoxManage modifyvm "Ubuntu22" --boot1 dvd --boot2 disk --boot3 none

# 芯片组
VBoxManage modifyvm "Ubuntu22" --chipset ich9

# 指点设备
VBoxManage modifyvm "Ubuntu22" --pointing "usbtablet"

4.2 显示设置

# 显存
VBoxManage modifyvm "Ubuntu22" --vram 128

# 显卡加速
VBoxManage modifyvm "Ubuntu22" --accelerate3d on

# 显示器数量
VBoxManage modifyvm "Ubuntu22" --monitorcount 2

# 全屏模式
VBoxManage setextradata "Ubuntu22" GUI/Fullscreen True

4.3 存储设置

flowchart LR A[存储控制器] --> B[SATA] A --> C[IDE] A --> D[SCSI] A --> E[NVMe] A --> F[USB] B --> B1[可选 ICH9] C --> C1[传统控制器] D --> D1[适合服务器] E --> E1[高性能] F --> F1[USB 设备]

常用命令:

# 查看存储控制器
VBoxManage showvminfo "Ubuntu22" --storagecontrollers

# 添加 SATA 控制器
VBoxManage storagectl "Ubuntu22" --name "SATA" --add sata --controller IntelAhci --portcount 4

# 附加虚拟硬盘
VBoxManage storageattach "Ubuntu22" --storagectl "SATA" --port 0 --device 0 --type hdd --medium "D:\VMs\Ubuntu22\disk.vdi"

# 附加 ISO
VBoxManage storageattach "Ubuntu22" --storagectl "SATA" --port 1 --device 0 --type dvddrive --medium "D:\ISOs\ubuntu.iso"

# 移除介质
VBoxManage storageattach "Ubuntu22" --storagectl "SATA" --port 0 --device 0 --medium none

4.4 音频设置

# 启用音频
VBoxManage modifyvm "Ubuntu22" --audio pulse --audiocontroller ac97

# 禁用音频
VBoxManage modifyvm "Ubuntu22" --audio none

4.5 USB 设置

# 启用 USB 控制器
VBoxManage modifyvm "Ubuntu22" --usb on
VBoxManage modifyvm "Ubuntu22" --usbxhci on  # USB 3.0

# 添加 USB 过滤器(虚拟机启动时自动连接)
VBoxManage modifyvm "Ubuntu22" --usbfilter add 0 --name "MyDevice" --vendorid 0x1234 --productid 0x5678

5. 网络配置

5.1 网络类型

flowchart TD A[网络类型] --> B[NAT] A --> C[NAT 网络] A --> D[桥接网络] A --> E[Host-Only] A --> F[内部网络] A --> G[桥接到] B --> B1[虚拟机可上网] B --> B2[主机不可访问] B --> B3[端口转发] C --> C1[多虚拟机互通] C --> C2[隔离网络] D --> D1[获取局域网 IP] D --> D2[主机可访问] E --> E1[仅主机互通] E --> E2[需虚拟网卡] F --> F1[完全隔离] F --> F2[仅虚拟机间]
类型 虚拟机访问外网 主机访问虚拟机 虚拟机之间互通
NAT 需端口转发
NAT 网络 需端口转发
桥接
Host-Only
内部网络

5.2 NAT 端口转发

# 添加端口转发(SSH 为例)
VBoxManage modifyvm "Ubuntu22" --natpf1 "ssh,tcp,,2222,,22"

# 格式:<规则名>,<协议>,<主机端口>,<虚拟机IP>,<虚拟机端口>

# 删除端口转发规则
VBoxManage modifyvm "Ubuntu22" --natpf1 delete "ssh"

# 查看端口转发规则
VBoxManage showvminfo "Ubuntu22" --details | findstr NAT

常用端口转发规则:

服务 主机端口 虚拟机端口
SSH 2222 22
HTTP 8080 80
HTTPS 8443 443
RDP 3389 3389

5.3 Host-Only 网络

flowchart LR A[Host-Only] --> B[创建虚拟网卡] A --> C[配置网络] A --> D[分配给虚拟机] B --> B1[VBoxManage hostonlyif create] C --> C2[设置 IP] D --> D1[选择网络接口]
# 1. 创建 Host-Only 网络接口
VBoxManage hostonlyif create

# 2. 配置 IP(主机端)
VBoxManage hostonlyif ipconfig "VirtualBox Host-Only Network" --ip 192.168.56.1 --netmask 255.255.255.0

# 3. 虚拟机选择 Host-Only 适配器
VBoxManage modifyvm "Ubuntu22" --nic1 hostonly

# 4. 设置虚拟机 IP(虚拟机内手动配置)

5.4 桥接网络

# 选择桥接网络
VBoxManage modifyvm "Ubuntu22" --nic1 bridged

# 选择物理网卡
VBoxManage modifyvm "Ubuntu22" --bridgeadapter1 "Intel(R) Ethernet Connection"

5.5 内网设置

# 创建内部网络
VBoxManage modifyvm "Ubuntu22" --nic1 intnet --intnet1 "intnet1"

# 设置网段
VBoxManage dhcpserver modify --network "intnet1" --ip 10.0.1.1 --netmask 255.255.255.0 --enable

6. 存储与快照

6.1 虚拟硬盘类型

类型 格式 特点
VDI VirtualBox 原生 跨平台兼容性好
VHD 微软格式 Azure/Hyper-V 兼容
VMDK VMware 格式 VMware 兼容性

分配方式:

方式 初始大小 实际占用 性能
动态分配 较小 随使用增长 稍差
固定大小 预分配 始终满额 较好

6.2 创建虚拟硬盘

# 创建 50GB 动态分配 VDI
VBoxManage createmedium disk --filename "D:\VMs\Ubuntu22\disk.vdi" --size 51200 --format VDI --variant Standard

# 创建 50GB 固定大小 VDI
VBoxManage createmedium disk --filename "D:\VMs\Ubuntu22\disk_fixed.vdi" --size 51200 --format VDI --variant Fixed

# 创建 VHD
VBoxManage createmedium disk --filename "D:\VMs\Ubuntu22\disk.vhd" --size 51200 --format VHD --variant Standard

6.3 硬盘操作

# 克隆硬盘
VBoxManage clonemedium disk "D:\VMs\Ubuntu22\disk.vdi" "D:\VMs\Ubuntu22\disk_clone.vdi" --format VDI

# 移动硬盘
VBoxManage modifymedium disk "D:\VMs\Ubuntu22\disk.vdi" --move "D:\NewLocation\disk.vdi"

# 压缩硬盘(仅动态分配 VDI)
VBoxManage modifymedium disk "D:\VMs\Ubuntu22\disk.vdi" --compact

# 调整大小
VBoxManage modifymedium disk "D:\VMs\Ubuntu22\disk.vdi" --resize 102400  # 调整到 100GB

6.4 快照管理

flowchart TD A[快照管理] --> B[拍摄快照] A --> C[恢复快照] A --> D[删除快照] A --> E[克隆快照状态] B --> B1[保存当前状态] B --> B2[可多次拍摄] B --> B3[命名管理] C --> C1[回滚到之前] C --> C2[不影响当前] D --> D1[清理磁盘空间] D --> D2[释放存储]

快照命令:

# 拍摄快照
VBoxManage snapshot "Ubuntu22" take "snapshot-name" --description "描述"

# 查看快照
VBoxManage snapshot "Ubuntu22" list

# 恢复快照(关闭虚拟机后)
VBoxManage snapshot "Ubuntu22" restore "snapshot-name"

# 恢复快照(运行中)
VBoxManage snapshot "Ubuntu22" restorecurrent

# 删除快照
VBoxManage snapshot "Ubuntu22" delete "snapshot-name"

6.5 克隆虚拟机

flowchart LR A[克隆类型] --> B[完整克隆] A --> C[链接克隆] B --> B1[复制所有磁盘] B --> B2[完全独立] B --> B3[占用空间大] C --> C1[依赖原磁盘] C --> C2[占用空间小] C --> C3[快速创建]
# 完整克隆
VBoxManage clonevm "Ubuntu22" --name "Ubuntu22 Clone" --register

# 链接克隆
VBoxManage clonevm "Ubuntu22" --name "Ubuntu22 Linked" --register --options link

# 克隆到特定位置
VBoxManage clonevm "Ubuntu22" --name "Ubuntu22 Clone" --register --options all --snapshot "snapshot-name" --basefolder "D:\VMs\Clones"

7. 共享功能

7.1 共享文件夹

flowchart TD A[共享文件夹] --> B[创建共享] A --> C[自动挂载] A --> D[手动挂载] B --> B1[主机选择文件夹] B --> B2[命名共享文件夹] C --> C1[虚拟机启动时挂载] C --> C2[自动分配驱动器号] D --> D1[客户机手动挂载] D --> D2[支持 Linux 挂载点]

设置共享文件夹:

# 添加共享文件夹(需要 Guest Additions)
VBoxManage sharedfolder add "Ubuntu22" --name "shared" --hostpath "D:\Shared" --automount

# 添加只读共享
VBoxManage sharedfolder add "Ubuntu22" --name "readonly" --hostpath "D:\Readonly" --readonly

# 移除共享
VBoxManage sharedfolder remove "Ubuntu22" --name "shared"

Linux 客户机挂载:

# 手动挂载
sudo mount -t vboxsf shared /mnt/shared

# 卸载
sudo umount /mnt/shared

# 永久挂载(/etc/fstab)
shared  /mnt/shared  vboxsf  defaults  0  0

7.2 共享剪贴板

# 启用双向剪贴板
VBoxManage modifyvm "Ubuntu22" --clipboard bidirectional

# 选项:disabled / hosttoguest / guesttohost / bidirectional

7.3 拖放

# 启用拖放(双向)
VBoxManage modifyvm "Ubuntu22" --draganddrop bidirectional

# 选项:disabled / hosttoguest / guesttohost / bidirectional

7.4 远程桌面

# 启用 VRDP 服务器
VBoxManage modifyvm "Ubuntu22" --vrde on

# 设置端口
VBoxManage modifyvm "Ubuntu22" --vrdeport 3389

# 设置地址(仅允许特定 IP 连接)
VBoxManage modifyvm "Ubuntu22" --vrdeaddress "192.168.1.100"

# 查看 VRDE 配置
VBoxManage showvminfo "Ubuntu22" | findstr VRDE

8. 高级功能

8.1 虚拟机控制

# 启动虚拟机(无界面)
VBoxManage startvm "Ubuntu22" --type headless

# 后台启动
VBoxManage startvm "Ubuntu22" --type separate

# 正常关闭(发送 ACPI 信号)
VBoxManage controlvm "Ubuntu22" acpipowerbutton

# 强制关闭(拔电源)
VBoxManage controlvm "Ubuntu22" poweroff

# 暂停
VBoxManage controlvm "Ubuntu22" pause

# 恢复运行
VBoxManage controlvm "Ubuntu22" resume

# 重启
VBoxManage controlvm "Ubuntu22" reset

# 保存状态(休眠)
VBoxManage controlvm "Ubuntu22" savestate

8.2 导入导出

flowchart LR A[OVA 导入导出] --> B[导出为 OVA] A --> C[导入 OVA] A --> D[转换格式] B --> B1[VBoxManage export] C --> C1[VBoxManage import] D --> D1[VBoxManage clonemedium]
# 导出为 OVA
VBoxManage export "Ubuntu22" --output "D:\VMs\Ubuntu22.ova"

# 导入 OVA
VBoxManage import "D:\VMs\Ubuntu22.ova"

# 自定义导入配置
VBoxManage import "D:\VMs\Ubuntu22.ova" --vsys 0 --memory 4096 --cpus 2

8.3 性能优化

# 推荐设置
VBoxManage modifyvm "Ubuntu22" --memory 4096      # 分配足够内存
VBoxManage modifyvm "Ubuntu22" --cpus 2           # 分配多核 CPU
VBoxManage modifyvm "Ubuntu22" --accelerate3d on   # 启用 3D 加速
VBoxManage modifyvm "Ubuntu22" --vram 128         # 分配足够显存

# I/O 缓存
VBoxManage storagectl "Ubuntu22" --name "SATA" --hostiocache on

# CPU 执行限制
VBoxManage modifyvm "Ubuntu22" --cpuexecutioncap 100  # 100% 性能

# 使用固态硬盘
VBoxManage modifymedium disk "D:\VMs\Ubuntu22\disk.vdi" --type ssd

8.4 虚拟机组

# 创建组
VBoxManage groups create "/MyGroup"

# 将虚拟机添加到组
VBoxManage modifyvm "Ubuntu22" --groups "/MyGroup"

# 从组移除
VBoxManage modifyvm "Ubuntu22" --groups ""

# 列出组
VBoxManage list groups

9. 常见问题与解决方案

9.1 虚拟机无法启动

问题:VT-x/AMD-V 未启用

# 检查虚拟化状态
systeminfo | findstr /C:"Hyper-V"

# 解决方案:重启电脑,进入 BIOS/UEFI 设置
# 找到 Virtualization Technology 选项,设为 Enabled

问题:内存不足

# 减少虚拟机内存
VBoxManage modifyvm "Ubuntu22" --memory 2048

9.2 虚拟机卡死无法关闭

# 强制关闭
VBoxManage controlvm "Ubuntu22" poweroff

# 或者杀死进程
taskkill /F /IM VBoxHeadless.exe
taskkill /F /IM VirtualBox.exe

9.3 虚拟机黑屏

# 1. 禁用 3D 加速
VBoxManage modifyvm "Ubuntu22" --accelerate3d off

# 2. 更改显存
VBoxManage modifyvm "Ubuntu22" --vram 128

# 3. 更改图形控制器
VBoxManage modifyvm "Ubuntu22" --graphicscontroller vmsvga

9.4 虚拟机无法识别 USB

# 1. 确保安装了 Extension Pack
VBoxManage list extpacks

# 2. 启用 USB 控制器
VBoxManage modifyvm "Ubuntu22" --usb on
VBoxManage modifyvm "Ubuntu22" --usbxhci on  # USB 3.0

# 3. 在虚拟机内安装 Guest Additions

9.5 虚拟机网络不通

# 1. 检查网络类型
VBoxManage showvminfo "Ubuntu22" | findstr NIC

# 2. NAT 模式检查端口转发
VBoxManage showvminfo "Ubuntu22" --details | findstr NAT

# 3. 重置网络
VBoxManage modifyvm "Ubuntu22" --nic1 nat
VBoxManage modifyvm "Ubuntu22" --nic1 bridged

9.6 虚拟机磁盘空间不足

# 1. 扩展虚拟硬盘
VBoxManage modifymedium disk "D:\VMs\Ubuntu22\disk.vdi" --resize 102400

# 2. 在虚拟机内扩展分区(需要 GParted)

# 3. 压缩动态分配硬盘
VBoxManage modifymedium disk "D:\VMs\Ubuntu22\disk.vdi" --compact

9.7 性能问题排查

flowchart TD A[性能问题] --> B[CPU 占用高] A --> C[内存不足] A --> D[磁盘 IO 慢] A --> E[网络延迟] B --> B1[减少 vCPU 数量] B --> B2[降低 cpuexecutioncap] C --> C1[增加分配内存] C --> C2[启用内存 ballooning] D --> D1[使用固定大小硬盘] D --> D2[启 用主机缓存] E --> E1[使用桥接网络] E --> E2[减少网络延迟]

性能优化建议:

问题 解决方案
运行缓慢 分配更多 CPU/内存
3D 应用卡顿 启用 3D 加速,安装 Guest Additions
磁盘性能差 使用固定大小 VDI/启 用主机缓存
网络延迟高 使用桥接网络代替 NAT
全屏模糊 安装 Guest Additions,启用 2D 加速

9.8 Guest Additions 安装

# 在 Linux 客户机中安装
sudo apt update
sudo apt install build-essential dkms linux-headers-$(uname -r)

# 挂载 Guest Additions 镜像
sudo mkdir -p /mnt/cdrom
sudo mount /dev/cdrom /mnt/cdrom

# 运行安装程序
sudo /mnt/cdrom/VBoxLinuxAdditions.run

# 重启
sudo reboot
# 在 Windows 客户机中安装
# 插入 Guest Additions 镜像(设备 -> 插入增强功能...)
# 运行安装程序

附录:VBoxManage 常用命令速查

# 虚拟机管理
VBoxManage list vms                          # 列出所有虚拟机
VBoxManage showvminfo <vm>                   # 显示虚拟机信息
VBoxManage createvm --name <name>            # 创建虚拟机
VBoxManage modifyvm <vm> --memory <size>     # 修改内存
VBoxManage startvm <vm>                       # 启动虚拟机
VBoxManage controlvm <vm> poweroff            # 强制关闭

# 存储管理
VBoxManage storagectl <vm> --add sata        # 添加存储控制器
VBoxManage storageattach <vm> ...             # 挂载存储介质
VBoxManage createmedium disk ...             # 创建虚拟硬盘

# 快照管理
VBoxManage snapshot <vm> take <name>          # 拍摄快照
VBoxManage snapshot <vm> restore <name>      # 恢复快照
VBoxManage snapshot <vm> delete <name>       # 删除快照

# 网络管理
VBoxManage hostonlyif create                 # 创建 Host-Only 网络
VBoxManage natnetwork add ...                # 添加 NAT 网络

最后更新:2026年5月

posted @ 2026-05-12 11:50  RK5123153  阅读(28)  评论(0)    收藏  举报