KVM Virtualization on CentOS: A Beginner’s Guide

Okay, here’s a comprehensive beginner’s guide to KVM Virtualization on CentOS, aiming for approximately 5000 words:

KVM Virtualization on CentOS: A Beginner’s Guide

Introduction

Virtualization has become a cornerstone of modern IT infrastructure. It allows you to run multiple operating systems (guests) simultaneously on a single physical server (host), maximizing resource utilization, improving flexibility, and reducing costs. Kernel-based Virtual Machine (KVM) is a powerful, open-source virtualization technology built directly into the Linux kernel. This guide will walk you through the fundamentals of KVM virtualization on CentOS, providing a practical, step-by-step approach suitable for beginners. We’ll cover everything from installation and configuration to managing virtual machines (VMs) and exploring advanced features.

Table of Contents

  1. Understanding Virtualization and KVM

    • What is Virtualization?
    • Types of Virtualization
      • Type 1 (Bare-Metal) Hypervisors
      • Type 2 (Hosted) Hypervisors
    • What is KVM?
    • KVM Architecture
    • Advantages of KVM
    • Why CentOS for KVM?
  2. Prerequisites and System Requirements

    • Hardware Requirements
      • Processor with Virtualization Support (Intel VT-x or AMD-V)
      • Sufficient RAM
      • Adequate Storage Space
    • Software Requirements
      • CentOS Installation (Minimal or Server with GUI)
      • Network Connectivity
    • Checking for Virtualization Support
    • Enabling Virtualization in BIOS/UEFI
  3. Installing KVM and Required Packages on CentOS

    • Updating the System
    • Installing KVM Packages
      • qemu-kvm
      • libvirt
      • virt-install
      • virt-manager (Optional, for GUI management)
      • bridge-utils
    • Starting and Enabling libvirtd Service
    • Verifying Installation
    • Adding User to libvirt Group
  4. Creating Your First Virtual Machine (CLI Method)

    • Understanding virt-install
    • Downloading a CentOS ISO Image
    • Creating a Virtual Disk Image (qemu-img)
    • Building the VM with virt-install
      • --name
      • --ram
      • --vcpus
      • --disk
      • --cdrom (or --location)
      • --network
      • --graphics
      • --os-variant
    • Connecting to the VM Console (VNC)
    • Completing the Operating System Installation
  5. Managing Virtual Machines with virsh (CLI)

    • Introduction to virsh
    • Listing VMs (virsh list --all)
    • Starting a VM (virsh start)
    • Stopping a VM (virsh shutdown, virsh destroy)
    • Suspending and Resuming a VM (virsh suspend, virsh resume)
    • Deleting a VM (virsh undefine)
    • Getting VM Information (virsh dominfo)
    • Editing VM Configuration (virsh edit)
    • Creating and Managing Snapshots (virsh snapshot-create-as, virsh snapshot-list, virsh snapshot-revert, virsh snapshot-delete)
    • Autostarting VMs (virsh autostart)
  6. Creating and Managing Virtual Machines with virt-manager (GUI)

    • Launching virt-manager
    • Connecting to the Local libvirtd Daemon
    • Creating a New VM (Wizard-based)
      • Choosing Installation Media
      • Configuring CPU and Memory
      • Creating or Selecting Storage
      • Network Configuration
      • Reviewing and Starting the VM
    • Managing VMs
      • Starting, Stopping, Pausing, and Resuming
      • Viewing VM Details and Performance
      • Accessing the VM Console
      • Adding and Removing Hardware
      • Creating and Managing Snapshots
  7. Networking with KVM

    • Understanding Virtual Networking Concepts
      • Virtual Networks
      • Network Bridges
      • NAT (Network Address Translation)
      • Routed Networks
      • Isolated Networks
    • Default Network (default)
    • Creating a Bridged Network
      • Using nmcli (NetworkManager CLI)
      • Configuring the Bridge Interface
      • Assigning the Bridge to the VM
    • Creating a NAT Network
      • Using virsh net-define and XML Configuration
      • Starting and Autostarting the Network
    • Network Troubleshooting
  8. Storage Management with KVM

    • Understanding Storage Pools
    • Default Storage Pool (default)
    • Creating a Directory-Based Storage Pool
      • Using virsh pool-define-as and XML Configuration
      • Building and Starting the Pool
    • Creating a Logical Volume (LVM) Based Storage Pool
      • Creating a Volume Group and Logical Volume
      • Using virsh pool-define-as for LVM
    • Managing Virtual Disk Images
      • qcow2 vs. raw
      • Creating Disk Images (qemu-img create)
      • Resizing Disk Images (qemu-img resize)
      • Converting Disk Image Formats (qemu-img convert)
      • Cloning Disk Images
  9. Advanced KVM Concepts

    • Live Migration
      • Requirements for Live Migration
      • Shared Storage (NFS, iSCSI, GlusterFS)
      • Performing Live Migration with virsh migrate
    • Overcommitting Resources
      • CPU Overcommit
      • Memory Overcommit (KSM – Kernel Samepage Merging)
    • SR-IOV (Single Root I/O Virtualization)
      • Understanding SR-IOV
      • Configuring SR-IOV
      • Assigning Virtual Functions (VFs) to VMs
    • vGPU (Virtual GPU)
      • Understanding vGPU
      • Types of vGPU (Mediated Pass-through, Full GPU Pass-through)
    • CPU Pinning
      • Improving Performance with CPU Pinning
      • Using virsh vcpupin
  10. Security Considerations

    • SELinux and KVM
      • Understanding SELinux Contexts
      • Managing SELinux for KVM
    • AppArmor (If applicable)
    • Firewall Configuration
      • Using firewalld
      • Allowing Necessary Ports for VNC, SPICE, etc.
    • Regular Security Updates
    • User Access Control
    • Securing Virtual Networks
  11. Troubleshooting Common Issues

    • VM Fails to Start
      • Checking Logs (/var/log/libvirt/qemu/)
      • Verifying Configuration Files
      • Ensuring Sufficient Resources
    • Network Connectivity Problems
      • Checking Network Configuration (Bridge, NAT)
      • Firewall Rules
      • IP Address Conflicts
    • Performance Issues
      • Monitoring Resource Usage (CPU, Memory, Disk I/O)
      • Adjusting VM Resources
      • Using virt-top
    • Storage Issues
      • Checking Disk Space
      • Verifying Storage Pool Configuration
      • Disk Image Corruption
  12. Backup and Restore

    • Backing Up VM Configuration (virsh dumpxml)
    • Backing Up Virtual Disk Images
      • Using qemu-img (Creating Snapshots, Copying Images)
      • Using LVM Snapshots
    • Restoring VMs
      • Defining the VM from XML (virsh define)
      • Restoring Disk Images
  13. Conclusion and Further Learning


1. Understanding Virtualization and KVM

  • What is Virtualization?

    Virtualization is a technology that allows you to create multiple virtual instances (virtual machines or VMs) on a single physical hardware platform. Each VM acts like a separate, independent computer with its own operating system, applications, and resources (CPU, memory, storage, network). This is achieved through a software layer called a hypervisor, which manages the allocation of physical resources to the VMs.

  • Types of Virtualization

    There are two main types of hypervisors:

    • Type 1 (Bare-Metal) Hypervisors: These hypervisors run directly on the host’s hardware, without the need for an underlying operating system. Examples include VMware ESXi, Xen, and Microsoft Hyper-V Server. They offer excellent performance and security due to their direct access to hardware.

    • Type 2 (Hosted) Hypervisors: These hypervisors run on top of an existing operating system (the host OS). Examples include VMware Workstation, Oracle VirtualBox, and KVM (when used with a host OS like CentOS). They are generally easier to set up and manage but may have slightly lower performance compared to Type 1 hypervisors.

  • What is KVM?

    KVM (Kernel-based Virtual Machine) is a full virtualization solution for Linux on x86 hardware containing virtualization extensions (Intel VT or AMD-V). It consists of a loadable kernel module, kvm.ko, that provides the core virtualization infrastructure and a processor-specific module, kvm-intel.ko or kvm-amd.ko. KVM itself performs no emulation. Instead, it exposes the /dev/kvm interface, which a userspace program (like QEMU) then uses to:

    • Set up the guest VM’s address space.
    • Feed the guest simulated I/O.
    • Map the guest’s video display back to the host’s display.
  • KVM Architecture

    KVM leverages the Linux kernel as its hypervisor. This means that every VM is essentially a regular Linux process, scheduled by the standard Linux scheduler. This integration provides several advantages:

    • Leveraging Kernel Features: KVM benefits from all the features and improvements of the Linux kernel, including memory management, process scheduling, device drivers, and security features.
    • Performance: Because KVM is part of the kernel, it has very low overhead, resulting in near-native performance for VMs.
    • Stability: The Linux kernel is a highly stable and mature platform.
    • Open Source: KVM is open-source, meaning it’s free to use, distribute, and modify.

    KVM uses QEMU (Quick Emulator) as a user-space component to provide hardware emulation. QEMU emulates devices like network cards, disk controllers, and graphics adapters, allowing the guest OS to interact with virtual hardware. libvirt is a toolkit to manage virtualization platforms, providing a unified API to interact with KVM, Xen, and other hypervisors.

  • Advantages of KVM

    • Performance: Near-native performance due to kernel integration.
    • Cost-Effective: Open-source and free to use.
    • Scalability: Can handle a large number of VMs.
    • Flexibility: Supports a wide range of guest operating systems.
    • Security: Leverages Linux kernel security features (SELinux, cgroups).
    • Community Support: Large and active open-source community.
    • Integration: Well-integrated with other Linux tools and technologies.
  • Why CentOS for KVM?

    CentOS is a popular choice for KVM hosts due to its:

    • Stability: CentOS is known for its stability and reliability, making it an excellent choice for production environments.
    • Long-Term Support: CentOS releases have long support lifecycles, providing security updates and bug fixes for many years. (Note: CentOS Linux is reaching end-of-life, and migration to alternatives like CentOS Stream, AlmaLinux, or Rocky Linux is recommended).
    • Compatibility: CentOS is closely related to Red Hat Enterprise Linux (RHEL), which is widely used in enterprise environments and has excellent KVM support.
    • Community: Large and active community provides ample resources and support.
    • Package Management: The yum (and now dnf) package manager makes it easy to install and manage software.

2. Prerequisites and System Requirements

Before you begin installing and configuring KVM, ensure your system meets the following requirements:

  • Hardware Requirements

    • Processor with Virtualization Support (Intel VT-x or AMD-V): This is the most critical requirement. Modern processors from Intel and AMD usually include virtualization extensions. These extensions provide hardware assistance for virtualization, significantly improving performance.
    • Sufficient RAM: The amount of RAM you need depends on the number and size of the VMs you plan to run. A good starting point is at least 8GB, but 16GB or more is recommended for running multiple VMs. Remember to account for the RAM needed by the host OS itself.
    • Adequate Storage Space: Each VM will require disk space for its operating system and data. The amount of storage you need depends on the size of your VMs. Consider using a separate partition or logical volume for storing VM images. SSDs will provide significantly better performance than traditional HDDs.
  • Software Requirements

    • CentOS Installation (Minimal or Server with GUI): You’ll need a working installation of CentOS. A minimal installation is sufficient for a dedicated virtualization host, but a server with a GUI can be helpful for beginners, especially when using virt-manager. CentOS 7, 8, or CentOS Stream are all suitable. (Again, note the end-of-life for CentOS Linux).
    • Network Connectivity: Your host server needs network connectivity to download packages and, optionally, to provide network access to your VMs.
  • Checking for Virtualization Support

    You can check if your processor supports virtualization using the following command in the terminal:

    bash
    grep -E --color '(vmx|svm)' /proc/cpuinfo

    • If the command returns output (highlighted in color), your processor supports virtualization. vmx indicates Intel VT-x, and svm indicates AMD-V.
    • If the command returns nothing, your processor likely does not support virtualization, or it may be disabled in the BIOS/UEFI settings.
  • Enabling Virtualization in BIOS/UEFI

    If your processor supports virtualization but the grep command doesn’t show it, you’ll need to enable it in your system’s BIOS or UEFI settings. The exact steps vary depending on your motherboard manufacturer, but generally:

    1. Reboot your computer.
    2. During startup, press the appropriate key (usually Del, F2, F10, F12, or Esc) to enter the BIOS/UEFI setup.
    3. Look for settings related to virtualization, CPU features, or advanced settings. Common names include:
      • Intel Virtualization Technology (VT-x)
      • AMD Virtualization (AMD-V)
      • Virtualization
      • VT-d (for Intel I/O virtualization)
    4. Enable the virtualization setting.
    5. Save the changes and exit the BIOS/UEFI setup.

3. Installing KVM and Required Packages on CentOS

Now that you’ve verified your system meets the requirements, let’s install KVM and the necessary packages:

  • Updating the System

    It’s always a good practice to update your system before installing new software:

    bash
    sudo yum update -y

    Or, if you are using a newer version of CentOS with dnf:
    bash
    sudo dnf update -y

  • Installing KVM Packages

    Install the following packages using yum (or dnf):

    bash
    sudo yum install -y qemu-kvm libvirt virt-install virt-manager bridge-utils

    Or, with dnf:
    bash
    sudo dnf install -y qemu-kvm libvirt virt-install virt-manager bridge-utils

    Let’s break down what each package does:

    • qemu-kvm: The core KVM package, providing the QEMU emulator modified to use KVM acceleration.
    • libvirt: The virtualization management library and daemon (libvirtd). It provides a consistent API for managing various hypervisors, including KVM.
    • virt-install: A command-line tool for creating new virtual machines.
    • virt-manager: (Optional) A graphical user interface (GUI) for managing virtual machines. Highly recommended for beginners.
    • bridge-utils: Utilities for creating and managing network bridges, which are often used for VM networking.
  • Starting and Enabling libvirtd Service

    The libvirtd daemon needs to be running for KVM to function. Start and enable it to run on boot:

    bash
    sudo systemctl start libvirtd
    sudo systemctl enable libvirtd

    Check its status:
    bash
    sudo systemctl status libvirtd

    You should see it is active (running).

  • Verifying Installation

    Verify that the KVM modules are loaded:

    bash
    lsmod | grep kvm

    You should see output similar to:

    kvm_intel 286720 0
    kvm 663552 1 kvm_intel
    irqbypass 13503 1 kvm

    (or kvm_amd instead of kvm_intel if you have an AMD processor).

  • Adding User to libvirt Group

To manage VMs without needing sudo for every command, add your user account to the libvirt group:

```bash
sudo usermod -a -G libvirt $(whoami)
```

You'll need to log out and log back in (or start a new shell) for this change to take effect.  Verify group membership:

```bash
groups
```

4. Creating Your First Virtual Machine (CLI Method)

Let’s create your first VM using the command-line tool virt-install. This method provides more control and flexibility than the GUI method.

  • Understanding virt-install

    virt-install is a powerful command-line utility for provisioning new virtual machines. It uses libvirt to interact with KVM. It takes numerous options to specify the VM’s configuration, including CPU, memory, storage, network, and installation source.

  • Downloading a CentOS ISO Image

    You’ll need an ISO image of the operating system you want to install in your VM. Download a CentOS ISO image from the official CentOS website or a mirror. For example:

    bash
    wget https://example.com/path/to/CentOS-8-x86_64-minimal.iso # Replace with the actual URL

  • Creating a Virtual Disk Image (qemu-img)

    Before creating the VM, you need to create a virtual disk image where the guest OS will be installed. Use the qemu-img command:

    bash
    qemu-img create -f qcow2 /var/lib/libvirt/images/centos8-vm1.qcow2 20G

    • create: Specifies that you want to create a new image.
    • -f qcow2: Specifies the image format. qcow2 (QEMU Copy-On-Write) is a recommended format because it supports features like snapshots and thin provisioning (only allocating space as needed).
    • /var/lib/libvirt/images/centos8-vm1.qcow2: The path and filename of the virtual disk image. /var/lib/libvirt/images/ is the default location for VM images.
    • 20G: The size of the virtual disk image (20 GB in this example).
  • Building the VM with virt-install

    Now, use virt-install to create the VM:

    bash
    virt-install \
    --name centos8-vm1 \
    --ram 2048 \
    --vcpus 2 \
    --disk /var/lib/libvirt/images/centos8-vm1.qcow2,format=qcow2 \
    --cdrom /path/to/CentOS-8-x86_64-minimal.iso \
    --network network=default,model=virtio \
    --graphics vnc,listen=0.0.0.0 \
    --os-variant centos8.0

    Let’s break down the options:

    • --name centos8-vm1: The name of the VM.
    • --ram 2048: The amount of RAM allocated to the VM (in MB).
    • --vcpus 2: The number of virtual CPUs (vCPUs) assigned to the VM.
    • --disk /var/lib/libvirt/images/centos8-vm1.qcow2,format=qcow2: Specifies the virtual disk image to use.
    • --cdrom /path/to/CentOS-8-x86_64-minimal.iso: Specifies the ISO image to use as the installation source. (Replace /path/to/ with the actual path). Alternatively, you can use --location to specify a network installation source (e.g., an HTTP or FTP URL).
    • --network network=default,model=virtio: Configures the network interface. network=default uses the default NAT network provided by libvirt. model=virtio uses a paravirtualized network driver (virtio-net) for better performance.
    • --graphics vnc,listen=0.0.0.0: Enables VNC access to the VM’s console. listen=0.0.0.0 allows connections from any IP address (you might want to restrict this for security).
    • --os-variant centos8.0: Specifies the guest OS variant. This helps virt-install optimize the VM configuration for the specific OS. You can get a list of supported variants with osinfo-query os.
  • Connecting to the VM Console (VNC)

    After running virt-install, the VM will start, and you’ll need to connect to its console to complete the OS installation. Since we used --graphics vnc, you can use a VNC client (like TigerVNC, TightVNC, or Remmina) to connect.

    1. Find the VNC Port: virt-install will usually print the VNC port number to the terminal. You can also find it using virsh:

      bash
      virsh vncdisplay centos8-vm1

      This will output something like :1, which means the VNC port is 5901 (VNC ports start at 5900 and increment).

    2. Connect with a VNC Client: Open your VNC client and connect to the host server’s IP address and the VNC port (e.g., 192.168.1.100:5901).

  • Completing the Operating System Installation

    Once connected via VNC, you’ll see the CentOS installer. Follow the on-screen instructions to complete the installation, just as you would on a physical machine. Make sure to configure networking (if necessary) during the installation.

5. Managing Virtual Machines with virsh (CLI)

virsh is the primary command-line tool for managing virtual machines managed by libvirt. It provides a wide range of commands for controlling and monitoring VMs.

  • Introduction to virsh

    virsh provides a shell-like interface for interacting with libvirt. You can run virsh commands directly, or you can enter the virsh interactive shell by typing virsh and pressing Enter.

  • Listing VMs (virsh list --all)

    To list all VMs, both running and stopped:

    bash
    virsh list --all

    To list only running VMs:

    bash
    virsh list

  • Starting a VM (virsh start)

    To start a stopped VM:

    bash
    virsh start centos8-vm1

  • Stopping a VM (virsh shutdown, virsh destroy)

    To gracefully shut down a VM (sends an ACPI shutdown signal):

    bash
    virsh shutdown centos8-vm1

    To forcefully power off a VM (equivalent to pulling the power plug):

    bash
    virsh destroy centos8-vm1

    Use destroy with caution, as it can lead to data loss if the VM is writing to disk.

  • Suspending and Resuming a VM (virsh suspend, virsh resume)

    To suspend a VM (save its state to RAM and pause it):

    bash
    virsh suspend centos8-vm1

    To resume a suspended VM:

    bash
    virsh resume centos8-vm1

  • Deleting a VM (virsh undefine)

    To delete a VM (remove its definition from libvirt):

    bash
    virsh undefine centos8-vm1

    Important: This command does not delete the virtual disk image. You’ll need to manually delete the image file (e.g., centos8-vm1.qcow2) if you no longer need it. To also remove the associated storage, use the --remove-all-storage flag. This requires additional flags to specify which storage volumes to remove. It’s generally safer to remove the storage manually after undefining the domain.

  • Getting VM Information (virsh dominfo)

    To get detailed information about a VM:

    bash
    virsh dominfo centos8-vm1

    This will display information like the VM’s ID, state, CPU usage, memory usage, and network interfaces.

  • Editing VM Configuration (virsh edit)

    To edit a VM’s configuration (XML file):

    bash
    virsh edit centos8-vm1

    This will open the VM’s XML configuration file in your default text editor (usually vi or nano). You can modify various settings, such as CPU, memory, network, and disk configuration. libvirt will validate the XML file when you save and close it.

  • Creating and Managing Snapshots (virsh snapshot-create-as, virsh snapshot-list, virsh snapshot-revert, virsh snapshot-delete)

Snapshots allow you to save the state of a VM at a specific point in time. This makes it easy to revert to a previous state if something goes wrong.

*   Create a snapshot:
```bash
    virsh snapshot-create-as centos8-vm1 snap1 "Description of snapshot"
```

*    List snapshots:
```bash
virsh snapshot-list centos8-vm1
```

*    Revert to a snapshot:
```bash
    virsh snapshot-revert centos8-vm1 snap1
```

*    Delete a snapshot:
```bash
    virsh snapshot-delete centos8-vm1 snap1
```
  • Autostarting VMs (virsh autostart)

    To configure a VM to start automatically when the host boots:

    bash
    virsh autostart centos8-vm1

    To disable autostart:
    bash
    virsh autostart --disable centos8-vm1

6. Creating and Managing Virtual Machines with virt-manager (GUI)

virt-manager (Virtual Machine Manager) provides a user-friendly graphical interface for managing VMs. It’s a great option for beginners or for those who prefer a visual approach.

  • Launching virt-manager

    You can launch virt-manager from the desktop environment’s application menu (usually under “System Tools” or “Applications”). You can also launch it from the terminal:

    bash
    virt-manager

  • Connecting to the Local libvirtd Daemon

    By default, virt-manager will connect to the local libvirtd daemon (QEMU/KVM). You can also connect to remote libvirtd daemons (e.g., on another server).

  • Creating a New VM (Wizard-based)

    1. Click the “Create a new virtual machine” button (looks like a computer with a plus sign).
    2. Choose Installation Media:
      • Local install media (ISO image or CDROM): Select this option and browse to your CentOS ISO image.
      • Network Install (HTTP, FTP, or NFS): Enter the URL of a network installation source.
      • Import existing disk image: Use this option if you have a pre-existing virtual disk image.
      • Manual Install: Offers a very high degree of control, allowing custom partitioning, bootloader selection, etc.
    3. Choose the OS type and version. virt-manager will try to auto-detect the OS from the ISO, but you can manually select it.
    4. Configuring CPU and Memory: Specify the number of vCPUs and the amount of RAM to allocate to the VM.
    5. Creating or Selecting Storage:
      • Create a disk image for the virtual machine: This is the default option. Enter the desired size of the disk image.
      • Select or create custom storage: Choose an existing virtual disk image or create a new one in a specific location.
    6. Network Configuration:
      • Virtual network ‘default’: NAT: Uses the default NAT network.
      • Bridged network: Select a bridge interface to connect the VM directly to your physical network.
    7. Reviewing and Starting the VM: Review the VM configuration and click “Finish” to create and start the VM.
  • Managing VMs

    Once the VM is created, you can manage it from the virt-manager window:

    • Starting, Stopping, Pausing, and Resuming: Use the buttons in the toolbar or the right-click context menu.
    • Viewing VM Details and Performance: Double-click the VM to open its details window. You can view information about the VM’s hardware, resource usage, and performance graphs.
    • Accessing the VM Console: Click the “Open” button (or double-click the VM) to open a console window (usually using VNC or SPICE).
    • Adding and Removing Hardware: In the VM details window, you can add or remove virtual hardware, such as network interfaces, disks, and USB devices.
    • Creating and Managing Snapshots: Click the “Take Snapshot” button (camera icon) to create a snapshot. You can manage snapshots from the “Snapshots” tab in the VM details window.

7. Networking with KVM

Networking is a crucial aspect of virtualization. KVM and libvirt provide several options for connecting your VMs to the network.

  • Understanding Virtual Networking Concepts

    • Virtual Networks: libvirt creates virtual networks that act as virtual switches. VMs can connect to these virtual networks.
    • Network Bridges: A network bridge connects a virtual network to a physical network interface on the host. This allows VMs to communicate directly with the physical network, obtaining IP addresses from a DHCP server on the network.
    • NAT (Network Address Translation): NAT allows VMs to share the host’s IP address. The host acts as a router, translating traffic between the VMs and the external network. This is useful when you don’t have enough IP addresses for each VM or when you want to isolate VMs from the physical network.
    • Routed Networks: Similar to bridged networks, but traffic is routed between subnets instead of bridged. Requires more complex configuration.
    • Isolated Networks: VMs on an isolated network can communicate with each other, but they cannot access the external network or the host.
  • Default Network (default)

    libvirt creates a default NAT network called default during installation. This network provides basic network connectivity for VMs using NAT. You can inspect its configuration with:

    bash
    virsh net-info default

    And view the XML configuration:
    bash
    virsh net-dumpxml default

  • Creating a Bridged Network
    Bridged networking is generally preferred when VMs require direct access to the physical network.

    • Using nmcli (NetworkManager CLI)

      CentOS uses NetworkManager to manage network connections. The nmcli command-line tool is the preferred way to configure networking.

      1. Identify your physical network interface:

        bash
        nmcli device status

        This will list your network interfaces (e.g., enp0s3, eth0).

      2. Create the bridge interface:

        bash
        sudo nmcli connection add type bridge con-name br0 ifname br0

      3. Configure the bridge IP address (optional – for static IP): If you want to assign a static IP address to the bridge interface, use:

        bash
        sudo nmcli connection modify br0 ipv4.addresses '192.168.1.101/24' # Replace with your desired IP and subnet
        sudo nmcli connection modify br0 ipv4.gateway '192.168.1.1' # Replace with your gateway
        sudo nmcli connection modify br0 ipv4.dns '8.8.8.8,8.8.4.4' # Replace with your DNS servers
        sudo nmcli connection modify br0 ipv4.method manual

        If using DHCP, omit these steps; the bridge will get an IP from your DHCP server.

      4. Connect the physical interface to the bridge:

        bash
        sudo nmcli connection add type bridge-slave ifname enp0s3 master br0 #Replace enp0s3

        5. Deactivate the original connection for your physical interface:

      bash
      sudo nmcli connection down enp0s3 # Replace with your interface name

      1. Activate the bridge:

        bash
        sudo nmcli connection up br0

        7. Verify the bridge configuration:

        “`bash

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top