Bare Metal Cloud – Part 4 – Getting a Serial Console When the Network is Broken

When you are working with virtual machines in the cloud, I’m sure you’ve come across a situation in which you thought you had a great idea to reconfigure the network stack and ended up with the VM not being reachable anymore. As a last resort, one can always turn to the virtual machine manager and open a virtual display and log-in. When it comes to KVMs Virtual Machine Manager GUI, however, the display functionality is quite rudimentary, one can’t even copy/paste text to and from it. Especially when making larger changes on a config file, that’s quite a showstopper. But there’s another way: A virtual serial console!

By default, VMM configures a virtual serial interface for a virtual machine as shown in the screenshot on the left. Nothing much is done with it by default. However, one can put a console on the serial interface that can then be accessed from the host operating system. In the virtual machine, the console is activated with the following command:

sudo systemctl enable --now serial-getty@ttyS0.service

Yes, that is quite a bit to type in, but the result is well worth the effort if a lot of things have to be done in a virtual machine with broken network connectivity.

On the host, the console of the virtual machine can now be accessed over the virtual serial interface as follows:

# Get the names of all VMs currently running
virsh list

# Open the console
virsh console NAME-OF-VM

Once in the console, pressing enter then shows the login prompt. After login, the console is available with a default number of lines and columns. Unfortunately, the local shell window size and any resize actions are not taken into account. But there’s a fix for that as well. In the console, type in the following command and be careful to use the correct single quote characters that go from the upper left to the lower right:

eval `resize`

This command automagically synchronizes the local and remote shell dimensions. A little quirk: Resize requires the xterm package to be installed in the virtual machine. By default, xterm is often not installed however, and one has to grab it with an apt install xterm. That’s a bit difficult without network connectivity, however. But even without adapting the window size, the console over the virtual serial connection is much more useful than the virtual screen of the VM in the virtual machine manager, as one can copy/paste from and to it!

And finally, CTRL-5 (not F5!!!) quits the console and returns to the host’s shell!