关于 proxmox-admin
面向命令行的 Proxmox VE 管理技能,适用于管理虚拟化主机的管理员。它通过 Proxmox 命令行工具处理 KVM 虚拟机、LXC 容器、存储、网络、集群及常见故障。
主要功能
- 01使用 `qm` 管理 KVM 虚拟机
- 02使用 `pct` 管理 LXC 容器
- 03配置存储与网络
- 04管理 Proxmox 集群
- 05通过脚本配置来宾系统
- 06排查主机与来宾系统问题
适用场景
- 01创建和配置虚拟机
- 02部署和维护 LXC 容器
- 03自动化配置来宾系统
- 04排查迁移或存储错误
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ bastos/ skills --skill proxmox-admin安装后在 Claude Code 中输入/proxmox-admin确认已加载。
附带文件references/cli-cheat-sheet.md
详细介绍
Proxmox VE 管理
概述
Proxmox VE 是基于 Debian 构建的服务器虚拟化平台。它通过 Web UI 或命令行工具管理 KVM 虚拟机和 LXC 容器。本技能介绍使用 qm(虚拟机)、pct(容器)及配套工具进行基于命令行的管理。
适用场景
- 创建、配置或管理 KVM 虚拟机
- 创建和管理 LXC 容器
- 管理 Proxmox 存储、网络或集群
- 通过脚本自动预配虚拟机/容器
- 排查 Proxmox 主机或客户机问题
不适用于: 仅使用 Web UI 的工作流(请使用下文对应的命令行工具)。
快速参考
| 工具 | 用途 |
|---|---|
qm | 管理 KVM 虚拟机 |
pct | 管理 LXC 容器 |
pvesm | 管理存储 |
pvecm | 管理集群 |
pveam | 管理设备/模板下载 |
pvesh | 从 Shell 访问 Proxmox API |
pveperf | 测试主机性能 |
使用 qm 管理虚拟机
创建虚拟机
# Create a VM with ID 100
qm create 100 --name my-vm --memory 2048 --cores 2 --sockets 1 \
--net0 virtio,bridge=vmbr0 --ostype l26
# Create with SCSI disk on local-lvm storage (32GB)
qm create 100 --name my-vm --memory 4096 --cores 4 \
--scsi0 local-lvm:32 --scsihw virtio-scsi-pci \
--net0 virtio,bridge=vmbr0 --ostype l26
# Attach an ISO for installation
qm set 100 --cdrom local:iso/ubuntu-22.04-server.iso --boot order=ide2
虚拟机生命周期
| 命令 | 用途 |
|---|---|
qm start <vmid> | 启动虚拟机 |
qm shutdown <vmid> | 通过 ACPI 正常关机 |
qm stop <vmid> | 强制停止(类似拔掉电源) |
qm reboot <vmid> | 重启虚拟机 |
qm reset <vmid> | 硬重置 |
qm suspend <vmid> | 挂起到内存 |
qm resume <vmid> | 从挂起状态恢复 |
qm destroy <vmid> | 删除虚拟机及其磁盘 |
qm destroy <vmid> --purge | 删除虚拟机、磁盘及所有相关任务 |
虚拟机配置
# Show current config
qm config 100
# Modify hardware
qm set 100 --memory 8192
qm set 100 --cores 4
qm set 100 --balloon 2048 # dynamic memory (min)
qm set 100 --cpu cputype=host # pass through host CPU features
qm set 100 --machine q35 # use Q35 chipset (for PCIe passthrough)
# Add/resize disks
qm set 100 --scsi1 local-lvm:50 # add 50GB disk
qm disk resize 100 scsi0 +20G # grow existing disk by 20GB
# Networking
qm set 100 --net0 virtio,bridge=vmbr0,tag=10 # VLAN tagged
qm set 100 --net1 virtio,bridge=vmbr1 # second NIC
# Cloud-init (for automated provisioning)
qm set 100 --ide2 local-lvm:cloudinit
qm set 100 --ciuser admin --cipassword 'secret'
qm set 100 --ipconfig0 ip=10.0.0.50/24,gw=10.0.0.1
qm set 100 --sshkeys ~/.ssh/authorized_keys
qm set 100 --boot order=scsi0
# EFI / UEFI boot
qm set 100 --bios ovmf --efidisk0 local-lvm:1,efitype=4m,pre-enrolled-keys=1
# Serial console (headless)
qm set 100 --serial0 socket --vga serial0
# PCI passthrough (GPU, NIC, etc.)
qm set 100 --hostpci0 0000:01:00.0,pcie=1
快照和克隆
# Create a snapshot
qm snapshot 100 before-upgrade --description "Before kernel upgrade"
# List snapshots
qm listsnapshot 100
# Rollback to snapshot
qm rollback 100 before-upgrade
# Delete a snapshot
qm delsnapshot 100 before-upgrade
# Clone a VM (full copy)
qm clone 100 101 --name cloned-vm --full
# Clone as linked clone (shares base disk, faster)
qm clone 100 101 --name linked-vm
模板
# Convert VM to template (irreversible)
qm template 100
# Create VM from template (linked clone)
qm clone 100 200 --name from-template
# Create VM from template (full clone)
qm clone 100 200 --name from-template --full
迁移
# Online migration to another node
qm migrate 100 node2 --online
# Offline migration
qm migrate 100 node2
监控
# VM status
qm status 100
# List all VMs
qm list
# Show running processes/agent info
qm agent 100 ping
qm agent 100 get-osinfo
# Monitor interface (QEMU monitor)
qm monitor 100
使用 pct 管理容器
创建容器
# Download a template first
pveam update
pveam available --section system
pveam download local debian-12-standard_12.2-1_amd64.tar.zst
# Create container with ID 200
pct create 200 local:vztmpl/debian-12-standard_12.2-1_amd64.tar.zst \
--hostname my-ct --memory 1024 --cores 2 \
--rootfs local-lvm:8 \
--net0 name=eth0,bridge=vmbr0,ip=dhcp \
--password 'secret' --unprivileged 1
# Create with static IP
pct create 201 local:vztmpl/debian-12-standard_12.2-1_amd64.tar.zst \
--hostname web-ct --memory 2048 --cores 2 \
--rootfs local-lvm:16 \
--net0 name=eth0,bridge=vmbr0,ip=10.0.0.51/24,gw=10.0.0.1 \
--nameserver 1.1.1.1 --unprivileged 1
容器生命周期
| 命令 | 用途 |
|---|---|
pct start <ctid> | 启动容器 |
pct shutdown <ctid> | 正常关机 |
pct stop <ctid> | 强制停止 |
pct reboot <ctid> | 重启容器 |
pct destroy <ctid> | 删除容器及其卷 |
pct enter <ctid> | 打开容器内的 Shell |
pct exec <ctid> -- <cmd> | 在容器内运行命令 |
pct console <ctid> | 连接到容器控制台 |
容器配置
# Show config
pct config 200
# Modify resources
pct set 200 --memory 4096
pct set 200 --cores 4
pct set 200 --swap 1024
# Add mount point (bind mount from host)
pct set 200 --mp0 /mnt/data,mp=/data
# Add additional storage volume
pct set 200 --mp1 local-lvm:50,mp=/var/lib/data
# Networking
pct set 200 --net0 name=eth0,bridge=vmbr0,ip=10.0.0.60/24,gw=10.0.0.1
pct set 200 --net1 name=eth1,bridge=vmbr1,ip=dhcp
# Features (nesting, FUSE, NFS)
pct set 200 --features nesting=1
pct set 200 --features nesting=1,fuse=1,mount=nfs
# DNS
pct set 200 --nameserver "1.1.1.1 8.8.8.8" --searchdomain example.com
# Start on boot
pct set 200 --onboot 1 --startup order=1,up=30
容器快照和克隆
# Snapshot
pct snapshot 200 clean-install
# Rollback
pct rollback 200 clean-install
# Clone
pct clone 200 201 --hostname cloned-ct --full
存储管理
# List storage pools
pvesm status
# List content of a storage
pvesm list local
pvesm list local-lvm
# Add storage (examples)
pvesm add dir my-backup --path /mnt/backup --content backup
pvesm add nfs nfs-share --server 10.0.0.5 --export /exports/pve --content images,vztmpl
pvesm add lvm my-lvm --vgname my-vg --content rootdir,images
pvesm add zfspool my-zfs --pool rpool/data --content rootdir,images
# Remove storage
pvesm remove my-backup
# Download ISO
wget -P /var/lib/vz/template/iso/ https://example.com/image.iso
网络
# List network interfaces
cat /etc/network/interfaces
# Common bridge configuration (in /etc/network/interfaces)
# auto vmbr0
# iface vmbr0 inet static
# address 10.0.0.1/24
# bridge-ports eno1
# bridge-stp off
# bridge-fd 0
# Apply network changes
ifreload -a
集群管理
# Create a new cluster
pvecm create my-cluster
# Join an existing cluster
pvecm add 10.0.0.1
# Show cluster status
pvecm status
# List cluster nodes
pvecm nodes
# Remove a node (run from a remaining node)
pvecm delnode nodename
# Check quorum
pvecm expected 1 # force quorum (dangerous, single-node recovery only)
防火墙
# Start or stop the node firewall service
pve-firewall start
pve-firewall stop
pve-firewall status
# Enable datacenter firewall in /etc/pve/firewall/cluster.fw
# [OPTIONS]
# enable: 1
#
# Manage rules via config files. Add remote management allow rules before
# enabling restrictive policies.
# Datacenter: /etc/pve/firewall/cluster.fw
# Node: /etc/pve/nodes/<node>/host.fw
# VM/CT: /etc/pve/firewall/<vmid>.fw
备份和恢复
# Backup a VM
vzdump 100 --storage local --mode snapshot --compress zstd
# Backup a container
vzdump 200 --storage local --mode stop --compress zstd
# Backup all guests
vzdump --all --storage local --mode snapshot --compress zstd --mailto [email protected]
# Restore a VM
qmrestore /var/lib/vz/dump/vzdump-qemu-100-*.vma.zst 100
# Restore a container
pct restore 200 /var/lib/vz/dump/vzdump-lxc-200-*.tar.zst
# Restore to different storage
qmrestore /var/lib/vz/dump/vzdump-qemu-100-*.vma.zst 100 --storage local-lvm
常见预配模式
从模板创建 Cloud-Init 虚拟机
# 1. Create base VM and install OS, then convert to template
qm template 9000
# 2. Clone and customize with cloud-init
qm clone 9000 110 --name web-server --full
qm set 110 --ciuser deploy --sshkeys ~/.ssh/authorized_keys
qm set 110 --ipconfig0 ip=10.0.0.110/24,gw=10.0.0.1
qm set 110 --nameserver 1.1.1.1
qm start 110
批量创建容器
for i in $(seq 1 5); do
CTID=$((300 + i))
pct create $CTID local:vztmpl/debian-12-standard_12.2-1_amd64.tar.zst \
--hostname "worker-${i}" --memory 1024 --cores 2 \
--rootfs local-lvm:8 \
--net0 name=eth0,bridge=vmbr0,ip=10.0.0.$((60 + i))/24,gw=10.0.0.1 \
--unprivileged 1 --start 1
done
导入磁盘映像(例如云映像)
# Download a cloud image
wget https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img
# Import to a VM
qm disk import 100 jammy-server-cloudimg-amd64.img local-lvm
# Attach the imported disk
qm set 100 --scsi0 local-lvm:vm-100-disk-0
qm set 100 --boot order=scsi0
故障排除
| 问题 | 解决方案 |
|---|---|
| 虚拟机无法启动 | 检查 qm config <vmid>,使用 pvesm status 确认存储存在 |
| “TASK ERROR: can't lock file” | 先检查正在运行的任务;如果任务已结束,使用 qm unlock <vmid> 或 pct unlock <ctid> |
| 容器没有网络 | 检查网桥是否存在:brctl show;确认防火墙规则 |
| 存储空间已满 | 使用 pvesm status 检查使用情况;使用 lvs 检查 LVM 精简池 |
| 集群失去仲裁 | 在存活节点上运行 pvecm expected 1(仅用于单节点恢复) |
| 迁移失败 | 确保 CPU 类型相同,或使用 --online 进行在线迁移;检查节点间的网络 |
| 备份因锁定错误而失败 | 使用 qm unlock <vmid> 或 pct unlock <ctid> |
| 虚拟机磁盘 I/O 缓慢 | 使用 --scsihw virtio-scsi-single 和 ,iothread=1,discard=on 等磁盘选项 |
| 客户机 Agent 无响应 | 在虚拟机中安装 qemu-guest-agent 并启用:qm set <vmid> --agent 1 |
常用路径
| 路径 | 内容 |
|---|---|
/etc/pve/ | 集群范围的配置(pmxcfs) |
/etc/pve/qemu-server/<vmid>.conf | VM 配置文件 |
/etc/pve/lxc/<ctid>.conf | 容器配置文件 |
/etc/pve/storage.cfg | 存储定义 |
/etc/pve/nodes/ | 各节点的配置 |
/var/lib/vz/ | 默认本地存储根目录 |
/var/lib/vz/template/iso/ | ISO 镜像 |
/var/lib/vz/template/cache/ | 容器模板 |
/var/lib/vz/dump/ | 备份文件 |
/var/log/pve/tasks/ | 任务日志 |
评论
支持 Markdown