{"id":153,"date":"2015-06-30T16:50:19","date_gmt":"2015-06-30T11:20:19","guid":{"rendered":"https:\/\/bhuvan.wordpress.com\/?p=153"},"modified":"2015-06-30T16:50:19","modified_gmt":"2015-06-30T11:20:19","slug":"configure-network-in-lxc-containers-to-access-host-system-physical-network-devices","status":"publish","type":"post","link":"https:\/\/www.bhuvankrishna.in\/blog\/2015\/06\/30\/configure-network-in-lxc-containers-to-access-host-system-physical-network-devices\/","title":{"rendered":"configure network in lxc containers to access host system physical network devices"},"content":{"rendered":"<p>I was trying to configure containers to get IPs from dhcp and it was difficult. at last I found a way to do it and thanks to debian wiki it again solved my problem, the source to solutions :).<\/p>\n<p>&nbsp;<\/p>\n<h4>Step 1<\/h4>\n<p>Installing lxc containers<br \/>\ninstalling lxc in debian stretch aka testing<\/p>\n<p><code>apt-get install lxc bridge-utils debootstrap<\/code><\/p>\n<h4>Step 2<\/h4>\n<p>Create a template. From my experience I was unable to create template of other OS&#8217;s than debian on debian. I didn&#8217;t go deep into the problem but to find templates check this directory.<\/p>\n<p><code>\/usr\/share\/lxc\/templates<\/code> and in <code>\/usr\/share\/lxc<\/code>examples of config files and other stuff is there.<\/p>\n<p>To create a new template<\/p>\n<p><code>lxc-create -n&lt;container name&gt; -t&lt;template&gt; <\/code><\/p>\n<h4>Step 3<\/h4>\n<p>After creating the template a random password is generated, copy that and change it after first login in to container.<\/p>\n<p>To start a container<br \/>\n<code>lxc-start -n &lt;container&gt;<\/code><br \/>\nTo run the container in demon mode give -d option.<\/p>\n<h4>Step 4<\/h4>\n<p>Now let us check network part. Change the following files accordingly<\/p>\n<p>This will create a new device and forward traffic using masquerade<br \/>\n<code>\/etc\/network\/interfaces<br \/>\nauto lxc-bridge-nat<br \/>\niface lxc-bridge-nat inet static<br \/>\nbridge_ports none<br \/>\nbridge_fd 0<br \/>\nbridge_maxwait 0<br \/>\naddress 192.168.100.1<br \/>\nnetmask 255.255.0.0<br \/>\nup iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE<br \/>\n<\/code><\/p>\n<p>Uncomment the line<br \/>\n<code>\/etc\/sysctl.conf<br \/>\n# Uncomment the next line to enable packet forwarding for IPv4<br \/>\nnet.ipv4.ip_forward=1<br \/>\n<\/code><\/p>\n<p>And give the command to forward<\/p>\n<p><code>echo 1 &gt; \/proc\/sys\/net\/ipv4\/ip_forward<\/code><\/p>\n<p>edit container config file and add these lines<\/p>\n<p><code>\/var\/lib\/lxc\/&lt;container&gt;\/config<\/p>\n<p># Template used to create this container: \/usr\/share\/lxc\/templates\/lxc-debian<br \/>\n# Parameters passed to the template:<br \/>\n# For additional config options, please look at lxc.container.conf(5)<br \/>\nlxc.network.type = empty<br \/>\nlxc.rootfs = \/var\/lib\/lxc\/builds\/rootfs<\/p>\n<p># Common configuration<br \/>\nlxc.include = \/usr\/share\/lxc\/config\/debian.common.conf<\/p>\n<p># Container specific configuration<br \/>\nlxc.mount = \/var\/lib\/lxc\/builds\/fstab<br \/>\nlxc.utsname = builds<br \/>\nlxc.arch = amd64<br \/>\nlxc.autodev = 1<br \/>\nlxc.kmsg = 0<\/p>\n<p># networking<br \/>\nlxc.network.type = veth<br \/>\nlxc.network.flags = up<br \/>\nlxc.network.link = lxc-bridge-nat<br \/>\nlxc.network.name = brid0<br \/>\n# It is fine to be commented out<br \/>\nlxc.network.ipv4 = 192.168.100.10\/24<br \/>\n# Change this<br \/>\nlxc.network.hwaddr = 00:11:22:33:44:01<br \/>\nlxc.network.ipv4.gateway = 192.168.100.1<br \/>\n<\/code><\/p>\n<p>and lastly add dhcp in containers interfaces file<br \/>\n<code>\/var\/lib\/lxc\/&lt;container&gt;\/rootfs\/etc\/network\/interfaces<br \/>\nauto lo<br \/>\niface lo inet loopback<\/p>\n<p>auto eth0<br \/>\niface eth0 inet dhcp<br \/>\n<\/code><\/p>\n<p>That is it and container should get internet.<br \/>\nSources:<br \/>\nhttps:\/\/wiki.debian.org\/LXC\/SimpleBridge<br \/>\nhttps:\/\/www.flockport.com\/enable-lxc-networking-in-debian-jessie-fedora-and-others\/<br \/>\nhttps:\/\/github.com\/claudyus\/LXC-Web-Panel\/issues\/74<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was trying to configure containers to get IPs from dhcp and it was difficult. at last I found a way to do it and thanks to debian wiki it again solved my problem, the source to solutions :). &nbsp; Step 1 Installing lxc containers installing lxc in debian stretch aka testing apt-get install lxc\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.bhuvankrishna.in\/blog\/2015\/06\/30\/configure-network-in-lxc-containers-to-access-host-system-physical-network-devices\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,7],"tags":[11,12,14,17],"class_list":["post-153","post","type-post","status-publish","format-standard","hentry","category-setup","category-techy","tag-computers","tag-container","tag-lxc","tag-tips"],"_links":{"self":[{"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/posts\/153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/comments?post=153"}],"version-history":[{"count":0,"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/posts\/153\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/media?parent=153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/categories?post=153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/tags?post=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}