Skip to content

Instantly share code, notes, and snippets.

@jasonacox
Last active February 16, 2026 20:47
Show Gist options
  • Select an option

  • Save jasonacox/955b9f44c33ee035d75ded6fcec50f40 to your computer and use it in GitHub Desktop.

Select an option

Save jasonacox/955b9f44c33ee035d75ded6fcec50f40 to your computer and use it in GitHub Desktop.
Set up Ubuntu VM on Ubuntu 24.04 via CLI

Manual Ubuntu VM Setup with LAN Networking

Description

This guide shows how to install Ubuntu VMs on an Ubuntu 24.04 host using only command line options with virt-manager. By default, virt-manager wants to use a GUI installer, but we'll use the virt-install command line tool instead to create VMs that connect directly to your LAN via a network bridge.

What you'll learn:

  • Create a persistent network bridge for LAN access
  • Install Ubuntu VMs using serial console (no GUI needed)
  • Manage VMs with virsh commands
  • VMs will appear as separate devices on your network with their own IP addresses

TL;DR - Quick VM with NAT (No Bridge Setup)

Want to create a VM quickly without setting up bridging? Use this command after installing packages:

# Install packages first
sudo apt update
sudo apt install -y virt-manager libvirt-daemon-system libvirt-clients bridge-utils qemu-kvm

# Create VM with NAT (uses default virbr0)
sudo virt-install \
  --name my-vm \
  --disk path=/vm/my-vm.qcow2,size=20,bus=virtio,format=qcow2 \
  --os-variant ubuntu20.04 \
  --vcpus 2 \
  --ram 2048 \
  --location http://ftp.ubuntu.com/ubuntu/dists/focal/main/installer-amd64/ \
  --network bridge=virbr0,model=virtio \
  --graphics none \
  --extra-args='console=ttyS0,115200n8 serial'

Follow the installer prompts, then get the IP with: sudo virsh net-dhcp-leases default

For LAN access (VMs get IPs from your router), continue reading below.


Quick Start

Install Required Packages

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

Add your user to the libvirt group:

sudo usermod -aG libvirt $USER

Important: Log out and log back in after this command.


Part 1: Create Network Bridge (for LAN Access)

If you want your VMs to get IP addresses from your LAN DHCP server (instead of NAT), you need to create a network bridge first. Skip to Part 2 if you're okay with VMs using NAT (virbr0).

Step 1: Identify Your Network Interface

Find your active network interface:

ip link
# Or using NetworkManager
nmcli device status

Look for your physical interface (e.g., enp5s0, eth0, eno1). Note the name for later steps.

Step 2: Check Current Connection

nmcli connection show --active

Note the connection name associated with your physical interface (e.g., "Wired connection 2").

Step 3: Create Bridge Connection

Replace br0 with your desired bridge name and configure for DHCP:

sudo nmcli connection add type bridge ifname br0 con-name br0 \
  ipv4.method auto ipv6.method auto connection.autoconnect yes

Step 4: Create Slave Connection

Replace enp5s0 with your physical interface name:

sudo nmcli connection add type ethernet ifname enp5s0 \
  con-name enp5s0-to-br0 master br0

Step 5: Activate the Bridge

Warning: This will cause a brief network interruption (5-10 seconds).

First, bring down your existing connection (replace name):

sudo nmcli connection down "Wired connection 2"

Then bring up the bridge:

sudo nmcli connection up br0

Step 6: Wait for DHCP Lease

Wait 5-15 seconds for DHCP to assign an IP:

# Watch for IP assignment
watch -n 1 ip addr show br0

# Or check periodically
ip addr show br0 | grep "inet "

Step 7: Verify Bridge Configuration

Check bridge status:

ip addr show br0

You should see an IP address (e.g., inet 10.0.1.93/24).

Verify connectivity:

ping -c 3 8.8.8.8

Check active connections:

nmcli connection show --active | grep -E "(br0|enp5s0)"

You should see:

  • br0 connection on br0 device (bridge)
  • enp5s0-to-br0 connection on enp5s0 device (ethernet)

Bridge setup is complete! This configuration persists across reboots.


Part 2: Create Ubuntu VM

Network Mode: Bridge vs NAT

Before creating your VM, decide which networking mode to use:

Bridge Mode (br0) - Recommended if you completed Part 1

  • VMs get IP addresses from your LAN DHCP
  • VMs appear as separate devices on your network
  • Easier to access from other devices
  • Use --network bridge=br0 in virt-install command

NAT Mode (virbr0) - Default, no setup needed

  • VMs use libvirt's built-in NAT network
  • VMs get 192.168.122.x addresses
  • VMs can access internet but are isolated from LAN
  • Use --network bridge=virbr0 in virt-install command

The examples below use bridge=br0 (LAN mode). Replace with bridge=virbr0 for NAT mode.

Step 1: Install Required Packages

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

Step 2: Add User to libvirt Group

sudo usermod -aG libvirt $USER

Important: Log out and log back in for group changes to take effect.

Verify:

groups | grep libvirt

Step 3: Download Ubuntu ISO

For Ubuntu 24.04.4:

cd /vm
curl -L -o ubuntu-24.04.4-live-server-amd64.iso \
  https://releases.ubuntu.com/24.04/ubuntu-24.04.4-live-server-amd64.iso

This will take several minutes (~2-3 GB download).

Verify download:

ls -lh ubuntu-24.04.4-live-server-amd64.iso
file ubuntu-24.04.4-live-server-amd64.iso

File size should be around 2.5-3 GB and type should be "ISO 9660".

Step 4: Verify Bridge Exists

Before creating the VM, confirm the bridge is up:

ip addr show br0 | grep "inet "

You should see an IP address.

Step 5: Create the Virtual Machine

Run the virt-install command with your desired settings:

sudo virt-install \
  --name openclaw-vm \
  --disk path=/vm/openclaw-vm.qcow2,size=20,bus=virtio,format=qcow2 \
  --os-variant ubuntu24.04 \
  --vcpus 2 \
  --ram 2048 \
  --location /vm/ubuntu-24.04.4-live-server-amd64.iso,kernel=casper/vmlinuz,initrd=casper/initrd \
  --network bridge=br0,model=virtio \
  --graphics none \
  --console pty,target_type=serial \
  --extra-args='console=ttyS0,115200n8 serial'

Parameters explained:

  • --name openclaw-vm - VM name (change as desired)
  • --disk path=/vm/openclaw-vm.qcow2,size=20 - Disk location and size (20 GB)
  • --os-variant ubuntu24.04 - OS type for optimization
  • --vcpus 2 - Number of CPU cores
  • --ram 2048 - RAM in MB (2 GB)
  • --location - ISO path with kernel/initrd locations for console install
  • --network bridge=br0 - Connect to your bridge (use LAN DHCP)
  • --graphics none - No GUI, serial console only
  • --console pty,target_type=serial - Use serial console
  • --extra-args - Kernel arguments for serial console

Step 6: Complete Ubuntu Installation

A text-based interactive wizard will launch in your terminal. Follow the prompts:

  1. Language selection - Choose your language
  2. Keyboard layout - Select keyboard
  3. Network configuration - Should auto-detect DHCP from your LAN (or NAT if using virbr0)
  4. Ubuntu archive mirror - Use default
  5. Storage configuration - Use entire disk (the virtual disk)
Ubuntu installer disk setup screen
  1. Profile setup - Create username/password. Important: Make sure you set this up so you can SSH into the server later.
  2. SSH setup - Recommended: Install OpenSSH server so you can access the VM remotely
  3. Featured snaps - Select any additional software

The installation will continue and download/install packages. This takes a bit of time:

Ubuntu installing and downloading packages

Make sure to select the OpenSSH server when prompted for additional packages:

OpenSSH server selection

Installation will take 10-20 minutes total. When complete, the VM will reboot.

Step 7: Disconnect from Console

After installation and reboot, press Ctrl + ] to disconnect from the VM console.

Step 8: Get VM IP Address

If using LAN bridge (br0):

Get the VM's IP address:

sudo virsh domifaddr openclaw-vm

Or check your router's DHCP leases - the VM will appear as a separate device on your LAN.

If using default NAT (virbr0):

sudo virsh net-dhcp-leases default

This will show you the local IP address assigned by libvirt (usually 192.168.122.x).

Step 9: Verify VM Status

Check running VMs:

sudo virsh list --all

You should see your VM in "running" state.

Step 10: Connect to VM

Via SSH (recommended):

ssh username@192.168.122.12

Replace with your VM's IP address and the username you created during installation.

Via console:

sudo virsh console openclaw-vm

(Press Ctrl + ] to exit)

Via SSH (if installed):

ssh username@<vm-ip-address>

Part 3: Manage VMs

Start a VM

sudo virsh start openclaw-vm

Shutdown a VM

sudo virsh shutdown openclaw-vm

Force stop a VM

sudo virsh destroy openclaw-vm

(Don't worry - "destroy" just means force stop, not delete)

Connect to console

sudo virsh console openclaw-vm

List all VMs

sudo virsh list --all

Get VM info

sudo virsh dominfo openclaw-vm

Check VM network

sudo virsh domifaddr openclaw-vm

Part 4: Remove a VM

Quick Delete (Simple Method)

To quickly shutdown and remove a VM:

sudo virsh destroy openclaw-vm
sudo virsh undefine openclaw-vm --remove-all-storage
sudo virsh list --all

This will:

  1. Force stop the VM (destroy)
  2. Remove the VM definition and all storage (undefine --remove-all-storage)
  3. List VMs to verify removal

Detailed Steps (if you need more control)

Step 1: Stop the VM

Check if running:

sudo virsh list --state-running

If running, stop it:

sudo virsh destroy openclaw-vm

Step 2: Undefine the VM

Remove the VM definition:

sudo virsh undefine openclaw-vm --remove-all-storage

The --remove-all-storage flag removes associated storage automatically.

Step 3: Manually Remove Disk (if needed)

If the disk file still exists:

ls -lh /vm/openclaw-vm.qcow2
sudo rm /vm/openclaw-vm.qcow2

Step 4: Verify Removal

sudo virsh list --all

The VM should no longer appear in the list.


Part 5: Revert Network Bridge (Optional)

If you need to remove the bridge and return to direct connection:

Step 1: Bring Down Bridge

sudo nmcli connection down br0

Step 2: Bring Up Original Connection

Replace with your original connection name:

sudo nmcli connection up "Wired connection 2"

Step 3: Delete Bridge Connections (Optional)

If you want to completely remove the bridge configuration:

sudo nmcli connection delete br0
sudo nmcli connection delete enp5s0-to-br0

Troubleshooting

Bridge has no IP after activation

Wait longer for DHCP:

# Wait and check every few seconds
for i in {1..15}; do 
  ip addr show br0 | grep "inet " && break
  echo "Waiting for DHCP... ($i/15)"
  sleep 1
done

Restart bridge manually:

sudo nmcli connection down br0 && sudo nmcli connection up br0

VM installation fails with "ERROR validating location"

  • Check ISO path is correct: ls -lh /vm/*.iso
  • Verify ISO isn't corrupted: file /vm/*.iso (should be "ISO 9660")
  • For Ubuntu 24.04+, ensure you use the --location format with kernel=casper/vmlinuz,initrd=casper/initrd

Cannot connect to VM serial console

Try:

sudo virsh console openclaw-vm --force

Or check VM is running:

sudo virsh list --all

VM not getting IP address

Check VM is using correct bridge:

sudo virsh domiflist openclaw-vm

Should show br0 as the source. If not, you may need to recreate the VM with correct --network parameter.

Permission denied errors

Ensure you're in the libvirt group:

groups | grep libvirt

If not:

sudo usermod -aG libvirt $USER

Then log out and back in.


Tips and Variations

Use Static IP Instead of DHCP

In Step 3 of bridge creation, use:

sudo nmcli connection add type bridge ifname br0 con-name br0 \
  ipv4.method manual ipv4.addresses 192.168.1.100/24 ipv4.gateway 192.168.1.1 \
  ipv4.dns "8.8.8.8,8.8.4.4" ipv6.method auto connection.autoconnect yes

Create Larger VM Disk

Change size=20 to desired size in GB:

--disk path=/vm/openclaw-vm.qcow2,size=100,bus=virtio,format=qcow2

Allocate More Resources

--vcpus 4 \
--ram 8192 \

Use Different Ubuntu Version

For Ubuntu 20.04 (Focal), which has traditional debian-installer:

sudo virt-install \
  --name focal-vm \
  --disk path=/vm/focal-vm.qcow2,size=20,bus=virtio,format=qcow2 \
  --os-variant ubuntu20.04 \
  --vcpus 2 \
  --ram 2048 \
  --location http://ftp.ubuntu.com/ubuntu/dists/focal/main/installer-amd64/ \
  --network bridge=br0,model=virtio \
  --graphics none \
  --extra-args='console=ttyS0,115200n8 serial'

No ISO download needed - installs directly from network!

Clone Existing VM

sudo virt-clone --original openclaw-vm --name openclaw-vm-clone \
  --file /vm/openclaw-vm-clone.qcow2

Summary

This guide covered:

  1. ✓ Creating a persistent network bridge with NetworkManager
  2. ✓ Installing virtualization packages
  3. ✓ Downloading Ubuntu ISO
  4. ✓ Creating and installing Ubuntu VM with serial console
  5. ✓ Managing VMs (start/stop/console)
  6. ✓ Removing VMs and cleaning up
  7. ✓ Troubleshooting common issues

Your VM now has full LAN access via the bridge and will receive DHCP addresses from your router, appearing as a separate device on your network.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment