7.4. Opening a virtual machine serial console by using the command line
By using the virsh console command, it is possible to connect to the serial console of a virtual machine (VM).
This is useful when the VM: * Does not provide VNC protocols, and thus does not offer video display for GUI tools.
- Does not have a network connection, and thus cannot be interacted with using SSH.
Prerequisites
The GRUB boot loader on your host must be configured to use serial console. To verify, check that the
/etc/default/grubfile on your host contains theGRUB_TERMINAL=serialparameter.$ sudo grep GRUB_TERMINAL /etc/default/grub GRUB_TERMINAL=serialThe VM must have a serial console device configured, such as
console type='pty'. To verify, do the following:# virsh dumpxml vm-name | grep console <console type='pty' tty='/dev/pts/2'> </console>The VM must have the serial console configured in its kernel command line. To verify this, the
cat /proc/cmdlinecommand output on the VM should include console=<console-name>, where <console-name> is architecture-specific:For AMD64 and Intel 64:
ttyS0注意The following commands in this procedure use
ttyS0.# cat /proc/cmdline BOOT_IMAGE=/vmlinuz-6.12.0-0.el10_0.x86_64 root=/dev/mapper/rhel-root ro console=tty0 console=ttyS0,9600n8 rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgbIf the serial console is not set up properly on a VM, using virsh console to connect to the VM connects you to an unresponsive guest console. However, you can still exit the unresponsive console by using the Ctrl+] shortcut.
To set up serial console on the VM, do the following:
On the VM, enable the
console=ttyS0kernel option:# grubby --update-kernel=ALL --args="console=ttyS0"Clear the kernel options that might prevent your changes from taking effect.
# grub2-editenv - unset kernelopts- Reboot the VM.
The
serial-getty@<console-name>service must be enabled. For example, on AMD64 and Intel 64:# systemctl status serial-getty@ttyS0.service ○ serial-getty@ttyS0.service - Serial Getty on ttyS0 Loaded: loaded (/usr/lib/systemd/system/serial-getty@.service; enabled; preset: enabled)
Procedure
On your host system, use the
virsh consolecommand. The following example connects to the guest1 VM, if thelibvirtdriver supports safe console handling:# virsh console guest1 --safe Connected to domain 'guest1' Escape character is ^] Subscription-name Kernel 3.10.0-948.el7.x86_64 on an x86_64 localhost login:- You can interact with the virsh console in the same way as with a standard command-line interface.