Setting up LXD on a VPS for Service Isolation and Lightweight Container Deployment
TL;DR
In this detailed guide, we will set up LXD on your VPS to create and manage isolated, lightweight containers. LXD allows you to run multiple applications or services, each in its own clean environment, minimizing conflicts and enhancing security while efficiently utilizing server resources. You will learn how to install LXD, configure its network and storage, launch containers, and ensure their stable operation and backup.
- LXD Installation and Initialization: Detailed steps for installing LXD on Ubuntu 24.04 LTS and its basic configuration.
- Isolation and Security: Creating isolated containers for each service, leveraging the benefits of LXD's lightweight virtualization.
- Resource Management: Configuring container profiles with CPU, RAM, and disk space limits.
- Network Configuration: Setting up a bridged network for containers, ensuring external access and internal communication.
- Backup: Implementing a backup strategy using LXD snapshots and external storage.
- Maintenance: Recommendations for LXD updates and container lifecycle management.
What We Are Setting Up and Why
In this guide, we will focus on setting up LXD — a system container manager — on your Virtual Private Server (VPS). LXD allows you to run full-fledged operating systems (e.g., Ubuntu, Debian, Alpine) inside lightweight containers which, unlike virtual machines (such as KVM), utilize the host's main operating system kernel. This provides significantly lower overhead, faster startup, and high service density on a single server, while maintaining a high level of isolation.
What the reader will gain: You will learn to effectively utilize your VPS resources by running various services (web servers, databases, game servers, blockchain nodes) in separate, fully isolated containers. Each container will have its own file system, network stack, and set of processes, which will prevent dependency conflicts and simplify management. This is an ideal solution for developers, SaaS founders, and enthusiasts who need flexibility and control over their infrastructure without the need to rent multiple separate VPS instances.
What alternatives exist and why self-hosted on a VPS:
- Virtual Machines (KVM, VMware): Provide full hardware isolation but have significant resource overhead (each VM requires its own OS kernel, more RAM, and CPU). LXD is more efficient for tasks requiring OS-level isolation rather than hardware isolation.
- Docker: An excellent tool for application containerization, but focused on running individual processes or microservices, not full-fledged operating systems. LXD, in contrast, allows running "virtual machines" at the container level, which is more convenient for migrating existing applications or running services that require a full init system (systemd).
- Cloud-managed services (AWS EC2, Google Cloud Compute): Convenient and scalable, but often more expensive and less flexible for those who want full control over their environment. Self-hosting LXD on a VPS gives you full system access, allows you to optimize costs, and tailor the configuration to your unique needs.
Choosing self-hosted LXD on a VPS is justified when you need flexibility, isolation, and efficient resource utilization, while also wanting to avoid the high costs and limitations of cloud providers.
dnsA server for this guide
Matching plans from verified providers, prices updated daily.
Compare:DigitalOcean vs VultrDigitalOcean vs ContaboAll VPS by price
Affiliate links: we may earn a commission, your price stays the same.
What VPS Configuration is Needed for This Task
Choosing the right VPS for LXD depends on the number and type of containers you plan to run. LXD itself is very lightweight, but each running container consumes resources.
Minimum Requirements for an LXD Host (excluding containers):
- CPU: 1-2 vCPU (for the host and a few lightweight containers). Modern processors with virtualization instructions (VT-x/AMD-V) are desirable, but LXD can work without them, albeit with lower performance.
- RAM: 2 GB (for the host and basic LXD operations). If you plan to run containers with databases or web servers, more will be needed.
- Disk: 40-60 GB NVMe SSD. LXD can work with HDDs, but SSDs significantly improve I/O performance, which is critical for containers. NVMe provides maximum speed.
- Network: 1 Gbps port (standard for most VPS).
Specific VPS Plan for the Task (e.g., for 3-5 containers: web server, database, VPN server):
- CPU: 4 vCPU
- RAM: 8 GB
- Disk: 160-200 GB NVMe SSD (including space for container data and snapshots)
- Network: 1 Gbps, unlimited traffic or with a high limit.
To rent a VPS with the specified characteristics, ensure that the provider offers NVMe SSD and sufficient cores/RAM for your needs. Most modern VPS providers offer such configurations.
When a dedicated server is needed, not a VPS:
If you plan to run dozens of containers, high-load databases, game servers with many players, or have very specific hardware requirements (e.g., GPU for machine learning), then you should consider a dedicated server. Dedicated servers provide you with all the physical resources of the machine, which eliminates "noisy neighbor" issues and delivers maximum performance. Dedicated servers also often have more flexible disk subsystem options (RAID, HDD+SSD).
Location: What it affects
The choice of VPS location plays an important role, especially for services critical to latency:
- Latency: The closer the server is to your target audience, the lower the latency and faster the response. For websites, this affects page load speed; for game servers, it affects ping.
- Geopolitical Factors and Legislation: Some countries have stricter laws regarding data storage or censorship. Consider this when choosing a location for sensitive data or services requiring a high degree of privacy.
- Cost: VPS prices can vary depending on the location due to differences in electricity costs, data center rent, and taxes.
Always choose a location that is geographically closer to the majority of your users or to you, if you are the primary consumer of the service.
Server Preparation
Before installing LXD, you need to perform basic configuration of your VPS to ensure security and stability. We will use Ubuntu Server 24.04 LTS as the main host operating system.
1. SSH Connection
Connect to your server as the root user or the user provided by your provider:
ssh root@YOUR_IP_ADDRESS
2. System Update
First, update all packages to their latest versions:
sudo apt update # Update package list
sudo apt upgrade -y # Upgrade installed packages without confirmation
sudo apt autoremove -y # Remove unnecessary dependencies
3. Creating a New User with Sudo Privileges (if not already done)
Working as root is insecure. Create a new user and grant them sudo privileges.
adduser your_username # Create a new user
usermod -aG sudo your_username # Add user to the sudo group
Now, exit the root session and log in as the new user:
exit
ssh your_username@YOUR_IP_ADDRESS
4. SSH Key Configuration (Recommended)
To enhance security, it is recommended to use SSH keys instead of passwords. If you don't have keys yet, generate them on your local machine:
# On your local machine
ssh-keygen -t rsa -b 4096 -C "[email protected]"
Then copy the public key to the server:
# On your local machine
ssh-copy-id your_username@YOUR_IP_ADDRESS
After verifying login with the key, disable password authentication in /etc/ssh/sshd_config:
sudo nano /etc/ssh/sshd_config
Find the lines and set the values:
PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM no
Restart the SSH service:
sudo systemctl restart sshd
5. Firewall Configuration (UFW)
Enable the UFW firewall and allow only the necessary ports (SSH, HTTP/HTTPS, as well as ports that will be used by containers).
sudo apt install ufw -y # Install UFW
sudo ufw allow OpenSSH # Allow SSH (port 22)
sudo ufw allow http # Allow HTTP (port 80)
sudo ufw allow https # Allow HTTPS (port 443)
sudo ufw enable # Enable UFW
sudo ufw status verbose # Check UFW status
Later, if your containers will use other ports, they will also need to be opened.
6. Installing Fail2Ban
Fail2Ban helps protect the server from brute-force attacks by blocking IP addresses from which numerous failed login attempts occur.
sudo apt install fail2ban -y # Install Fail2Ban
sudo systemctl enable fail2ban # Enable service autostart
sudo systemctl start fail2ban # Start the service
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local # Create local config
You can edit /etc/fail2ban/jail.local to configure rules, but the default settings for SSH are already quite good.
sudo systemctl restart fail2ban # Restart Fail2Ban after changes
Your server is now basically secured and ready for LXD installation.
Software Installation — Step-by-Step
Software Installation — Step-by-Step
Now that the server is prepared, let's proceed with LXD installation. We will use the snap package, as it is the recommended and most up-to-date method for installing LXD on Ubuntu.
1. Installing LXD via Snap
LXD on Ubuntu is delivered as a snap package, ensuring its up-to-dateness and isolation. For Ubuntu 24.04 LTS, snap is already installed by default.
sudo snap install lxd --channel=6.0/stable # Install LXD 6.0 LTS (current version for 2026)
After installation, add your user to the lxd group so you can manage LXD without sudo:
sudo usermod -aG lxd your_username # Add user to lxd group
newgrp lxd # Apply group changes without re-login (or just re-login)
Ensure that LXD is installed and accessible:
lxd --version # Check LXD version
2. Initializing LXD
After installing LXD, you need to initialize it by setting up basic storage and network configurations. This is an interactive process.
lxd init # Start interactive LXD initialization
During initialization, you will be asked the following questions. Here are the recommended answers for most VPS scenarios:
Would you like to use LXD clustering? (yes/no) [default=no]:no (clustering is not needed for a single VPS)Do you want to setup a new storage pool? (yes/no) [default=yes]:yesName of the new storage pool [default=default]:default (or any other name)Would you like to use an existing block device? (yes/no) [default=no]:no (if you don't have a separate disk)Would you like to use a new loop device? (yes/no) [default=yes]:yes (will create an image file for container storage)Size in GiB of the new loop device (minimum 4GiB) [default=100GiB]:50GiB (or more, depending on your disk size and plans. Leave some space for the host)Would you like to connect to a remote LXD daemon? (yes/no) [default=no]:noWould you like to setup a new network bridge? (yes/no) [default=yes]:yesWhat should the new bridge be called? [default=lxdbr0]:lxdbr0 (standard name)What IPv4 address should be used? (CIDR format, e.g. 10.0.0.1/24) [default=10.200.200.1/24]:10.200.200.1/24 (or any other private range that does not overlap with your local network)Would you like LXD to NAT traffic on its own? (yes/no) [default=yes]:yes (for containers to access the internet)What IPv6 address should be used? (CIDR format, e.g. fd42:42:42:42::1/64) [default=none]:none (if IPv6 is not needed)Would you like the LXD daemon to be available over the network? (yes/no) [default=no]:no (if you do not plan to manage LXD remotely)Would you like stale cached images to be updated automatically? (yes/no) [default=yes]:yesWould you like a YAML summary of your configuration? (yes/no) [default=yes]:yes
After initialization is complete, LXD is ready for use. You can check the network status:
lxc network list # View configured LXD networks
3. Launching the First Container
Now let's launch our first container. For example, Ubuntu 22.04 LTS.
lxc launch ubuntu:22.04 my-first-container # Launch a container with Ubuntu 22.04 LTS image named my-first-container
Check the container status:
lxc list # List all running containers
You will see the RUNNING status and the IP address assigned to the container on the lxdbr0 network.
4. Entering the Container and Basic Configuration
You can get a command line inside the container:
lxc exec my-first-container bash # Execute bash command inside the container
Inside the container, you can work as on a regular Ubuntu system. Update it:
apt update && apt upgrade -y # Update packages inside the container
exit # Exit the container
5. Configuring a Profile for Containers (Example)
LXD profiles allow you to apply standard settings (resources, network) to multiple containers. Let's create a profile for a web server.
lxc profile copy default webserver # Copy the default profile to a new 'webserver' profile
lxc profile edit webserver # Edit the new profile
In the opened YAML file, add or modify the limits section:
config:
limits.cpu: "2" # Limit CPU to 2 cores
limits.memory: "2GB" # Limit RAM to 2GB
limits.disk: "50GB" # Limit disk to 50GB (if using a pool with quotas)
user.user-data: | # Example cloud-init for initial setup
#cloud-config
runcmd:
- echo "Hello from cloud-init!" > /root/cloud-init-test.txt
packages:
- nginx
users:
- name: myuser
sudo: ALL=(ALL) NOPASSWD:ALL
groups: users, sudo
shell: /bin/bash
ssh_authorized_keys:
- ssh-rsa AAAAB3NzaC... your_public_key_here
Save and close the file. Now you can launch containers with this profile:
lxc launch ubuntu:22.04 my-web-server --profile webserver # Launch a container with the webserver profile
This ensures standardization and simplifies resource management.
6. Port Forwarding on the Host for Container Access
By default, containers have private IP addresses. To make a service inside a container accessible from the internet, you need to forward a port from the host to the container. For example, if Nginx in the my-web-server container listens on port 80, and you want it to be accessible on port 80 of your VPS:
lxc config device add my-web-server myport80 proxy listen=tcp:0.0.0.0:80 connect=tcp:10.200.200.X:80 # Forward port 80 from host to container
Replace 10.200.200.X with the actual IP address of your container (you can find it using lxc list). Don't forget to open port 80 in the host's UFW, if it's not already open.