Install LXD pure-container hypervisor on Ubuntu 18.04 LTS

last updated in Categories , ,

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 install LXD pure-container hypervisor on Ubuntu 18.04 LTS

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:
Setup LXD
LXD is now set up and configured. You can verify it with the following command:
$ lxc list
$ lxc info | more

Verify the LXD server running

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

LXD list images

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
LXD running Alpine Linux 3.8

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

LXD list containers using lxc command
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

Conclusion

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:

This entry is 13 of 13 in the LXD Tutorial series. Keep reading the rest of the series:
  1. How to install LXD container hypervisor on Ubuntu 16.04 LTS
  2. How to install and setup LXC (Linux Container) on Fedora Linux 26
  3. How to install LXD container under KVM or Xen virtual machine
  4. How to list VM images in LXD (Linux Containers)
  5. How to upgrade LXD VMs powered by Ubuntu/Debian or CentOS Linux
  6. How to auto start LXD containers at boot time in Linux
  7. How to rename LXD / LXC container
  8. How to run commands on Linux Container (LXD) instance at provision launch time
  9. How to use LXD (Linux containers) in a shell script to create VM when the cloud instance launches
  10. How to move/migrate LXD VM to another host on Linux
  11. How to install and setup LXD on Fedora Linux server
  12. How to install and setup LXD on CentOS Linux 7.x server
  13. Install LXD pure-container hypervisor on Ubuntu 18.04 LTS

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.

Notable Replies

  1. [quote]It would be nice if you also could add some information about how to move the default store …
    I’ve done it, but it was not a simple task… which is one of the reasons why I’m still using lxc …
    [/quote]

    When you run sudo lxd init you can always change storage type. It is possible to move after installation as well but it needs more work.

Continue the discussion www.nixcraft.com

2 more replies

Participants