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:
- Run containers
- Update containers
- Clustering support
- Install different Linux distro inside containers
- 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:
- Install lxd
- Setup and configure LXD networking and storage
- Create your first container
- 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 to set up the LXD server. You must configure networking and storage option such as directory, ZFS, Btrfs 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 16.04 or 18.04 or 20.04 LTS container
$ lxc launch images:ubuntu/bionic/amd64 ubuntu-nginx-c3
$ lxc launch images:ubuntu/eoan/amd64 ubuntu-nginx-c4
$ lxc launch images:ubuntu/focal/amd64 ubuntu-nginx-c5
Debian Linux 9.x “stretch” or 10.x container
$ lxc launch images:debian/stretch/amd64 debian9-c4
OR
$ lxc launch images:debian/10/amd64 debian10-www01
Arch Linux container
$ lxc launch images:archlinux/amd64 arch-c5
Gentoo Linux container
$ lxc launch images:gentoo/amd64 gentoo-c6
Fedora Linux 30 container
$ lxc launch images:fedora/30/amd64 fedora30-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 -i ubuntu
$ lxc list "*c1*"
$ lxc list "*c2*"
$ lxc list
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
$ lxc start cenots-c2 opensuse-stable-c8
How do I stop my containers?
Stop containers using the following syntax:
$ lxc stop containerName
$ lxc stop foo
$ lxc 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
$ lxc 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
$ lxc 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:
- Install LXD container hypervisor on Ubuntu 16.04 LTS
- How to install and setup LXC (Linux Container) on Fedora Linux 26
- Set up LXD container under KVM or Xen virtual machine
- List VM images in LXD (Linux Containers)
- Upgrade LXD containers powered by Ubuntu/Debian or CentOS Linux
- Auto start LXD containers at boot time in Linux
- Command to rename LXD / LXC container
- Run commands on Linux Container (LXD) instance at provision launch time
- Use LXD (Linux containers) in a shell script to create VM when the cloud instance launches
- Move/migrate LXD VM to another host on Linux
- Fedora install and set up LXD
- CentOS 7.x install and set up LXD server
- Install LXD pure-container hypervisor on Ubuntu 18.04 LTS
- Create snapshots with lxc command for LXD
- Set up and install LXD on CentOS/RHEL 8
- Ubuntu 20.04 LTS install and set up LXD
- Full backup and restore LXD containers
- Disable firewall and NAT rules on the LXD bridge
- Delete or remove LXD container using the lxc
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 0 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |