{"id":13523,"date":"2017-05-13T12:19:51","date_gmt":"2017-05-13T06:49:51","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=13523"},"modified":"2017-05-13T12:19:51","modified_gmt":"2017-05-13T06:49:51","slug":"run-multiple-distros-simultaneously-using-linux-containers","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/run-multiple-distros-simultaneously-using-linux-containers\/","title":{"rendered":"How to Run Multiple Distros Simultaneously Using Linux Containers"},"content":{"rendered":"<p>Linux Containers (LXC) are a lightweight virtualization technology and they have different employments. It is a piece of the Linux kernel, and can give you a chance to copy one, or numerous Linux distros on a solitary Linux host. Consider it a center ground between a chroot and full virtualization technology, for example, VirtualBox, KVM or Xen. A comparative innovation exhibit in the BSD world are FreeBSD Jails.<\/p>\n[ad type=\u201dbanner\u201d]\n<p>For instance, the machine I am composing on is a portable PC running Linux Mint 18, fueled by an Intel Atom processor, and has a pitiful 2 GB of RAM. However, I am running three Linux containers, each with an example of the Apache web server running, without a major execution hit. This would be unbelievable with a conventional virtual machine like VirtualBox. Along these lines, on the off chance that you have been needing to run various distros on your Linux system, Linux Containers ought to carry out the employment for you fine and great.<\/p>\n<h4 id=\"installing-and-configuring-linux-containers\"><span style=\"color: #993300;\"><strong>Installing and Configuring Linux Containers<\/strong><\/span><\/h4>\n<p>We are setting up LXC on Linux Mint 18 64-bit. The installation instructions provided here will also work unmodified on Ubuntu 16.04 and above. A familiarity with the command line and general troubleshooting is also assumed.<\/p>\n<h4 id=\"prerequisites\"><span style=\"color: #ff6600;\"><strong>Prerequisites<\/strong><\/span><\/h4>\n<p>Here are a few things you should set up to start using multiple distros:<\/p>\n<ul>\n<li><strong>Install LXC\u00a0<\/strong>and other prerequisite software using:<\/li>\n<\/ul>\n<blockquote><p><code>sudo apt install lxc lxc-templates uidmap<\/code><\/p><\/blockquote>\n<ul>\n<li>Now you need to\u00a0<strong>configure your profile<\/strong>. Enter the following commands to set it up:<\/li>\n<\/ul>\n<blockquote><p><code>\u00a0 mkdir -p ~\/.config\/lxc<\/code><\/p>\n<p><code>echo \"lxc.id_map = u 0 100000 65536\" &amp;amp;amp;amp;amp;amp;gt; ~\/.config\/lxc\/default.conf<\/code><\/p>\n<p><code>echo \"lxc.id_map = g 0 100000 65536\" &amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;gt; ~\/.config\/lxc\/default.conf<\/code><\/p>\n<p><code> echo \"lxc.network.type = veth\" &amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;gt; ~\/.config\/lxc\/default.conf<\/code><\/p>\n<p><code> echo \"lxc.network.link = lxcbr0\" &amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;gt; ~\/.config\/lxc\/default.conf<\/code><\/p>\n<p><code>echo \"$USER veth lxcbr0 2\" | sudo tee -a \/etc\/lxc\/lxc-usernet<\/code><\/p><\/blockquote>\n<ul>\n<li>\u00a0Then, you need to\u00a0<strong>set up the user permissions\u00a0<\/strong>as follows:<\/li>\n<\/ul>\n<blockquote><p><code>sudo usermod --add-subuids 100000-165536 $USER<\/code><\/p>\n<p><code>sudo usermod --add-subgids 100000-165536 $USER<\/code><\/p>\n<p><code>sudo cgm create all user<\/code><\/p>\n<p><code>sudo cgm chown all user $(id -u) $(id -g)<\/code><\/p>\n<p><code><\/code><code>cgm movepid all user $$<\/code><\/p><\/blockquote>\n<h4 id=\"setting-up-your-container\"><span style=\"color: #99cc00;\"><strong>Setting Up Your Container<\/strong><\/span><\/h4>\n<p>Now that you have LXC Container installed along with other prerequisite software, here are the steps to set up the Container:<\/p>\n<ul>\n<li>In this example,<strong>we will set up an Ubuntu container<\/strong>, named <code>ubu1<\/code>. To do it, execute the following command:<\/li>\n<\/ul>\n<blockquote><p><code>lxc-create --template download --name ubu1<\/code><\/p><\/blockquote>\n<ul>\n<li>Here, the <code>\u2013template<\/code> parameter tells lxc to\u00a0<strong>download\u00a0<\/strong><strong>a preconfigured image from the internet<\/strong>, while the <code>\u2013name<\/code> parameter specifies the name of the container \u2013 <code>ubu1<\/code> in this case. You can use whichever name you like.<\/li>\n<li>You will now see a list of\u00a0<strong>supported distro images<\/strong>:<\/li>\n<\/ul>\n[ad type=\u201dbanner\u201d]\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-13585\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/distro-selection.png\" alt=\"How to Run Multiple Distros Simultaneously Using Linux Containers\" width=\"640\" height=\"438\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/distro-selection.png 640w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/distro-selection-300x205.png 300w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<ul>\n<li><strong>Enter the specifics of the distribution\u00a0<\/strong>that you wish to install. I will install the 64-bit version of Ubuntu 16.04 (code name xenial) here:<\/li>\n<\/ul>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-13586\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/install-ubuntu.png\" alt=\"How to Run Multiple Distros Simultaneously Using Linux Containers\" width=\"640\" height=\"438\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/install-ubuntu.png 640w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/install-ubuntu-300x205.png 300w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<ul>\n<li>If you wish to\u00a0<strong>install the image non-interactively<\/strong>, the following command achieves the same result as the above:<\/li>\n<\/ul>\n<blockquote><p><code>lxc-create -t download -n ubu1 -- --dist ubuntu --release xenial --arch amd64<\/code><\/p><\/blockquote>\n<ul>\n<li>LXC will now download and install a minimal Ubuntu xenial image on your host system. The download and installation might take a bit of time depending on your internet connection and your PC\u2019s speed.<strong>After the installation<\/strong>, you will see a screen like this:<\/li>\n<\/ul>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-13588\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/ubuntu_installed.png\" alt=\"How to Run Multiple Distros Simultaneously Using Linux Containers\" width=\"640\" height=\"438\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/ubuntu_installed.png 640w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/ubuntu_installed-300x205.png 300w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<p><span style=\"color: #993300;\"><strong>Using Multiple Distros with Linux Containers<\/strong><\/span><\/p>\n<h4 id=\"starting-up-the-container\"><span style=\"color: #ff6600;\"><strong>Starting Up The Container<\/strong><\/span><\/h4>\n<ul>\n<li>Start your container using the <code>lxc-start<\/code> command:<\/li>\n<\/ul>\n<blockquote><p><code>lxc-start -n ubu1 -d<\/code><\/p><\/blockquote>\n<ul>\n<li>Here, the <code>-n<\/code> parameter\u00a0<strong>specifies the name of the container<\/strong>\u00a0that you wish to start (<code>ubu1<\/code> in this case), and the <code>-d<\/code> parameter\u00a0<strong>makes it run in the background<\/strong>.<\/li>\n<li>You can verify that the container started by using the <code>lxc-ls<\/code> command:<\/li>\n<\/ul>\n<blockquote><p><code>lxc-ls -f<\/code><\/p><\/blockquote>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-13587\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/running-containers.png\" alt=\"How to Run Multiple Distros Simultaneously Using Linux Containers\" width=\"640\" height=\"73\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/running-containers.png 640w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/running-containers-300x34.png 300w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<ul>\n<li>The <code>-f<\/code> parameter enables\u00a0fancy\u00a0 Here, you can see that I have\u00a0<strong>two containers<\/strong>\u00a0\u2013 one Debian (stopped) and one Ubuntu (running).<\/li>\n<\/ul>\n<h4 id=\"accessing-and-using-your-container\"><span style=\"color: #99cc00;\"><strong>Accessing and Using Your Container<\/strong><\/span><\/h4>\n<ul>\n<li>You can\u00a0<strong>access the container\u2019s console<\/strong>\u00a0using the <code>lxc-attach<\/code> command:<\/li>\n<\/ul>\n<blockquote><p><code>lxc-attach -n ubu1<\/code><\/p><\/blockquote>\n<ul>\n<li>You will now have a<strong>root shell<\/strong>\u00a0on your container. It is recommended that you set up a password for the root user, and\u00a0<strong>create a regular user account<\/strong>:\n<ul>\n<li style=\"text-align: left;\"><strong>passwd<\/strong><\/li>\n<li style=\"text-align: left;\"><strong>adduser Wikitechy<\/strong><\/li>\n<\/ul>\n<\/li>\n<li>Of course, replace\u00a0<strong>Wikitechy<\/strong>\u00a0with the username you want. You can then install software and configure your container like you would do on a regular system. For example, in a Debian or Ubuntu container:<\/li>\n<\/ul>\n<blockquote><p><code>apt install wget openssh-server htop tmux nano iptables<\/code><\/p><\/blockquote>\n<h4 id=\"stopping-your-container\"><span style=\"color: #993300;\"><strong>Stopping Your Container<\/strong><\/span><\/h4>\n<ul>\n<li>After you are done playing with the container, either use the <code>exit<\/code> command\u00a0<strong>to return to the host system<\/strong>. Now use the <code>lxc-stop<\/code> command\u00a0<strong>to stop your container<\/strong>:<\/li>\n<\/ul>\n<blockquote><p><code>lxc-stop -n ubu1<\/code><\/p><\/blockquote>\n<ul>\n<li>This will make the container shut down cleanly and it will not consume any more resources on your system, except for disk space.<\/li>\n<\/ul>\n[ad type=\u201dbanner\u201d]\n<h4 id=\"cloning-and-snapshots\"><span style=\"color: #ff6600;\"><strong>Cloning and Snapshots<\/strong><\/span><\/h4>\n<h4 id=\"clones\"><span style=\"color: #99cc00;\"><strong>Clones<\/strong><\/span><\/h4>\n<p>Once you have installed programs in a container and configured it to your liking, you might want to\u00a0<strong>create one or multiple copies of it<\/strong>\u00a0for easy provisioning. You can do this by creating a\u00a0<strong>clone<\/strong>, which is an exact replica of a container.<\/p>\n<ul>\n<li>For instance, to create a clone of the <code>ubu1<\/code> container (let\u2019s call it <code>ubu2<\/code>),<strong>first stop the container<\/strong>\u00a0using <code>lxc-stop<\/code>, then use the <code>lxc-copy<\/code> command:<\/li>\n<\/ul>\n<blockquote><p><code>lxc-stop -n ubu1<\/code><\/p>\n<p><code>lxc-copy -n ubu1 -N ubu2<\/code><\/p><\/blockquote>\n<ul>\n<li>Here, the<strong>-n option specifies the source container<\/strong>, and the\u00a0<strong>-N option specifies the name of the clone<\/strong>. To\u00a0<strong>verify<\/strong>\u00a0that the container was cloned, use the <code>lxc-ls<\/code> command:<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-13584\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/clone.png\" alt=\"How to Run Multiple Distros Simultaneously Using Linux Containers\" width=\"640\" height=\"116\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/clone.png 640w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/clone-300x54.png 300w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<h4 id=\"snapshots\"><span style=\"color: #993300;\"><strong>Snapshots<\/strong><\/span><\/h4>\n<p>Suppose you are about to make some potentially dangerous or hard to recover from changes to a container, such as re-configuring a web server. To minimize the damage, you can create a snapshot of the container before making such a change. In case something goes wrong during the configuration, you can simply stop the container and recover it to its previous working state by restoring a snapshot.<\/p>\n<ul>\n<li>To create the snapshot, first\u00a0<strong>stop the container<\/strong>:<\/li>\n<\/ul>\n<blockquote><p><code>lxc-stop -n ubu1<\/code><\/p><\/blockquote>\n<ul>\n<li>Then,<strong>create a snapshot<\/strong>\u00a0using the <code>lxc-snapshot<\/code> command:<\/li>\n<\/ul>\n<blockquote><p><code>lxc-snapshot -n ubu1<\/code><\/p><\/blockquote>\n<ol start=\"3\">\n<li>This creates a snapshot called <code>snap0<\/code>. Any subsequent snapshots you create using this command will be called\u00a0snap1,\u00a0snap2, etc.<\/li>\n<li>After this, you can\u00a0<strong>start<\/strong>\u00a0the container, and make the changes you want. If at any time, you want to return to a snapshot you created,\u00a0<strong>stop<\/strong>\u00a0the container, and use the <code>lxc-snapshot<\/code> command with the <code>-r<\/code> parameter to\u00a0<strong>restore a snapshot<\/strong>:<\/li>\n<\/ol>\n<blockquote><p><code>lxc-snapshot -r snap0 -n ubu1<\/code><\/p><\/blockquote>\n<ol start=\"5\">\n<li>This will restore the snapshot <code>snap0<\/code> to the <code>ubu1<\/code> container.<\/li>\n<\/ol>\n<h4 id=\"auto-start-containers-at-boot\"><span style=\"color: #ff6600;\"><strong>Auto-start Containers at Boot<\/strong><\/span><\/h4>\n<ul>\n<li>You can make a container, for example, a web server container, startup automatically when you boot up your system. To do this, go to the\u00a0<strong>container\u2019s config file<\/strong>, located in <code>$HOME\/.local\/share\/lxc\/<NAME OF CONTAINER>\/config<\/code>, and\u00a0<strong>add the following lines<\/strong>:<\/li>\n<\/ul>\n<blockquote><p><code>lxc.start.auto = 1<\/code><\/p>\n<p><code>lxc.start.delay = 5<\/code><\/p><\/blockquote>\n<ul>\n<li>The first line specifies that the container should be started at boot. The second tells the system to<strong>wait 5 seconds<\/strong>\u00a0before starting the next container, if any.<\/li>\n<\/ul>\n<h4 id=\"troubleshooting\"><span style=\"color: #99cc00;\"><strong>Troubleshooting<\/strong><\/span><\/h4>\n<ul>\n<li>If you are having trouble starting containers, the first thing to try is to run the <code>lxc-start<\/code> command in\u00a0Foreground\u00a0 For example:<\/li>\n<\/ul>\n<blockquote><p><code>lxc-start -n ubu1 -F<\/code><\/p><\/blockquote>\n<ul>\n<li>This will\u00a0<strong>show you the errors on the current consol<\/strong>e, which is very useful in identifying the nature of the problem.<\/li>\n<\/ul>\n<h4 id=\"problems-with-running-multiple-containers-simultaneously\"><span style=\"color: #993300;\"><strong>Problems With Running Multiple Containers Simultaneously<\/strong><\/span><\/h4>\n<ul>\n<li>If you try to run multiple containers at once,<strong>you might see errors<\/strong>\u00a0such as \u201cQuota reached\u201d, or \u201cfailed to create the configured network\u201d.\u00a0 This is because you are running more network interfaces than allotted to you. You can\u00a0<strong>increase the number of network bridges a\u00a0user can run<\/strong>\u00a0by modifying the <code>\/etc\/lxc\/lxc-usernet<\/code> file as\u00a0<strong>root<\/strong>. It might look something like this:<\/li>\n<\/ul>\n<blockquote><p><code># USERNAME TYPE BRIDGE COUNT<\/code><\/p>\n<p><code><\/code><code>Wikitechy veth lxcbr0 5<\/code><\/p><\/blockquote>\n<ul>\n<li>You can<strong>change the number at the end<\/strong>\u00a0(5 in this example), to a bigger number such as 10. This will allow you to run up to 10 containers at once.<\/li>\n<\/ul>\n[ad type=\u201dbanner\u201d]\n<h4 id=\"other-uses-of-linux-containers\"><span style=\"color: #ff6600;\"><strong>Other Uses of Linux Containers<\/strong><\/span><\/h4>\n<p>Linux containers have various uses. You can use them as lightweight test boxes, for example, to test out various configurations of a web or database server before committing them to a production server. Another use case is to test how an application runs on different versions of various distros.<\/p>\n<p>You can also use them to isolate applications that you do not trust \u2013 any damage such an application does 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, it takes a significant amount of time and effort, and is thus not recommended .<\/p>\n<h4 id=\"run-multiple-distros-simultaneously-with-linux-containers\"><span style=\"color: #99cc00;\"><strong>Run Multiple Distros Simultaneously With Linux Containers<\/strong><\/span><\/h4>\n<p>Thus ends our How to running multiple Linux distros on a single computer, without the overhead of a full-sized virtual machine. The utility of this technology is only\u00a0limited by your creativity, so feel free to experiment and find out novel use cases.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Run Multiple Distros Simultaneously Using Linux Containers &#8211; PC &#8211; Linux Containers (LXC) are a lightweight virtualization technology <\/p>\n","protected":false},"author":1,"featured_media":11953,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1699,5739],"tags":[40640,40652,40649,40654,40661,40629,40639,40658,40630,40659,40636,40635,40632,40634,40651,40631,40637,40648,40641,40646,40633,40656,40638,40655,40650,40653,40643,40644,40642,40660,40657,40647,40645],"class_list":["post-13523","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-pc","tag-container-docker","tag-container-isolation","tag-container-no","tag-container-virtualization-linux","tag-debian-containers","tag-docker","tag-docker-container","tag-docker-for-dummies","tag-docker-images","tag-docker-integration","tag-docker-linux","tag-docker-remove-image","tag-docker-repository","tag-docker-run","tag-docker-server","tag-docker-service","tag-docker-wiki","tag-docker0","tag-dockers-and-containers","tag-host-docker-containers","tag-install-docker","tag-linux-container-virtualization","tag-linux-containers","tag-linux-containers-vs-vm","tag-linux-docker-containers","tag-linux-lxle","tag-linux-namespaces","tag-lxc","tag-lxc-container","tag-lxc-download","tag-lxc-hypervisor","tag-lxc-server","tag-openvz"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/13523","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=13523"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/13523\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/11953"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=13523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=13523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=13523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}