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:

apt install snapd
snap install lxd
snap start lxd
lxd init

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:

lxc launch images:centos/7 centos
lxc launch ubuntu:16.04
lxc launch images:archlinux archlinux
lxc launch images:debian/stretch debian

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:

lxc exec centos bash

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

yum install net-tools openssh openssh-server bash-completion sudo less
service sshd start  # start the ssh daemon

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

adduser username  # add a non-root user
passwd username  # set the password
usermod -a -G wheel username  # add user to wheel group for special admin privileges
visudo  # edit the sudoers file to check/change any settings

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:

route add 10.111.164.0 mask 255.255.255.0 192.168.1.20

On a Linux system the command would be:

route add -net 10.111.164.0/24 gw 192.168.1.20

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.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *