YouTip LogoYouTip

Linux Comm Lxc

Linux lxc Commands

Linux lxc Commands

Linux Command Manual Linux Command Manual


LXC (Linux Containers) Overview

LXC (Linux Containers) is an operating system-level virtualization technology that allows multiple isolated Linux environments (containers) to run on a single Linux system. Unlike traditional virtual machines, LXC containers share the host system's kernel, making them more lightweight and efficient.

LXC provides isolation close to that of virtual machines while maintaining near-native performance. It serves as the foundation for container technologies such as Docker.

Basic Concepts of LXC

  • Container: A lightweight, isolated process space with its own file system, network configuration, and process tree.
  • Template: A predefined configuration and file system layout used to create containers. LXC offers various templates, such as ubuntu, debian, centos, etc.
  • Cgroups: A Linux kernel feature used to limit, account for, and isolate resource usage (CPU, memory, disk I/O, etc.) of process groups.
  • Namespaces: Linux kernel features providing process isolation, including PID, network, mount points, UTS namespaces, and more.

LXC Command Syntax

The basic command format is:

lxc  

Common global options include:

  • --debug: Enables debug output.
  • --logfile=: Specifies a log file.
  • --version: Displays version information.
  • --help: Shows help information.

Common LXC Commands

Container Management

  • Create Container:
  • lxc-create -n  -t  
    
  • Start Container:
  • lxc-start -n  
    
  • Stop Container:
  • lxc-stop -n 
    
  • Delete Container:
  • lxc-destroy -n 
    

Container Information

  • List Containers:
  • lxc-ls 
    
  • View Container Status:
  • lxc-info -n 
    
  • Access Container Console:
  • lxc-console -n 
    

Container Configuration

  • Copy Container:
  • lxc-copy -n  -N 
    
  • Freeze/Unfreeze Container:
  • lxc-freeze -n 
    lxc-unfreeze -n 
    
  • Modify Container Configuration:
  • lxc-config -n -s =
    

LXC Configuration Files

Each container's configuration file is located at:

/var/lib/lxc//config

Common configuration items include:

  • Network Configuration:
  • lxc.net.0.type = veth
    lxc.net.0.link = lxcbr0
    lxc.net.0.flags = up
    
  • Resource Limits:
  • lxc.cgroup.cpu.shares = 512
    lxc.cgroup.memory.limit_in_bytes = 512M
    

Practical Examples

Example 1: Create and Run an Ubuntu Container

# Create Container
lxc-create -n myubuntu -t ubuntu --release jammy

# Start Container
lxc-start -n myubuntu -d

# Enter Container Console
lxc-console -n myubuntu

# Execute Commands Inside Container
lxc-attach -n myubuntu -- apt update

Example 2: Limit Container Resources

Edit the container configuration file /var/lib/lxc/myubuntu/config and add:
lxc.cgroup.cpu.shares = 256
lxc.cgroup.memory.limit_in_bytes = 256M

Then restart the container to apply the changes:
lxc-stop -n myubuntu
lxc-start -n myubuntu -d

Troubleshooting Common Issues

  • Issue 1: Container Cannot Start
  • Check logs: cat /var/log/lxc/.log
    Ensure kernel supports LXC: grep CONFIG_CGROUP /boot/config-$(uname -r)
    
  • Issue 2: Network Connection Problems
  • Check bridged networks: brctl show
    Ensure lxc-net is installed: apt install lxc-net
    
  • Issue 3: Permission Problems
  • Run commands as root user
    Or add user to lxc group: usermod -aG lxc 
    

Advanced Usage

Using the LXC API

LXC provides Python bindings, allowing programmatic management of containers:

import lxc

container = lxc.Container("mycontainer")

if not container.defined:
    container.create("ubuntu",{"release": "jammy"})
    container.start()

Container Snapshots

lxc-snapshot -n 
lxc-snapshot -n -r snap0

Container Migration

# On Source Host
lxc-checkpoint -n -D/path/to/dump

# On Target Host
lxc-restore -n -D/path/to/dump

Conclusion

LXC offers a lightweight virtualization solution ideal for scenarios requiring isolation without the overhead of full virtual machines. By mastering LXC commands, you can efficiently create, manage, and maintain Linux containers. With deeper understanding, you can explore advanced features like custom configurations, resource limits, and container orchestration.

Linux Command Manual Linux Command Manual

← Linux Comm GpgLinux Comm Nm β†’