Let’s learn to install Install Ubuntu on LXC. Containers are one of the most instrumental and useful tools at our disposal. On Linux, we have LXC containers, which are super easy to setup and use. They allow us to use multiple operating systems on a single host in a way without the need for an hypervisor. in this module, we’ll learn how to set up an Ubuntu Server image in a LXC container using some very easy steps !
Steps to Install Ubuntu on LXC
Let’s get right into the steps to get an Ubuntu image on LXC.
1. Prerequisites
Before we begin, update and upgrade your system with the apt command:
$ sudo apt update && sudo apt upgrade -y
Then install the required packages with:
$ sudo apt install lxc lxd zfsutils-linux
However, in case of some Distributions, you might need to install the LXD package from the snap store with:
$ sudo snap install lxd
Now that we have all our required dependencies, we can continue with our setup!
2. Adding User To LXC Group
To allow your user to access the LXD daemon locally, it must be part of the lxd
group. You can check if your current user is in the mentioned group by typing:
$ id
uid=1000(mint) gid=1000(mint) groups=1000(mint),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),130(sambashare)
You can add the desired user to the lxd
group with the adduser command:
$ sudo adduser <USER> lxd
For the changes to take effect , you might need to restart your computer. Finally, we can verify if our user has been added to the group with:
$ id
uid=1000(mint) gid=1000(mint) groups=1000(mint),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),130(sambashare),999(lxd)
Now that we can locally access the lxd daemon we can go ahead and set up our containers!
3. Setting Up LXD
To set up the LXD daemon we need to type in:
$ lxd init
This should lead to a series of questions which are used to configure the daemon. For now, just hit enter and go with all the default ones.
$ lxd init
Would you like to use LXD clustering? (yes/no) [default=no]:
Do you want to configure a new storage pool? (yes/no) [default=yes]:
Name of the new storage pool [default=default]:
Name of the storage backend to use (btrfs, dir, lvm, zfs) [default=zfs]:
Create a new ZFS pool? (yes/no) [default=yes]:
Would you like to use an existing block device? (yes/no) [default=no]:
Size in GB of the new loop device (1GB minimum) [default=15GB]:
Would you like to connect to a MAAS server? (yes/no) [default=no]:
Would you like to create a new local network bridge? (yes/no) [default=yes]:
What should the new bridge be called? [default=lxdbr0]:
What IPv4 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]:
What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]:
Would you like LXD to be available over the network? (yes/no) [default=no]:
Would you like stale cached images to be updated automatically? (yes/no) [default=yes]
Would you like a YAML "lxd init" preseed to be printed? (yes/no) [default=no]:
Now we can go ahead and install Ubuntu server image in our container.
4. Install Ubuntu on LXC (Linux Container)
Next up, we need to list all available Ubuntu images available at our disposal with :
$ lxc image list images:ubuntu
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ALIAS | FINGERPRINT | PUBLIC | DESCRIPTION | ARCH | SIZE | UPLOAD DATE |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/14.04 (7 more) | 206eb037c726 | yes | Ubuntu trusty amd64 (20210426_07:42) | x86_64 | 75.58MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/14.04/arm64 (3 more) | 8cfec06e99b9 | yes | Ubuntu trusty arm64 (20210426_08:17) | aarch64 | 69.67MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/14.04/armhf (3 more) | 070778ecdb5c | yes | Ubuntu trusty armhf (20210426_08:13) | armv7l | 70.33MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/14.04/i386 (3 more) | 713d5412e372 | yes | Ubuntu trusty i386 (20210426_07:42) | i686 | 73.77MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/14.04/ppc64el (3 more) | 1ca3bb8e252c | yes | Ubuntu trusty ppc64el (20210426_07:42) | ppc64le | 74.40MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/16.04 (7 more) | 98ab2293381b | yes | Ubuntu xenial amd64 (20210426_07:42) | x86_64 | 83.31MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/16.04/arm64 (3 more) | f0929ebc6a36 | yes | Ubuntu xenial arm64 (20210426_08:31) | aarch64 | 75.57MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/16.04/armhf (3 more) | 8fad9647d7fa | yes | Ubuntu xenial armhf (20210426_08:03) | armv7l | 76.40MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/16.04/cloud (3 more) | 305d675ede12 | yes | Ubuntu xenial amd64 (20210426_07:42) | x86_64 | 102.54MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/16.04/cloud/arm64 (1 more) | 7dfb2383b7df | yes | Ubuntu xenial arm64 (20210426_08:03) | aarch64 | 94.06MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/16.04/cloud/armhf (1 more) | 5901500f1774 | yes | Ubuntu xenial armhf (20210426_07:59) | armv7l | 94.84MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/16.04/cloud/i386 (1 more) | f82b6db381b3 | yes | Ubuntu xenial i386 (20210426_07:42) | i686 | 102.69MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/16.04/cloud/ppc64el (1 more) | cb3e1afc0ba5 | yes | Ubuntu xenial ppc64el (20210426_07:59) | ppc64le | 100.74MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/16.04/cloud/s390x (1 more) | 85e0a8b3f5da | yes | Ubuntu xenial s390x (20210426_07:50) | s390x | 77.78MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/16.04/i386 (3 more) | e96f698aa6cd | yes | Ubuntu xenial i386 (20210426_07:42) | i686 | 83.12MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/16.04/ppc64el (3 more) | 62c2b64162de | yes | Ubuntu xenial ppc64el (20210426_07:59) | ppc64le | 81.54MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/16.04/s390x (3 more) | 9f47976f2914 | yes | Ubuntu xenial s390x (20210426_07:42) | s390x | 59.53MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/18.04 (7 more) | 8b4eeb802ff5 | yes | Ubuntu bionic amd64 (20210426_07:42) | x86_64 | 98.57MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/18.04/arm64 (3 more) | 2705ccf08122 | yes | Ubuntu bionic arm64 (20210426_07:42) | aarch64 | 89.77MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/18.04/armhf (3 more) | cf8e11bdb0cc | yes | Ubuntu bionic armhf (20210426_08:25) | armv7l | 89.74MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/18.04/cloud (3 more) | bcc0425bfe04 | yes | Ubuntu bionic amd64 (20210426_07:42) | x86_64 | 109.45MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/18.04/cloud/arm64 (1 more) | c150378cd2f0 | yes | Ubuntu bionic arm64 (20210426_08:32) | aarch64 | 100.08MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/18.04/cloud/armhf (1 more) | 9931b6542e8d | yes | Ubuntu bionic armhf (20210426_08:31) | armv7l | 99.86MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/18.04/cloud/i386 (1 more) | 5c73a72e09a9 | yes | Ubuntu bionic i386 (20210426_07:42) | i686 | 108.58MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/18.04/cloud/ppc64el (1 more) | 4fdb34bb3397 | yes | Ubuntu bionic ppc64el (20210426_07:42) | ppc64le | 110.07MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/18.04/cloud/s390x (1 more) | 7d126a22814c | yes | Ubuntu bionic s390x (20210426_07:50) | s390x | 81.22MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/18.04/i386 (3 more) | d51c526ad6c2 | yes | Ubuntu bionic i386 (20210426_07:42) | i686 | 97.75MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/18.04/ppc64el (3 more) | 9879ee7c311b | yes | Ubuntu bionic ppc64el (20210426_07:42) | ppc64le | 98.71MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/18.04/s390x (3 more) | da9ff3db9362 | yes | Ubuntu bionic s390x (20210426_07:42) | s390x | 71.03MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/20.10 (7 more) | 1839820c186a | yes | Ubuntu groovy amd64 (20210426_07:42) | x86_64 | 103.77MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/20.10/arm64 (3 more) | 16fb45a4596d | yes | Ubuntu groovy arm64 (20210426_08:15) | aarch64 | 99.16MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/20.10/armhf (3 more) | 3fe35efa8a56 | yes | Ubuntu groovy armhf (20210426_08:16) | armv7l | 96.61MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/20.10/cloud (3 more) | 42deda3e1f71 | yes | Ubuntu groovy amd64 (20210426_07:42) | x86_64 | 120.26MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/20.10/cloud/arm64 (1 more) | e69a15d157d9 | yes | Ubuntu groovy arm64 (20210426_08:31) | aarch64 | 115.12MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/20.10/cloud/armhf (1 more) | 53a84b64afc5 | yes | Ubuntu groovy armhf (20210426_08:17) | armv7l | 112.31MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/20.10/cloud/ppc64el (1 more) | fe884af581b2 | yes | Ubuntu groovy ppc64el (20210426_07:42) | ppc64le | 125.11MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/20.10/cloud/s390x (1 more) | efb0ebb7e4a1 | yes | Ubuntu groovy s390x (20210426_07:42) | s390x | 98.27MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/20.10/ppc64el (3 more) | 159366ee16d2 | yes | Ubuntu groovy ppc64el (20210426_07:42) | ppc64le | 107.61MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/20.10/s390x (3 more) | c305f9be891f | yes | Ubuntu groovy s390x (20210426_07:42) | s390x | 82.56MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/21.04 (7 more) | 2aebf4148b54 | yes | Ubuntu hirsute amd64 (20210426_07:42) | x86_64 | 104.73MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/21.04/arm64 (3 more) | 7f63017f49d9 | yes | Ubuntu hirsute arm64 (20210426_07:42) | aarch64 | 100.38MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/21.04/armhf (3 more) | 8330c0dabd40 | yes | Ubuntu hirsute armhf (20210426_07:42) | armv7l | 98.07MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/21.04/cloud (3 more) | 77fcbd9cbe96 | yes | Ubuntu hirsute amd64 (20210426_07:42) | x86_64 | 121.15MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/21.04/cloud/arm64 (1 more) | 6c0a8af1d2a1 | yes | Ubuntu hirsute arm64 (20210426_07:42) | aarch64 | 116.20MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/21.04/cloud/armhf (1 more) | 502474d0068d | yes | Ubuntu hirsute armhf (20210426_07:42) | armv7l | 113.34MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/21.04/cloud/ppc64el (1 more) | ed2437bcda33 | yes | Ubuntu hirsute ppc64el (20210426_07:42) | ppc64le | 126.67MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/21.04/cloud/s390x (1 more) | 875f23f2b967 | yes | Ubuntu hirsute s390x (20210426_07:42) | s390x | 101.16MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/21.04/ppc64el (3 more) | c10a5e4bfe1a | yes | Ubuntu hirsute ppc64el (20210426_07:42) | ppc64le | 109.09MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/21.04/s390x (3 more) | 0ad7c6db31c1 | yes | Ubuntu hirsute s390x (20210426_07:42) | s390x | 84.82MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/focal (7 more) | 5c5eda29629e | yes | Ubuntu focal amd64 (20210426_07:42) | x86_64 | 100.98MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/focal/arm64 (3 more) | 2b2c0998cadd | yes | Ubuntu focal arm64 (20210426_07:42) | aarch64 | 95.98MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/focal/armhf (3 more) | a8c4a14b77cd | yes | Ubuntu focal armhf (20210426_07:42) | armv7l | 93.48MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/focal/cloud (3 more) | d79f9ba499ea | yes | Ubuntu focal amd64 (20210426_07:42) | x86_64 | 116.07MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/focal/cloud/arm64 (1 more) | 7a404ae6363c | yes | Ubuntu focal arm64 (20210426_08:31) | aarch64 | 110.62MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/focal/cloud/armhf (1 more) | 677570e0f311 | yes | Ubuntu focal armhf (20210426_08:02) | armv7l | 107.86MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/focal/cloud/ppc64el (1 more) | a4bde8caa245 | yes | Ubuntu focal ppc64el (20210426_07:57) | ppc64le | 120.54MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/focal/cloud/s390x (1 more) | f914fb7160ed | yes | Ubuntu focal s390x (20210426_07:42) | s390x | 92.95MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/focal/ppc64el (3 more) | f31de423d1dd | yes | Ubuntu focal ppc64el (20210426_07:42) | ppc64le | 104.61MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
| ubuntu/focal/s390x (3 more) | 8a8131af401d | yes | Ubuntu focal s390x (20210426_07:42) | s390x | 78.75MB | Apr 26, 2021 at 12:00am (UTC) |
+-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
This gives us a huge list of images to choose from. However the one which we would installing is ubuntu/20.10/cloud
which we can fetch with :
$ lxc launch images:ubuntu/20.10/cloud ucloud
Here, ucloud is the name of our container which we created. Once the image has been downloaded, you can list it with :
$ lxc ls
+--------+---------+---------------------+-----------------------------------------------+------------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+--------+---------+---------------------+-----------------------------------------------+------------+-----------+
| ucloud | RUNNING | 10.120.27.98 (eth0) | fd42:7f00:50c2:7015:216:3eff:fee8:1392 (eth0) | PERSISTENT | 0 |
+--------+---------+---------------------+-----------------------------------------------+------------+-----------+
Thus we have a running Ubuntu cloud container !
5. Executing Commands In Our Container
Now that we have a running container, we can configure it and run commands on it as well ! The syntax to run commands as root is :
$ lxc exec [CONTAINER NAME] -- [CMD]
For example, we can now update our Ubuntu cloud image with :
$ lxc exec ucloud -- apt update
You can ever drop a root shell into the container with :
$ lxc exec ucloud -- /bin/bash
Conclusion
Containers are very instrumental in the modern day scenario. You can use them to host private or public servers or carry out tests. Either way, they are very useful for a lot of different stuff and are more effective than Virtual Machines in some ways. You can read more about LXC Containers here !