How to run shell in virsh domain / VM
To run a shell in a virsh domain (VM), you can use the virsh console
command. This allows you to connect to the console of a running virtual machine. Here’s how to do it:
- Open a terminal on your host machine.
- List the running domains to find the name or ID of the VM you want to connect to:
virsh list
- Connect to the console of the desired VM using its name or ID:
virsh console <domain-name-or-id>
- Exit the console when you’re done by pressing
Ctrl+]
(this is the default escape sequence for virsh console).
If virsh console
does not work
If virsh console
does not work, it might be due to the VM not having a console configured on its /dev/ttyS0
.
In that case, you can only connect to the VM using SSH if it has an SSH server running. You can do this by using the method from libvirt/virsh: How to list all domains/VMs with their IP addresses:
virsh net-dhcp-leases default
Example output:
Expiry Time MAC address Protocol IP address Hostname Client ID or DUID
-----------------------------------------------------------------------------------------------------------------------------------------------------
2025-02-05 02:11:17 4c:68:29:d5:aa:55 ipv4 192.168.122.73/24 abcdefghi-vm01 ff:b5:5e:67:ff:00:02:00:00:ab:11:53:fb:4f:97:fb:ea:c5:13
2025-02-05 02:06:19 52:54:00:12:34:56 ipv4 192.168.122.76/24 test1234-vm ff:40:56:63:a2:00:02:00:00:ab:11:39:d1:c8:61:96:3d:82:64
Now you can SSH into the VM using the IP address shown in the output:
If connecting via SSH does not work for you
In that case you need to configure the serial console and restart the VM.
Follow the method from Ravada in order to do that.
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow