<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Damilola Oladele</title>
    <description>The latest articles on DEV Community by Damilola Oladele (@damilola_oladele).</description>
    <link>https://dev.to/damilola_oladele</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F840913%2Fae8ecaf7-122f-4126-9865-e5660106de0c.jpg</url>
      <title>DEV Community: Damilola Oladele</title>
      <link>https://dev.to/damilola_oladele</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/damilola_oladele"/>
    <language>en</language>
    <item>
      <title>Get started with LXC: Explained with installation guide</title>
      <dc:creator>Damilola Oladele</dc:creator>
      <pubDate>Wed, 15 May 2024 05:00:20 +0000</pubDate>
      <link>https://dev.to/damilola_oladele/get-started-with-lxc-explained-with-installation-guide-4efj</link>
      <guid>https://dev.to/damilola_oladele/get-started-with-lxc-explained-with-installation-guide-4efj</guid>
      <description>&lt;p&gt;Containers have been around for a while, but their recent broad adoption has transformed modern application development and deployment.&lt;/p&gt;

&lt;p&gt;You can see containers as separate environments used for running different applications or software systems on a single computer while keeping them isolated from one another. Containers are different from virtual machines because they share the same operating system kernel as the host machine they operate on. This makes containers more lightweight and efficient. Each container has a separate view of the operating system, its processes, network, and storage. So applications running in different containers don't interfere with each other. &lt;/p&gt;

&lt;p&gt;There are two broad categories of containers—Linux containers (LXC) and Windows containers. While LXC run on the Linux kernel, Windows containers run on the Windows operating system.&lt;/p&gt;

&lt;p&gt;This article explains LXC, its installation on Ubuntu, the creation of privileged and unprivileged LXC containers, and management using LXC commands. Also, we'll compare LXC with LXD (Linux Daemon), a similar container solution.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is LXC?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;LXC was initially developed by &lt;a href="https://en.wikipedia.org/wiki/IBM"&gt;IBM&lt;/a&gt;. It's a tool used for operating-system virtualization. LXC is used to run multiple isolated Linux systems (containers) on a control host using a single Linux kernel. [Source: &lt;a href="https://en.wikipedia.org/wiki/LXC"&gt;Wikipedia&lt;/a&gt;]&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Each LXC container has its files, processes, network interfaces, and other resources isolated from the other containers and the host system. LXC uses Linux &lt;a href="https://en.wikipedia.org/wiki/Linux_namespaces"&gt;namespaces&lt;/a&gt;, a kernel feature that allows the partitioning of global system resources into isolated instances. LXC also uses &lt;a href="https://en.wikipedia.org/wiki/Cgroups"&gt;cgroups&lt;/a&gt;, another Linux kernel feature, to limit, measure, and control resource usage for processes.&lt;/p&gt;

&lt;p&gt;By combining resource isolation through &lt;code&gt;namespaces&lt;/code&gt; and resource control through &lt;code&gt;cgroups&lt;/code&gt;, LXC provides a secure and efficient way to run multiple isolated Linux environments on a single host system.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Install LXC
&lt;/h2&gt;

&lt;p&gt;Most Linux distributions offer recent versions of LXC either directly in their package repositories or through backport channels. For your first LXC experience, it is advisable to use a recent supported release for a smoother experience. If you're using Ubuntu, &lt;strong&gt;Ubuntu 18.04 LTS&lt;/strong&gt; is the recommended choice as a container host.&lt;/p&gt;

&lt;p&gt;Now let's go through the steps of installing LXC on Ubuntu, one of the most popular Linux distributions.&lt;/p&gt;

&lt;p&gt;First, update your package lists by running the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then install LXC and its dependencies by running the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt install lxc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running the preceding command, your system will have all the LXC commands available, all its templates as well as the python3 binding should you want to script LXC.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;On some Linux distributions, installing LXC may not automatically install all its dependencies and templates. So, you have to download the LXC dependencies and templates after installing the LXC package.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Once the installation is complete, you can verify that LXC is correctly installed by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lxc-checkconfig
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should get an output similar to this in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;LXC version 5.0.0
Kernel configuration not found at /proc/config.gz; searching...
Kernel configuration found at /boot/config-6.5.0-28-generic
--- Namespaces ---
Namespaces: enabled
Utsname namespace: enabled
Ipc namespace: enabled
Pid namespace: enabled
User namespace: enabled
Network namespace: enabled

--- Control groups ---
Cgroups: enabled
Cgroup namespace: enabled

Cgroup v1 mount points: 


Cgroup v2 mount points: 
/sys/fs/cgroup

Cgroup v1 systemd controller: missing
Cgroup v1 freezer controller: missing
Cgroup ns_cgroup: required
Cgroup device: enabled
Cgroup sched: enabled
Cgroup cpu account: enabled
Cgroup memory controller: enabled
Cgroup cpuset: enabled

--- Misc ---
Veth pair device: enabled, not loaded
Macvlan: enabled, not loaded
Vlan: enabled, not loaded
Bridges: enabled, loaded
Advanced netfilter: enabled, loaded
CONFIG_IP_NF_TARGET_MASQUERADE: enabled, not loaded
CONFIG_IP6_NF_TARGET_MASQUERADE: enabled, not loaded
CONFIG_NETFILTER_XT_TARGET_CHECKSUM: enabled, not loaded
CONFIG_NETFILTER_XT_MATCH_COMMENT: enabled, not loaded
FUSE (for use with lxcfs): enabled, not loaded

--- Checkpoint/Restore ---
checkpoint restore: enabled
CONFIG_FHANDLE: enabled
CONFIG_EVENTFD: enabled
CONFIG_EPOLL: enabled
CONFIG_UNIX_DIAG: enabled
CONFIG_INET_DIAG: enabled
CONFIG_PACKET_DIAG: enabled
CONFIG_NETLINK_DIAG: enabled
File capabilities: 

Note : Before booting a new kernel, you can check its configuration
usage : CONFIG=/path/to/config /usr/bin/lxc-checkconfig
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Privileged and Unprivileged LXC Containers
&lt;/h2&gt;

&lt;p&gt;There are two methods of using LXC—privileged and unprivileged. These methods determine the level of access and permissions granted to the container processes, affecting their isolation, security, and functionality.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to Create Privileged LXC Containers
&lt;/h3&gt;

&lt;p&gt;Privileged containers are containers created by root and run with root privileges on the host system. This grants them unrestricted access to system resources and the ability to perform operations reserved for the root user. This mode offers greater flexibility and control over system configurations, making it suitable for applications that require direct access to hardware resources or kernel modifications. However, privileged containers pose security risks. Due to their elevated privileges, any actions performed within a privileged container can affect the stability and security of the host system.&lt;/p&gt;

&lt;p&gt;To create a privileged LXC container interactively, run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo lxc-create --template download --name privileged-container
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The preceding command will interactively ask for the container root filesystem type to download, including the distribution, release, and architecture. The download template shows you a list of available container images and their details.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;You can replace the name, `privileged-container` with any container name that will be memorable for you.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to create a container non-interactively, specify the values of the root filesystem type, the distribution, release, and architecture in your command. For example, if you want to create a container, named &lt;code&gt;privileged-container&lt;/code&gt;, using the Ubuntu Linux distribution version 20.04 (Focal Fossa) on the AMD64 architecture, run the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo lxc-create --template download --name privileged-container -- --dist ubuntu --release focal --arch amd64
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See &lt;a href="https://images.linuxcontainers.org/"&gt;Linux Containers - Image server&lt;/a&gt; for the list of available container images and their details.&lt;/p&gt;

&lt;p&gt;To start your &lt;code&gt;privileged-container&lt;/code&gt; container, run the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo lxc-start --name privileged-container
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running the preceding command, use the following command to check if your container is now running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo lxc-info --name privileged-container
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running the preceding command should give you an output similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Name:           privileged-container
State:          RUNNING
PID:            7615
IP:             10.0.3.190
Link:           veth4LHNrz
 TX bytes:      1.57 KiB
 RX bytes:      4.16 KiB
 Total bytes:   5.74 KiB
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can follow the preceding steps to create as many privileged containers as you want. For a detailed list of all LXC (Linux Containers) on your system in a more readable format, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo lxc-ls --fancy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To remove a container, you have to first stop it using the &lt;code&gt;lxc-stop&lt;/code&gt; command like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo lxc-stop privileged-container
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then remove the container and it's configuration files using the &lt;code&gt;lxc-destroy&lt;/code&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo lxc-destroy privileged-container
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See &lt;a href="https://linuxcontainers.org/lxc/manpages/"&gt;LXC / Manpages&lt;/a&gt; for the complete list of LXC commands and their usage.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to Create Unprivileged LXC Containers
&lt;/h3&gt;

&lt;p&gt;Unprivileged LXC containers have limited access and enhanced isolation compared to privileged LXC containers. They are launched without root privileges using features like user namespaces and resource limitations. This improves security and minimizes the risk of breaching the host system.&lt;/p&gt;

&lt;p&gt;To create an unprivileged LXC container, you need to start by creating a default container configuration file. This file should specify your preferred ID mappings and network setup. Also, you will need to configure the host to allow the unprivileged user to connect to the host network.&lt;/p&gt;

&lt;p&gt;Now, let's go through the steps of creating unprivileged LXC Containers.&lt;/p&gt;

&lt;p&gt;First, run the following commands to check if your user has user and group id ranges:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;grep $USER /etc/subuid
grep $USER /etc/subgid
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If your user has no ranges assigned, add them with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo usermod -v 100000-200000 -w 100000-200000 user1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now create an LXC config directory and default config file by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir -p ~/.config/lxc
echo "lxc.idmap = u 0 100000 65536" &amp;gt; ~/.config/lxc/default.conf
echo "lxc.idmap = g 0 100000 65536" &amp;gt;&amp;gt; ~/.config/lxc/default.conf
echo "lxc.net.0.type = veth" &amp;gt;&amp;gt; ~/.config/lxc/default.conf
echo "lxc.net.0.link = lxcbr0" &amp;gt;&amp;gt; ~/.config/lxc/default.conf
echo "$USER veth lxcbr0 2" | sudo tee -a /etc/lxc/lxc-usernet
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The preceding command assumes that your user and group id ranges are `100000 65536`. Ensure you replace the values with the values you got from running `grep $USER /etc/subuid` and `grep $USER /etc/subgid`.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now create an unprivileged container named &lt;code&gt;unprivileged-container&lt;/code&gt;, using the Ubuntu Linux distribution version 20.04 (Focal Fossa) on the AMD64 architecture by running the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lxc-create -t download -n unprivileged-container -- --dist ubuntu --release focal --arch amd64
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you have created your unprivileged container, check if your working directory has execute permission by running the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ls -ld /&amp;lt;current working directory&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If your working directory has the execute permission, you will get an output similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;drwxr-x--x 21 &amp;lt;username&amp;gt; 4096 May 13 12:34 /&amp;lt;working directory&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If it doesn't, you can grant it the execute permission by running the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chmod +x /&amp;lt;current working directory&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, start the container by running the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lxc-start -n unprivileged-container
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  LXC vs LXD
&lt;/h2&gt;

&lt;p&gt;Both LXC (Linux Containers) and LXD (Linux Daemon) are tools used for virtualization and container management on Linux systems. While LXC provides the core functionality for creating and running containers, LXD is an extension that improves LXC with additional features and capabilities. To get started with LXD, see &lt;a href="https://documentation.ubuntu.com/lxd/en/latest/tutorial/first_steps/"&gt;First steps with LXD&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The following table shows the differences between LXC and LXD:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;LXC&lt;/th&gt;
&lt;th&gt;LXD&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Tool for virtualization of operating systems&lt;/td&gt;
&lt;td&gt;Extension of LXC with advanced features&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Requires multiple processes for containers&lt;/td&gt;
&lt;td&gt;Provides a single process for managing multiple containers.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Limited built-in security features&lt;/td&gt;
&lt;td&gt;Host-level security features to enhance container security&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Lacks support for snapshots, live migration, and storage pooling&lt;/td&gt;
&lt;td&gt;Supports snapshots, live migration, and storage pooling&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Basic security and integration capabilities&lt;/td&gt;
&lt;td&gt;Offers a wide range of security features&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Limited scalability capabilities&lt;/td&gt;
&lt;td&gt;Enables scalability within LXC&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Less user-friendly and requires expertise&lt;/td&gt;
&lt;td&gt;LXD is more user-friendly&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;No data retrieval feature after processing&lt;/td&gt;
&lt;td&gt;Provides data retrieval feature&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Uses C API&lt;/td&gt;
&lt;td&gt;Uses REST API&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;LXC have revolutionized the way applications are packaged and deployed across different environments, offering lightweight virtualization and efficient resource utilization.&lt;/p&gt;

&lt;p&gt;While LXC pioneered operating system-level virtualization on Linux, providing the core functionality for creating and managing containers, LXD has emerged as a powerful extension that enhances the container management experience. By introducing a single system daemon for managing multiple containers, host-level security features, live migration support, snapshots, and storage pooling capabilities, LXD addresses many of the shortcomings of LXC.&lt;/p&gt;

&lt;p&gt;As the adoption of containerization continues to grow, container solutions like LXD that streamline and simplify container management will become increasingly valuable.&lt;/p&gt;

&lt;h2&gt;
  
  
  References and Further Reading
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/LXC"&gt;https://en.wikipedia.org/wiki/LXC&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://linuxcontainers.org/lxc/getting-started/"&gt;https://linuxcontainers.org/lxc/getting-started/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://ubuntu.com/server/docs/lxc-containers"&gt;https://ubuntu.com/server/docs/lxc-containers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://askubuntu.com/questions/293275/what-is-lxc-and-how-to-get-started"&gt;https://askubuntu.com/questions/293275/what-is-lxc-and-how-to-get-started&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://documentation.ubuntu.com/lxd/en/latest/explanation/lxd_lxc/"&gt;https://documentation.ubuntu.com/lxd/en/latest/explanation/lxd_lxc/&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>linux</category>
      <category>containers</category>
      <category>ubuntu</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
