Thursday, May 28, 2020

Enabling Serial Console on Debian Linux

I wanted to enable the serial console on a Linux system. The system is a Debian Linux release 10 (a.k.a., "buster"). The official documentation has the following description about the serial console

"
If you are booting with a serial console, generally the kernel will autodetect this. If you have a videocard (framebuffer) and a keyboard also attached to the computer which you wish to boot via serial console, you may have to pass the console=device argument to the kernel, where device is your serial device, which is usually something like ttyS0

You may need to specify parameters for the serial port, such as speed and parity, for instance console=ttyS0,9600n8; other typical speeds may be 57600 or 115200. Be sure to specify this option after ---, so that it is copied into the bootloader configuration for the installed system (if supported by the installer for the bootloader). 

In order to ensure the terminal type used by the installer matches your terminal emulator, the parameter TERM=type can be added. Note that the installer only supports the following terminal types: linux, bterm, ansi, vt102 and dumb. The default for serial console in debian-installer is vt102. If you are using an IPMI console, or a virtualization tool which does not provide conversion into such terminals types itself, e.g. QEMU/KVM, you can start it inside a screen session. That will indeed perform translation into the screen terminal type, which is very close to vt102.

"

It also states,

"
Those used to change inittab to enable/disable virtual or serial consoles will notice that that file is gone from clean installs. This is all managed through systemd directly now. For example, you can enable a serial console on COM1 with: 

systemctl enable serial-getty@ttyS0.service
systemctl start serial-getty@ttyS0.service
 

However, it is generally preferable to add console=ttyS0 on the kernel commandline, since this also enables kernel output on reboots. This is done by adding the following to /etc/default/grub
 
GRUB_CMDLINE_LINUX="console=ttyS0"
 
... and running update-grub. This will take effect only on the next reboot, however.

"

It is clearly that the documentation offers two solutions, using the systemd service or using the boot parameter. Perhaps, the former has the shortcoming that the serial console may not start if the systemd does not get a chance to run because we happen to have a boot problem. The preferred method, as suggested is to pass a boot parameter to the kernel. Since I have the grub boot loader installed, this becomes a simple two step approach,

  1. Edit /etc/default/grub file. In my case, replace
    
    GRUB_CMDLINE_LINUX_DEFAULT="quiet"
    

    by
    
    GRUB_CMDLINE_LINUX_DEFAULT="console=ttyS0 console=tty0"
    

  2. Update the grub.cfg file
    
    update-grub -o /boot/grub/grub.cfg
    

I happened to back up the old grub.cfg file. By comparing the old and new grub.cfg file, we can easily see the difference between the two files,

$ diff /boot/grub/grub.cfg /boot/grub/grub.cfg.bu01
diff /boot/grub/grub.cfg /boot/grub/grub.cfg.bu01
119c119
<       linux   /boot/vmlinuz-4.19.0-8-686 root=UUID=123456789 ro  console=ttyS0 console=tty0
---
>       linux   /boot/vmlinuz-4.19.0-8-686 root=UUID=123456789 ro  quiet
137c137
<               linux   /boot/vmlinuz-4.19.0-8-686 root=UUID=123456789 ro  console=ttyS0 console=tty0
---
>               linux   /boot/vmlinuz-4.19.0-8-686 root=UUID=123456789 ro  quiet
$

1 comment:

  1. Thank you for the nice HOWTO. It helped me bringing by serial console to life :o)

    ReplyDelete