温馨提示×

CentOS VirtualBox如何安装Guest Additions

小樊
52
2025-09-19 23:14:42
栏目: 智能运维

Installing VirtualBox Guest Additions on CentOS: A Step-by-Step Guide

VirtualBox Guest Additions enhance your CentOS virtual machine (VM) with critical features like shared folders, clipboard sharing, automatic screen resolution adjustment, and smoother mouse/keyboard integration. Below is a structured guide to installing them, including prerequisites and troubleshooting tips.

1. Preparation: Update System and Install Dependencies

Before installing Guest Additions, ensure your CentOS system is up-to-date and has the necessary tools to compile kernel modules. Run these commands in the terminal:

# Update system packages (CentOS 7/8/9 compatible)
sudo yum update -y   # For CentOS 7
# sudo dnf update -y # For CentOS 8/9 (uncomment if using DNF)

# Install compilation tools and kernel headers (critical for Guest Additions)
sudo yum install -y epel-release gcc make perl kernel-devel kernel-headers dkms bzip2

Why?

  • kernel-devel and kernel-headers must match your running kernel version (check with uname -r) to compile Guest Additions modules.
  • dkms (Dynamic Kernel Module Support) ensures modules rebuild automatically after kernel updates.
  • bzip2 is required to extract installation files.

2. Mount the Guest Additions ISO

VirtualBox provides a virtual CD-ROM with Guest Additions. To mount it:

  1. Start your CentOS VM in VirtualBox.
  2. Click the Devices menu at the top of the VM window.
  3. Select Insert Guest Additions CD Image….
  4. A notification will appear; click Run (or open the CD-ROM manually via the file manager).

The ISO will mount to /run/media/[your-username]/VBox_GAs_[version] (e.g., /run/media/asfor/VBox_GAs_7.2.2).

3. Run the Installation Script

Open a terminal and execute these commands to install Guest Additions:

# Navigate to the mounted CD-ROM directory
cd /run/media/[your-username]/VBox_GAs_[version]  # Replace with your actual path

# Give execute permission to the script (if needed) and run it
sudo chmod +x VBoxLinuxAdditions.run
sudo ./VBoxLinuxAdditions.run

Notes:

  • If you encounter “Permission denied,” ensure you’re using sudo.
  • Ignore warnings about “X Server” or “GUI components”—they’re optional and won’t affect core functionality.

4. Reboot the VM

After installation completes, restart your VM to apply changes:

sudo reboot

Wait for the system to reboot fully before proceeding.

5. Verify Installation Success

Check if Guest Additions are active using these methods:

  • Automatic Resolution: Resize the VM window—the guest OS should adjust its resolution automatically.
  • Shared Folders: Create a shared folder in VirtualBox (Settings → Shared Folders), then run ls /media/sf_[folder-name] in the VM. You should see files from the host.
  • Module Loading: Run lsmod | grep vboxguest. If you see vboxguest, vboxsf, or similar modules, installation succeeded.

Troubleshooting Common Issues

  • Kernel Version Mismatch: If uname -r and rpm -q kernel-devel show different versions, update your kernel:
    sudo yum update -y kernel
    sudo reboot
    
    Then reinstall Guest Additions after the reboot.
  • Build Errors: If the installer fails with “Building kernel modules failed,” check /var/log/vboxadd-install.log for details. Missing dependencies (e.g., gcc, kernel-devel) are the most common cause.
  • Missing Shared Folder: Ensure you’ve configured a shared folder in VirtualBox (Settings → Shared Folders) and mounted it in the VM with:
    sudo mount -t vboxsf [folder-name] /mnt/shared  # Replace with your folder name and mount point
    

By following these steps, you’ll unlock VirtualBox’s full potential for your CentOS VM, enabling seamless interaction between the host and guest systems.

0