Install LXD pure-container hypervisor on Ubuntu 18.04 LTS nixCraft

Spread the love

How can I install LXD pure-container hypervisor on Ubuntu 18.04 LTS to runs unmodified Debian/Ubuntu/CentOS Linux operating systems VM at incredible speed? How do I setup and use LXD on Ubuntu Linux server?

Introduction: The LXD container hypervisors are supported and created by Ubuntu team. It provides 25% faster performance than ESX. 10 times the density with zero latency. One can move Linux VMs straight to containers using LXD without modifying the apps or administration processes. Ubuntu’s LXD is a pure-container hypervisor that runs unmodified Linux guest operating systems with VM-style operations at incredible speed. You can run CentOS, Arch Linux, Fedora Linux, OpenSUSE, Gentoo, Ubuntu, Debian and other Linux distro in containers. This page shows how to install and configure LXD pure-container hypervisor on Ubuntu Linux 18.04 LTS.

Why use LXD?

LXD is a container hypervisor providing a ReST API to manage LXC containers. One can do the following things with LXD:

  1. Run containers
  2. Update containers
  3. Clustering support
  4. Install different Linux distro inside containers
  5. Manage container resources, like storage volumes and networks

Setting up LXD on Ubuntu 18.04 LTS

The procedure to set up and use LXD on Ubuntu Linux 18.04 LTS is as follows:

  1. Install lxd
  2. Setup and configure LXD networking and storage
  3. Create your first container
  4. Launch a container

Let us see all steps in details.

Install LXD pure-container hypervisor on Ubuntu 18.04 LTS

Type the following apt-get command or apt command to LXD:
$ sudo sh -c 'apt update && apt upgrade'
$ sudo apt install lxd

How to add user to the LXD group for management purpose

No need to be the root user to manage LXD daemon. To manage LXD server add your username to lxd group using the adduser command on Ubuntu Linux:
$ sudo adduser {USERNameHere} lxd
$ sudo adduser vivek lxd

Please note that group membership is only applied at login or new ssh session, you then either need to close and re-open your user/ssh session or use the following command to avoid problems:
$ newgrp lxd
Verify it with the id command:
$ id

Setup ZFS (optional)

I suggest you use ZFS and along with DHCP based IP networking for ease of setup for each VM. The ZFS offers quick option to make snapshots and other advanced features:
$ sudo apt install zfsutils-linux
See how to install ZFS on Ubuntu server for more information.

Configure LXD storage and networking option

It is time set up the LXD server. You must configure networking and storage option such as directory, zfs, btrf and more:
$ sudo lxd init
You must answer a series of questions on how to configure the LXD server. Here is a sample session:

LXD is now set up and configured. You can verify it with the following command:
$ lxc list
$ lxc info | more

Creating your first Linux container

To list all LXD images run:
$ lxc image list images:
$ lxc image list images: | grep -i centos
$ lxc image list images: | grep -i ubuntu
$ lxc image list images: | grep -i debian

How create and use your first container on Ubuntu 18.04 LTS running LXD

To create and start containers from images use the launch command:
lxc launch images:{distro}/{version}/{arch} {container-name-here}
Let us see some examples to create and start containers from various Linux distro images as per your needs.

Alpine Linux 3.8 VM

$ lxc launch images:alpine/3.8/amd64 alpine-c1

CentOS Linux 7 container

$ lxc launch images:centos/7/amd64 cenots-c2

Ubuntu Linux 18.04 LTS container

$ lxc launch images:ubuntu/bionic/amd64 ubuntu-nginx-c3

Debian Linux 9.x “stretch” container

$ lxc launch images:debian/stretch/amd64 debian9-c4

Arch Linux container

$ lxc launch images:archlinux/amd64 arch-c5

Gentoo Linux container

$ lxc launch images:gentoo/amd64 gentoo-c6

Fedora Linux 28 container

$ lxc launch images:fedora/28/amd64 fedora28-c7

OpenSUSE Linux 15.0 container

$ lxc launch images:opensuse/15.0/amd64 opensuse-stable-c8

Oracle Linux 7.x VM

$ lxc launch images:oracle/7/amd64 oracle7-c9

How do I list all my containers?

List the existing containers:
$ lxc list --fast
$ lxc list | grep RUNNING
$ lxc list | grep STOPPED
$ lxc list | grep
$ lxc list "*c1*"
$ lxc list "*c2*"
$ lxc list

Click to enlarge it

How to execute/run the specified command in a container

To run or execute commands in containers use exec command:
lxc exec containerName -- command
lxc exec containerName -- /path/to/script
lxc exec containerName --env EDITOR=/usr/bin/vim -- command
### run date, ip a, ip rm and other commands on various containers ###
$ lxc exec cenots-c2 -- date
$ lxc exec cenots-c2 -- ip a
$ lxc exec ubuntu-nginx-c3 -- ip r
$ lxc exec fedora28-c7-- dnf update
$ lxc exec debian9-c4 -- cat /etc/debian_version

How to get the bash shell access in a container

To gain login and gain shell access in a container named file-server , enter:
$ lxc exec debian9-c4 bash
Now you can run commands or install packages:
# cat /etc/*issue*
# apt-get update
# apt-get upgrade

To exit simply from container simply type exit:
# exit

How do I start my containers?

Start containers using the following cli:
$ lxc start containerName
$ lxc start foo
$ lsc start cenots-c2 opensuse-stable-c8

How do I stop my containers?

Stop containers using the following syntax:
$ lxc stop containerName
$ lxc stop foo
$ lsc stop cenots-c2 opensuse-stable-c8

How do I restart my containers?

Want to restart your containers for any reasons? Try:
$ lxc restart containerName
$ lxc restart foo
$ lsc restart cenots-c2 opensuse-stable-c8

How do I delete my containers?

The command is (be careful as the LXD containers are deleted immediately without any confirmation prompt i.e. keep backups):
$ lxc delete containerName
$ lxc delete foo
$ lsc delete cenots-c2

You may get the following error while deleting the container:

The container is currently running, stop it first or pass –force.

To fix this:
$ lxc stop cenots-c2 && lxc delete cenots-c2

How do I show information on LXD servers and containers?

Type the following command:
$ lxc info
$ lxc info containerName
$ lxc info opensuse-stable-c8


And, there you have install lxd on Ubuntu 18.04, up and running with Linux distro of your choice. See the following urls for more info:

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Posted by News Monkey