Super-quick guide to create a Linux (CentOS) virtual machine under LXD

These instructions work for Ubuntu 16.04, possibly with other distros as well. My goal was to set up a CentOS virtual machine for learning purposes.

Install LXD via snap
These commands must be run as superuser/root:

Answer the questions. Default answers should work fine but I was getting issues with the default storage pool name “default” so just chose the name “storage1” which sounds more logical anyhow.

Create the virtual machine
Here are commands to create several different types of virtual machines:

I chose to create a CentOS 7 VM. Note that this default VM is very barebones with only the most minimal components installed.

Connect to the virtual machine
This part was initially tricky but turned out to be very easy. Without knowing the default password, if you use lxc console centos you will not be able to log in. So instead connect with:

Now you’re connected, you can run useradd, passwd, yum or other commands to do more setup. Here are a few very preliminary things you might want to do:

Install some essential things

Some basic admin stuff
Replace username with your user’s name

Connect to your VM
Run ifconfig on your VM to see what your interface address is. LXD init automatically sets up a private subnet for the VM.

Note the address and netmask of the eth0 adapter. For example it might be 10.111.164.72 with a netmask of 255.255.255.0.

On the LXD host system itself you can directly reach hosts in the private subnet via ssh. Other hosts on the same network as your LXD host need to somehow know that to reach hosts in the 10.111.164.0/24 range they should go through the main interface of the LXD host system. For example if the LXD host system’s main interface address is 192.168.1.20 then the following command on a Windows machine (run in an Administrative console) creates the appropriate net route:

On a Linux system the command would be:

This is called a net route because it defines that a network is reached through a specific route. This is in contrast to a host route which creates a route to a specific host. Knowing how to create routes is a powerful admin tool.

After this point, you can now reach the new virtual machine via ssh and the user account created above.