8 个用于在命令行上管理虚拟机的 Linux virsh 子命令
虚拟 shell 或 virsh是一种灵活的命令行实用程序,用于管理由libvirt控制的虚拟机 (VM) 。libvirt 是一种用于管理虚拟化平台的工具包和 API。它是基于 Linux 内核的虚拟机 (KVM) 的默认管理工具,还支持 Xen、VMware 和其他平台。
该virsh
命令允许您以交互方式或批量管理虚拟机。它还有助于从 Linux shell 控制虚拟机并与脚本或自动化工具集成。通过使用virsh
,您可以使用安全 shell (SSH) 快速连接到服务器并在虚拟机上执行操作,而无需访问图形界面。
当您在virsh
没有任何选项的情况下运行时,它会尝试连接到本地虚拟机管理程序。对于 Linux,默认连接指向本地 QEMU 系统以管理本地 KVM 计算机。您还可以使用选项-c
或--connect
并使用 libvirt 的语法指定远程虚拟机管理程序的统一资源标识符 (URI) 来连接到远程虚拟机管理程序。有关更多信息,请参阅 libvirt 的 URI规范。
默认情况下,virsh
提供数百个子命令和选项,可让您管理虚拟化平台或虚拟机的各个方面。在本文中,我将分享 virsh
我最常用的八个子命令。由于日常工作的性质,这些子命令中的大多数直接应用于虚拟机(或libvirt 术语中的域virsh
),但 也有用于管理平台本身的命令,例如添加存储池、网络等。
虚拟列表
virsh list
是一个基本命令,用于列出所有正在运行的域 (VM)。您还可以通过添加选项列出所有已配置的 VM --all
。如果您想查看目标虚拟机管理程序中配置的所有 VM 以便在后续命令中使用,这将非常有用。例如,要列出本地 Linux KVM 虚拟机管理程序上所有可用的虚拟机:
# virsh list --all
Id Name State
------------------------------
3 rh8-vm01 running
- crc shut off
- rh8-tower01 shut off
您可以使用该ID或域名作为后续命令的输入。
virsh 启动/重启/关闭
尽管它们不同,但我将start
、reboot
和shutdown
子命令分组,因为它们执行管理虚拟机电源状态的相同基本操作。
要关闭虚拟机rh8-vm01
(从list
上面的命令):
# virsh shutdown rh8-vm01
Domain 'rh8-vm01' is being shutdown
# virsh list
Id Name State
--------------------
您可以使用子命令重新启动它start
:
# virsh start rh8-vm01
Domain 'rh8-vm01' started
# virsh list
Id Name State
--------------------------
4 rh8-vm01 running
By using these subcommands, you can quickly start, reboot, or shut down a VM without having to fire up a heavy graphical application.
virsh dumpxml
The dumpxml
subcommand dumps the XML configuration for a given domain. You can use it to export the configuration to a file to make changes to an existing VM or use it as a template to create another VM with a similar configuration. By default, it dumps the configuration to STDOUT, so redirect it to a file using shell redirect operators >
to save to a file:
# virsh dumpxml rh8-vm01
<domain type='kvm' id='4'>
<name>rh8-vm01</name>
<uuid>53b92c48-fce3-4464-95bf-6f442e988c94</uuid>
<metadata>
<libosinfo:libosinfo xmlns:libosinfo="http://libosinfo.org/xmlns/libvirt/domain/1.0">
<libosinfo:os id="http://redhat.com/rhel/8.4"/>
</libosinfo:libosinfo>
</metadata>
<memory unit='KiB'>4194304</memory>
<currentMemory unit='KiB'>4194304</currentMemory>
<vcpu placement='static'>2</vcpu>
... TRUNCATED OUTPUT ...
</domain>
You can use the XML output in scripts and automation tools to automate creating VMs.
virsh domifaddr
The domifaddr
subcommand lists all the IP addresses configured for all virtual interfaces in a given VM. It's useful if the VM uses dynamic IP addresses, as it allows you to see the assigned IP address and connect to the VM:
# virsh domifaddr rh8-vm01
Name MAC address Protocol Address
-------------------------------------------------------------------------------
vnet6 52:54:00:c8:17:6e ipv4 192.168.122.19/24
vnet7 52:54:00:04:4d:ac ipv4 192.168.64.210/24
By default, it lists the IP address leased by a DHCP server. If the hypervisor does not provide this information, you can also use the option --source agent
to query the guest operating system (OS) directly via the virtualization agent. This requires a virtualization agent installed in the guest OS.
virsh edit
The edit
subcommand opens the current XML configuration in your default $EDITOR
, allowing you to make live modifications in a VM:
# virsh edit rh8-vm01
<domain type='kvm'>
<name>rh8-vm01</name>
<uuid>53b92c48-fce3-4464-95bf-6f442e988c94</uuid>
<metadata>
... TRUNCATED OUTPUT ...
</domain>
After making your modifications, save the file to apply them. Some modifications may only take effect after a reboot.
virsh net-edit
The net-edit
subcommand allows you to make live modifications to a virtual network configuration. It's helpful for changing options to a virtual network, such as associating a given MAC address to an IP address using standard libvirt
DHCP configuration. It's a more advanced command that I use regularly. Like the edit
subcommand, it opens the configuration file in your default $EDITOR
for making changes. Save the file to apply changes:
# virsh net-edit --network hostonly
<network>
<name>hostonly</name>
<uuid>eddd03ff-5825-42ef-bc99-968bddf773c2</uuid>
<bridge name='virbr1' stp='on' delay='0'/>
<mac address='52:54:00:67:75:b1'/>
<domain name='hostonly'/>
<ip address='192.168.64.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.64.128' end='192.168.64.254'/>
</dhcp>
</ip>
</network>
You can also use other network-related subcommands starting with net-
to manage different aspects of the hypervisor virtual networks.
What's next?
virsh is a powerful and flexible utility that allows you to manage every aspect of Linux virtualization and other platforms. For more information about it, consult the man pages or check its online documentation.