How to Run Multiple Distributions Simultaneously Using Linux Containers 1

How to Run Multiple Distributions Simultaneously Using Linux Containers

Linux Containers (LXC) is a lightweight virtualization technology and has a variety of uses. It is part of the Linux kernel and can allow you to emulate one or more Linux distributions on a single Linux host. Think of it as a middle ground between a chroot and full virtualization technologies like VirtualBox, KVM or Xen. A similar technology found in the BSD world is FreeBSD Prisons.

As an example, the machine I’m writing on is a laptop running Linux Mint 18, powered by an Intel Atom processor, and with insufficient 2GB of RAM. I’m running three Linux containers each running an instance of the Apache web server, though, without a huge performance boost. With a traditional virtual machine like VirtualBox this was unthinkable. So, if you want to run multiple distributions on your Linux system, Linux Containers should do the job just fine for you.

Installing and Configuring Linux Containers

We are installing LXC on Linux Mint 18 64-bit. The installation instructions provided here will also work unmodified on Ubuntu 16.04 and above. If you are using another distribution, please refer to your distribution’s official documentation if something is not working as expected. Familiarity with the command line and general troubleshooting are also assumed.

Prerequisites

Here are a few things you need to set up to start using multiple distributions:

one. Install LXC and using other prerequisite software:

[sourcecode]sudo apt install lxc lxc-templates uidmap[/sourcecode]

2. What you need to do now configure your profile. Enter the following commands to set it up:

[sourcecode]mkdir -p ~/.config/lxc
echo “lxc.id_map = u 0 100000 65536” > ~/.config/lxc/default.conf
echo “lxc.id_map = g 0 100000 65536” >> ~/.config/lxc/default.conf
echo “lxc.network.type = veth” >> ~/.config/lxc/default.conf
echo “lxc.network.link = lxcbr0” >> ~/.config/lxc/default.conf
echo “$USER veth lxcbr0 2” | sudo tee -a /etc/lxc/lxc-usernet[/sourcecode]

3. Next, you need to set user permissions as follows:

[sourcecode]sudo usermod –add-subuids 100000-165536 $USER
sudo usermod –add-subgids 100000-165536 $USER

sudo cgm create all users
sudo cgm all user $(id -u) $(id -g) chown
cgm movepid all user $$[/sourcecode]

Setting Up Your Container

Now that you have installed the LXC Container along with the other prerequisite software, here are the steps to install the Container:

1. In this example, we will install an Ubuntu container, ubu1 judicial. To do this, execute the following command:

[sourcecode]lxc-create –template download –name ubu1[/sourcecode]

2. Here, –template parameter tells lxc: download a preconfigured image from the internet, –name parameter specifying the name of the container – in this case ubu1. You can use any name you want.

In relation :  iPhone: How to find your lost or stolen phone?

3. You will now see a list. supported distribution images:

4. Enter the properties of the distribution Here I will install the 64-bit version (codename xenial) of Ubuntu 16.04 that you want to install:

install ubuntu

5. If you want upload the image non-interactivelythe following command achieves the same result as above:

[sourcecode]lxc-create -t ​​download -n ubu1 — –dist ubuntu – xenial –arch amd64 version[/sourcecode]

6. LXC will now download and install a minimal Ubuntu xenial image on your host system. Download and installation may take some time depending on your internet connection and computer speed. after installationYou will see a screen like this:

ubuntu_installed

You are now ready to use your newly installed Ubuntu container.

Using Multiple Distributions with Linux Containers

Starting the Container

lxc-start Start your container using the command:

[sourcecode]lxc-start -n ubu1 -d[/sourcecode]

Here, -n parameter Specifies the name of the container you want to start (in this case ubu1) and -d parameter makes it run in the background.

lxc-ls You can verify that the container is started using the command:

[sourcecode]lxc-ls -f[/sourcecode]

running containers

-f parameter enables: fancy reporting. Here you can see what I have two pots – one Debian (stopped) and one Ubuntu (running).

Accessing and Using Your Container

You can do access the container’s console lxc-attach using the command:

[sourcecode]lxc-add -n ubu1[/sourcecode]

now one root bark on your container. It is recommended to set a password for the root user and create a regular user account:

[sourcecode]password
adduser beebom[/sourcecode]

of course change beebom With the username you want. You can then install the software and configure your container as you would on a normal system. For example, in a Debian or Ubuntu container:

[sourcecode]apt wget openssh-server htop tmux nano iptables setup[/sourcecode]

Stopping Your Container

After you finish playing with the container, exit Use the command. to return to the main system. Now lxc-stop use the command to stop your container:

[sourcecode]lxc-stop -n ubu1[/sourcecode]

This will cleanly close the container and will not consume any more resources on your system other than disk space.

Cloning and Snapshots

clones

After installing the programs in a container and configuring them to your liking, make one or more copies of this for easy provisioning. You can do this by creating cloneis an exact copy of a container.

For example, ubu1 to create a clone of its container (this ubu2 let’s say), stop the container first lxc-stop using, then lxc-copy use the command:

[sourcecode]lxc-stop -n ubu1

lxc-copy -n ubu1 -N ubu2[/sourcecode]

Here, The -n option specifies the source containerand The -n option specifies the name of the clone. With verify to understand that the container has been cloned lxc-ls use the command:

clone

snapshots

Let’s say you are about to make some of the changes made to a container potentially dangerous or difficult to recover, such as reconfiguring a web server. You can create a snapshot of the container before making such a change to minimize damage. If something goes wrong during configuration, you can stop the container and bring it back to the previous working state by restoring a snapshot.

In relation :  How to Make Google Sheets Editable by Multiple Users

To create the snapshot, first stop container:

[sourcecode]lxc-stop -n ubu1[/sourcecode]

At that time, create snapshot lxc-snapshot using the command:

[sourcecode]lxc-snapshot -n ubu1[/sourcecode]

This, snap0 Creates a snapshot named The next snapshots you create using this command will be called snap1, snap2etc.

After that you can start select the container and make the changes you want. If at any time you want to revert to a snapshot you created, Stop select the container and lxc-snapshot command -r Use with parameter. restore snapshot:

[sourcecode]lxc-snapshot -r snap0 -n ubu1[/sourcecode]

This, snap0 snapshot of ubu1 will restore it to its container.

Autostart Containers on Boot

You can have a container, for example a web server container, start automatically when you start your system. To do this, go to: container’s config file$HOME/.local/share/lxc//config is in and add the following lines:

[sourcecode]lxc.start.auto = 1
lxc.start.delay = 5[/sourcecode]

The first line indicates that the container should be started on boot. The second tells the system: wait 5 seconds if any, before starting the next container.

Troubleshooting

First thing to try if you’re having trouble starting containers lxc-start to run the command. pre plan mode. For example:

[sourcecode]lxc-start -n ubu1 -F[/sourcecode]

This will show errors in current consolee is very helpful in determining the nature of the problem.

Issues with Running Multiple Containers at the Same Time

If you try to run multiple containers at the same time, you can see the errors Like “Quota reached” or “The configured network could not be created”. This is because you are running more network interfaces than are allocated to you. You can do increase the number of network bridges a user can run /etc/lxc/lxc-usernet by changing the file to root. It might look like this:

[sourcecode]# USERNAME TYPE NUMBER OF BRIDGES
beebom veth lxcbr0 5[/sourcecode]

You can do change the number at the end (5 in this example) to a larger number like 10. This will allow you to run up to 10 containers simultaneously.

Other Uses of Linux Containers

Linux containers have a variety of uses. You can use them as lightweight test boxes, for example, to test various configurations of a web or database server before committing to a production server. Another use case is testing how an app works on different versions of various distributions.

You can also use them to isolate applications you don’t trust; any damage from such an application will be limited to its own container and will not affect the host system. Please note that while it is possible to run GUI applications in a container, this requires a significant amount of time and effort and is therefore not recommended. If you want to run GUI applications in a sandbox, see our article on how to protect applications in Linux.

SEE ALSO: 7 Best VirtualBox Alternatives to Use

Run Multiple Distributions Simultaneously with Linux Containers

Thus ends our How-To on running multiple Linux distributions on a single computer without the overhead of a full-size virtual machine. The usefulness of this technology is limited only by your creativity, so feel free to experiment and find new use cases. If you’re having trouble installing containers, feel free to ask us a question in the comments section.