Virtual machine console access in Linux KVM

One of the things I always like about using VMWare's ESXi servers was the ability to access the console of the virtual machine (VM).

This ability also exists when using Linux KVM and Linux VMs, and is pretty simple to set up.

Bonus points after a small config change, it's much more usable.

Enable the console from within the virtual machine

From within the Linux virtual machine (this is from an Ubuntu VM):

sudo systemctl enable serial-getty@ttyS0.service --now
Created symlink /etc/systemd/system/getty.target.wants/serial-getty@ttyS0.service → /lib/systemd/system/serial-getty@.service.

Note the '--now' starts the service when enabling it

Verify configuration from the KVM host

From the KVM host, verfy the console settings:

virsh dumpxml ubuntu20.04 | egrep 'serial|console'
    <controller type='virtio-serial' index='0'>
      <alias name='virtio-serial0'/>
    <serial type='pty'>
      <target type='isa-serial' port='0'>
        <model name='isa-serial'/>
      <alias name='serial0'/>
    </serial>
    <console type='pty' tty='/dev/pts/6'>
      <target type='serial' port='0'/>
      <alias name='serial0'/>
    </console>
      <address type='virtio-serial' controller='0' bus='0' port='1'/>
      <address type='virtio-serial' controller='0' bus='0' port='2'/>

Access the console of the Linux VM from the KVM host

Now, also from the KVM host, access the console of the Linux VM (named ubuntu20.04):

virsh console ubuntu20.04
Connected to domain ubuntu20.04
Escape character is ^]

ubuntu-20 login: tom
Password:
Welcome to Ubuntu 20.04.4 LTS (GNU/Linux 5.13.0-40-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

0 updates can be applied immediately.

Your Hardware Enablement Stack (HWE) is supported until April 2025.
Last login: Sat Apr 23 10:50:03 MDT 2022 from 192.168.1.30 on pts/1

Check to see who is logged in:

who
tom      :0           2022-04-23 10:31 (:0)
tom      pts/1        2022-04-23 10:50 (192.168.1.30)
tom      ttyS0        2022-04-23 10:52

Logins explained

  • :0 - Desktop enviornment open
  • :pts/1 - SSH login
  • ttyS0 - this console login

Modify the console settings to make it more usable

The console, by default will only display 80 characters across, and with todays higher resolution monitors, this makes looking at longer lines, especially from log files, a lot more difficult.

Example, using the journalctl command:

journalctl
Apr 22 17:27:01 ubuntu-20 kernel: x86/fpu: Supporting XSAVE feature 0x001: 'x87>
Apr 22 17:27:01 ubuntu-20 kernel: x86/fpu: Supporting XSAVE feature 0x002: 'SSE>
Apr 22 17:27:01 ubuntu-20 kernel: x86/fpu: Supporting XSAVE feature 0x004: 'AVX>
Apr 22 17:27:01 ubuntu-20 kernel: x86/fpu: xstate_offset[2]:  576, xstate_sizes>
Apr 22 17:27:01 ubuntu-20 kernel: x86/fpu: Enabled xstate features 0x7, context>
Apr 22 17:27:01 ubuntu-20 kernel: BIOS-provided physical RAM map:

Check the default console settings, from within the Linux VM:

stty -a | grep columns
speed 9600 baud; rows 24; columns 80; line = 0;

To change the number of rows displayed by the console:

stty rows 60

To change the number of columns displayed by the console:

stty columns 140

Verify the changes:

stty -a | grep columns
speed 9600 baud; rows 46 columns 140; line = 0;

Now we would get 140 characters across:

journalctl
Apr 22 17:27:01 ubuntu-20 kernel: Linux version 5.13.0-40-generic (buildd@ubuntu) (gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0, GNU ld (GNU B>
Apr 22 17:27:01 ubuntu-20 kernel: Command line: BOOT_IMAGE=/boot/vmlinuz-5.13.0-40-generic root=UUID=07c35e3d-bbe4-47eb-9b65-53398290ca51 r>

The number of rows and columns can be changed to suite your needs.

Making the changes survive a reboot:

printf "stty rows 60\nstty columns 140\n" >> ~/.bash_profile

The resize command, from the xterm package, can automatically resize your columns to maximize the amount of data per line automatically, based on the width of your screen.

The downside to useing resize is that you need to do it each time you log in.

eval `/usr/bin/resize`

Examples, with different size (width) screens:

Resized 97 character width screen

Resized 157 character width screen

The ability to access a VM's console is a game changer for various reasons, among which:

  • Access VM without having OpenSSH installed
  • Access VM if there are network issues
  • Access VM if tests require no networking to be configured
  • Much easier time troubleshooting any issue, as the console is much more available than other access methods.

References

Ubuntu manuals - resize http://manpages.ubuntu.com/manpages/focal/man1/resize.1.html