Skip to Content
DokumentationEntwicklungVirtuelle Maschinen und Container

Virtuelle Maschinen und Container

Dieser Artikel behandelt verschiedene Virtualisierungs- und Systemcontainertechnologien, die auf Ubuntu 26.04 verfügbar sind.

Überprüfen Sie die Hardware-Virtualisierungsunterstützung

Bevor Sie beginnen, vergewissern Sie sich, dass Ihre CPU Hardwarevirtualisierung unterstützt:

# Check for virtualization support egrep -c '(vmx|svm)' /proc/cpuinfo

Ein Ergebnis größer als 0 weist auf Unterstützung hin. Sie können auch cpu-checker verwenden:

sudo apt install cpu-checker -y kvm-ok

KVM / QEMU

KVM (Kernel-based Virtual Machine) ist eine in den Linux-Kernel integrierte Virtualisierungslösung. In Kombination mit QEMU bietet es vollständige Virtualisierungsfunktionen.

Installieren Sie KVM und verwandte Tools

sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virt-manager -y

Fügen Sie den aktuellen Benutzer zu den erforderlichen Gruppen hinzu:

sudo usermod -aG libvirt $USER sudo usermod -aG kvm $USER

Melden Sie sich ab und wieder an, damit die Gruppenänderungen wirksam werden.

Überprüfen Sie die Installation

# Check libvirt service status sudo systemctl status libvirtd # Verify connection virsh list --all

Virt-Manager (GUI) verwenden

virt-manager

virt-manager bietet eine intuitive grafische Oberfläche zum Erstellen und Verwalten virtueller Maschinen.

Erstellen Sie eine VM über die Befehlszeile

# Create a disk image qemu-img create -f qcow2 /var/lib/libvirt/images/ubuntu-vm.qcow2 40G # Install a VM from ISO virt-install \ --name ubuntu-vm \ --ram 4096 \ --vcpus 2 \ --disk path=/var/lib/libvirt/images/ubuntu-vm.qcow2,size=40 \ --os-variant ubuntu26.04 \ --network bridge=virbr0 \ --graphics vnc \ --cdrom /path/to/ubuntu-26.04-desktop-amd64.iso
Note

Führen Sie osinfo-query os | grep ubuntu aus, um zu bestätigen, ob Ihre lokale osinfo-db ubuntu26.04 enthält. Wenn nicht, aktualisieren Sie osinfo-db, greifen Sie auf den nächstgelegenen ubuntu24.04 zurück oder verwenden Sie --osinfo detect=on,require=off, damit virt-install automatisch erkennt.

Allgemeine Virsh-Befehle

# List all VMs virsh list --all # Start a VM virsh start ubuntu-vm # Shut down a VM virsh shutdown ubuntu-vm # Force stop virsh destroy ubuntu-vm # Suspend and resume virsh suspend ubuntu-vm virsh resume ubuntu-vm # Create snapshots virsh snapshot-create-as ubuntu-vm --name "clean-install" virsh snapshot-list ubuntu-vm virsh snapshot-revert ubuntu-vm --snapshotname "clean-install" # Delete a VM virsh undefine ubuntu-vm --remove-all-storage

QEMU direkt verwenden

Ohne libvirt, direkte Verwendung von QEMU:

# Create a disk qemu-img create -f qcow2 disk.qcow2 20G # Start a VM qemu-system-x86_64 \ -enable-kvm \ -m 4G \ -smp 2 \ -hda disk.qcow2 \ -cdrom ubuntu.iso \ -boot d \ -net nic -net user,hostfwd=tcp::2222-:22

VirtualBox

VirtualBox ist die Open-Source-Virtualisierungssoftware von Oracle mit einer übersichtlichen grafischen Oberfläche.

Installieren Sie VirtualBox

# Method 1: Install from Ubuntu repository sudo apt install virtualbox -y # Method 2: Install from Oracle's official repository (newer version) wget -qO- https://www.virtualbox.org/download/oracle_vbox_2016.asc | sudo gpg --dearmor -o /etc/apt/keyrings/oracle-virtualbox.gpg echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/oracle-virtualbox.gpg] https://download.virtualbox.org/virtualbox/debian $(lsb_release -cs) contrib" | sudo tee /etc/apt/sources.list.d/virtualbox.list sudo apt update # Install VirtualBox (run apt-cache search virtualbox to confirm the current # available package name, e.g. virtualbox-7.1 or a newer version) sudo apt install virtualbox-7.1 -y

Installieren Sie das Erweiterungspaket

Das Erweiterungspaket bietet USB 3.0, RDP-Remotedesktop und weitere Funktionen:

# Install the extension pack after downloading the matching version sudo VBoxManage extpack install Oracle_VirtualBox_Extension_Pack-*.vbox-extpack

VBoxManage-Befehlszeile

# List VMs VBoxManage list vms # Start a VM (headless mode) VBoxManage startvm "vm-name" --type headless # Shut down a VM VBoxManage controlvm "vm-name" acpipowerbutton # Create a snapshot VBoxManage snapshot "vm-name" take "snapshot-name"

LXD / LXC-Systemcontainer

LXC ist eine Linux-Containertechnologie und LXD ist ihr Verwaltungsdaemon, der ein VM-ähnliches Erlebnis bietet, aber einfacher ist.

Installieren Sie LXD

sudo snap install lxd

LXD initialisieren

lxd init --minimal

Oder nutzen Sie die interaktive Konfiguration für weitere Optionen:

lxd init

Grundlegende LXD-Operationen

# Launch an Ubuntu container lxc launch ubuntu:26.04 mycontainer # Launch a VM (yes, LXD supports VMs too) lxc launch ubuntu:26.04 myvm --vm # List instances lxc list # Enter a container lxc exec mycontainer -- bash # Push and pull files lxc file push local-file.txt mycontainer/root/ lxc file pull mycontainer/root/file.txt . # Stop and delete lxc stop mycontainer lxc delete mycontainer # Create snapshots lxc snapshot mycontainer snap0 lxc restore mycontainer snap0

LXD-Profilkonfiguration

# View default profile lxc profile show default # Create a custom profile lxc profile create dev-profile lxc profile set dev-profile limits.cpu 4 lxc profile set dev-profile limits.memory 8GB # Launch a container with a custom profile lxc launch ubuntu:26.04 devbox --profile dev-profile

Amboss

Incus ist ein Community-Fork von LXD, der von der Linux Containers-Community verwaltet wird. Es hat eine ähnliche Funktionalität, wurde jedoch unabhängig entwickelt.

Installieren Sie Incus

# Add the Zabbly repository (officially recommended for Incus) curl -fsSL https://pkgs.zabbly.com/key.asc | sudo gpg --dearmor -o /etc/apt/keyrings/zabbly.gpg echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/zabbly.gpg] https://pkgs.zabbly.com/incus/stable $(. /etc/os-release && echo "$VERSION_CODENAME") main" | sudo tee /etc/apt/sources.list.d/zabbly-incus-stable.list sudo apt update sudo apt install incus -y

Initialisieren und verwenden

# Initialize sudo incus admin init --minimal # Add user to the incus-admin group sudo usermod -aG incus-admin $USER newgrp incus-admin # Usage is similar to LXD incus launch images:ubuntu/26.04 mycontainer incus list incus exec mycontainer -- bash incus stop mycontainer incus delete mycontainer

Von LXD nach Incus migrieren

sudo incus admin migrate-lxd

Technologievergleich

FunktionKVM/QEMUVirtualBoxLXD/Incus
VirtualisierungstypVollständige VirtualisierungVollständige VirtualisierungSystemcontainer/VMs
LeistungFast einheimischGutFast einheimisch
IsolierungStarkStarkMittel (Container) / Stark (VMs)
RessourcennutzungMittelHöherNiedrig (Behälter)
AnwendungsfälleServer, ProduktionDesktop-Entwicklung/TestsEntwicklungsumgebungen, leichte Isolation

Wählen Sie je nach Anwendungsfall den richtigen Virtualisierungsansatz: KVM für Produktionsserver, VirtualBox für Desktop-Benutzer und LXD oder Incus für isolierte Entwicklungsumgebungen.