Cover.jpeg
Cover.jpeg


Using pct in Proxmox VE

Prox­mox VE (PVE) is a pow­er­ful open-source vir­tu­al­iza­tion plat­form that sup­ports both LXC con­tain­ers and KVM vir­tual ma­chines. The pct com­mand line tool is specif­i­cally de­signed to man­age LXC con­tain­ers. Be­low is a de­tailed step-by-step guide to help mas­ter the pct com­mand.


1. Prerequisites

Be­fore us­ing pct, en­sure:

2025-02-14_templates .png
2025-02-14_templates .png

  • Necessary OS templates (e.g., Ubuntu, Debian, CentOS) are downloaded from the Proxmox template repository.

2. Basic Commands

2.1 Create a New Container

To cre­ate a new LXC con­tainer, use the pct create com­mand.

pct create <vmid> <ostemplate> [options]

Example:

pct create 100 local:vztmpl/ubuntu-22.04-standard_22.04-1_amd64.tar.gz \
  --storage local-lvm \
  --rootfs 8G \
  --hostname mycontainer \
  --memory 1024 \
  --cores 1 \
  --net0 name=eth0,bridge=vmbr0,ip=dhcp

Explanation:

  • 100: The unique ID for the container.
  • local:vztmpl/ubuntu-22.04-standard_22.04-1_amd64.tar.gz: The OS template to use.
  • --storage local-lvm: The storage where the container will be created.
  • --rootfs 8G: Allocate 8GB of disk space for the root filesystem.
  • --hostname mycontainer: Set the hostname for the container.
  • --memory 1024: Allocate 1024MB of RAM.
  • --cores 1: Assign 1 CPU core.
  • --net0: Configure the network interface (DHCP in this case).

2.2 Start a Container

To start a con­tainer, use the pct start com­mand.

pct start <vmid>

Example:

pct start 100

2.3 Stop a Container

To stop a run­ning con­tainer, use the pct stop com­mand.

pct stop <vmid>

Example:

pct stop 100

2.4 Restart a Container

To restart a con­tainer, use the pct restart com­mand.

pct restart <vmid>

Example:

pct restart 100

2.5 Access the Container's Shell

To ac­cess the con­tain­er's shell, use the pct enter com­mand.

pct enter <vmid>

Example:

pct enter 100

2.6 List All Containers

To list all con­tain­ers on the node, use the pct list com­mand.

pct list

Output Example:

VMID       Status     Lock         Name
100        running                 lxc-100
101        stopped                 lxc-101

2.7 Modify Container Configuration

To mod­ify a con­tain­er's con­fig­u­ra­tion, use the pct set com­mand.

pct set <vmid> [options]

Example (Increase Memory Limit):

pct set 100 --memory 2048

Example (Add a Mount Point):

pct set 100 --mp0 /mnt/data,/mnt/data,ro

2.8 Clone a Container

To clone an ex­ist­ing con­tainer, use the pct clone com­mand.

pct clone <source-vmid> <new-vmid> [options]

Example:

pct clone 100 101 --storage local-lvm

2.9 Backup a Container

To cre­ate a backup of a con­tainer, use the vzdump com­mand.

Example:

2025-02-14_backup.png
2025-02-14_backup.png

Backup Con­tainer ID 302 (Stopped Mode)

vzdump 302 --compress zstd --mode stop --storage local

Backup Con­tainer ID 101 (Snap­shot Mode)

vzdump 101 --compress lzo --mode snapshot --storage nas_backup

--compress: gzip (stan­dard), lzo (fast), zstd (best bal­ance)

--mode:

  • stop (service interruption, maximum consistency)
  • snapshot (live backup, requires supported filesystem)

--storage: Tar­get stor­age ID (view with pvesm list)

Multi LXC Backup

vzdump 100 101 102 --mode stop --storage local --compress zstd

2.10 Restore a Container

To re­store a con­tainer from a backup, use the pct restore com­mand.

pct restore <vmid> <backup-file> [options]

Example:

pct restore 100 /var/lib/vz/dump/vzdump-lxc-100-2023_10_01-12_00_01.tar.gz

2.11 Destroy a Container

To per­ma­nently delete a con­tainer.

pct destroy <vmid> --purge
  • <vmid>: The ID of the container to be destroyed.
  • --purge: Removes all associated data, including backups and configuration files.

Example:

pct destroy 100 --purge

3. Advanced Usage

3.1 Mount Host Directories

Di­rec­to­ries from the host can be mounted into the con­tainer.

Example:

pct set 100 --mp0 /mnt/host-data,/mnt/container-data,ro

3.2 Configure Networking

Sta­tic IPs or ad­di­tional net­work in­ter­faces can be con­fig­ured.

Example (Static IP):

pct set 100 --net0 name=eth0,bridge=vmbr0,ip=192.168.1.100/24,gw=192.168.1.1

Set VLAN

Static IP Example:

pct set 100 --net0 name=eth0,bridge=vmbr0,tag=10,ip=192.168.10.100/24,gw=192.168.10.1
  • Sets the VLAN tag to 10 for the eth0 interface.
  • Connects the interface to the vmbr0 bridge.
  • Assigns a static IP address 192.168.10.100/24 and gateway 192.168.10.1.

DHCP Example:

pct set 100 --net0 name=eth0,bridge=vmbr0,tag=10,ip=dhcp
  • Multiple network interfaces can be configured by incrementing the --netX option (e.g., --net1, --net2).
  • Ensure the VLAN is properly configured on the Proxmox host's network bridge (e.g., vmbr0 or vmbr1).

3.3 Resource Limits

CPU and mem­ory lim­its can be set for the con­tainer.

Example:

pct set 100 --memory 4096 --cores 2

4. Troubleshooting

  • Container Fails to Start: Check logs using pct logs <vmid>.
  • Network Issues: Verify the network configuration with pct config <vmid>.

5. Help and Documentation

  • Use pct help to see a list of available commands.
  • For detailed documentation, use man pct.

6. Automate to creation of an LXC container in Proxmox VE (PVE).

Script Overview

  1. Prompts the user for input (LXC ID and password).
  2. Checks if the LXC ID already exists and handles conflicts.
  3. Downloads the LXC template if it doesn't already exist.
  4. Creates a new LXC container with specified configurations.
  5. Appends additional configuration to the container's configuration file.
wget -q https://kingtam.win/usr/uploads/script/lxc-create.sh -O lxc-create.sh && chmod +x lxc-create.sh && bash ./lxc-create.sh

Conclusion

The pct com­mand line tool is a pow­er­ful way to man­age LXC con­tain­ers in Prox­mox VE. With this guide, cre­at­ing, con­fig­ur­ing, and man­ag­ing con­tain­ers ef­fi­ciently should be straight­for­ward. For fur­ther ques­tions or as­sis­tance, ad­di­tional re­sources are avail­able in the Prox­mox doc­u­men­ta­tion.


Related