Skip to content

Instantly share code, notes, and snippets.

@manoj23
Last active January 26, 2024 01:22
Show Gist options
  • Save manoj23/8a35849697945896cdaef77927c695a7 to your computer and use it in GitHub Desktop.
Save manoj23/8a35849697945896cdaef77927c695a7 to your computer and use it in GitHub Desktop.
Install Ubuntu containers on Archlinux using LXC

Install Ubuntu containers on Archlinux using LXC

Install the lxc and the debootstrap packages:

# pacman -Sy lxc debootstrap 

Install an Ubuntu container

I will install Ubuntu 16. Therfore, I will use the xenial release. If you want to install Ubuntu 12.04 LTS, use the precise release.

I create an LXC container named ubuntu-16 with the release xenial and the architecture amd64:

# lxc-create --name=ubuntu-16 --template=download -- --dist ubuntu --release xenial --arch amd64

Setup the network

Install the libvirt package because we need the virsh program. Also, we will need ebtables and dnsmasq for the default NAT/DHCP networking. (*)

# pacman -Sy libvirt ebtables dnsmasq

Start and enable the libvirtd daemon.

# systemctl start libvirtd
# systemctl enable libvirtd

Print the virtual network configuration default provided by libvirtd.

# virsh net-dumpxml default
<network>
  <name>default</name>
  <uuid>f9f9128b-8183-4c2c-af56-23cd52100d3a</uuid>
  <forward mode='nat'/>
  <bridge name='virbr0' stp='on' delay='0'/>
  <mac address='52:54:00:8d:ea:7f'/>
  <ip address='192.168.122.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.122.2' end='192.168.122.254'/>
    </dhcp>
  </ip>
</network>

Edit the file /var/lib/lxc/ubuntu-16/config to set-up the network configuration of the container:

lxc.net.0.type = veth
lxc.net.0.flags = up
lxc.net.0.name = eth0
lxc.net.0.link = virbr0

Setup the virb0 interface:

# virsh net-start default

Set-up the ubuntu password

sudo lxc-attach -n ubuntu-16
# passwd ubuntu

Type ctrl-d to detach to the session.

Install openssh-server

sudo lxc-start -F -n ubuntu-16

Connect as ubuntu and install openssh-server as follow:

sudo apt install openssh-server

Start the Ubuntu container

Start the Ubuntu container as follow:

# lxc-start --name ubuntu-16

Get the IP of the Ubuntu container:

# lxc-ls -f ubuntu-16 -F IPV4

You can stop the Ubuntu container as follow:

# lxc-stop --name ubuntu-16

Set a static IP for the container

Edit the default virtual network provided by libvirtd to restrict the DHCP range from 192.168.122.2-192.168.122.254 to 192.168.122.10-192.122.254.

# virsh net-edit default

as follow:

<network>
  <name>default</name>
  <uuid>bd881c3e-406f-40bb-84ed-b09e68f210e2</uuid>
  <forward mode='nat'/>
  <bridge name='virbr0' stp='on' delay='0'/>
  <mac address='52:54:00:2d:23:30'/>
  <ip address='192.168.122.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.122.10' end='192.168.122.254'/>
    </dhcp>
  </ip>
</network>

Restart the libvirtd daemon:

# systemctl restart libvirtd

Stop the the xenial container:

# lxc-stop --name ubuntu-16

Add the following line in the file /var/lib/lxc/ubuntu-16/config:

lxc.net.0.ipv4.address = 192.168.122.2/24

Restart the ubuntu-16 container.

Connect to the container

# ssh [email protected]

Use the password ubuntu to connect.

@RashikShahjahan
Copy link

@niclimcy
Copy link

niclimcy commented Apr 9, 2022

Edit the netplan.io config in the lxc container if not your static ip will not be set

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment