Contents

Enable serial console on Ubuntu 20.04

/ubuntu_console/2020-08-01-21-44-00.png
serial console

にほんご

1
2
3
sudo cp -nv /etc/default/grub   /etc/default/grub.orig
sudo cp -nv /boot/grub/grub.cfg /boot/grub/grub.cfg.orig
sudo vim /etc/default/grub

/etc/default/grub:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
## remove (optional):
# GRUB_TIMEOUT_STYLE=hidden

## change:
# (optional)
# GRUB_TIMEOUT=0
GRUB_TIMEOUT=2

## add:
GRUB_TERMINAL="console serial"
GRUB_SERIAL_COMMAND="serial --speed=115200"

## change:
# GRUB_CMDLINE_LINUX=""
GRUB_CMDLINE_LINUX="console=tty1 console=ttyS0,115200"

Detailed explanation

By removing GRUB_TIMEOUT_STYLE=hidden and giving GRUB_TIMEOUT=2, GRUB menu appears on boot.

/ubuntu_console/2020-08-01-21-53-43.png
GRUB menu

Even with GRUB_TIMEOUT_STYLE=hidden, you can show this menu by pressing Escape key if GRUB_TIMEOUT is not zero (ref.).

With GRUB_TERMINAL="console serial", GRUB menu appears in both VGA console and serial terminal.

/ubuntu_console/2020-08-01-21-54-07.png
GRUB menu in both VGA console and serial terminal

If you can’t see the serial output, consult the manual and adjust GRUB_SERIAL_COMMAND="serial --speed=115200" to your hardware.

Finally, GRUB_CMDLINE_LINUX="console=tty1 console=ttyS0,115200" is Linux kernel’s command-line parameter. Like GRUB_TERMINAL="console serial", this enables tty in/out on both VGA console (tty1) and serial console (ttyS0).

/ubuntu_console/2020-08-01-21-54-36.png
tty output on both tty1 and ttyS0

You can change the command-line parameter at boot time in GRUB menu.

/ubuntu_console/2020-08-01-21-54-53.png
Modify command-line parameter in GRUB menu

After editing /etc/default/grub, execute sudo update-grub. This will execute grub-mkconfig -o /boot/grub/grub.cfg internally and modifies /boot/grub/grub.cfg, which is actually loaded on the boot.

After reboot, we can see dmesg and login prompt on the serial console!

Appendix: Original, modified, and diff of /etc/default/grub

Original /etc/default/grub (may differ from yours depending on the environment, it’s QEMU’s one):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.
# For full documentation of the options in this file, see:
#   info -f grub -n 'Simple configuration'

GRUB_DEFAULT=0
GRUB_TIMEOUT_STYLE=hidden
GRUB_TIMEOUT=0
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT=""
GRUB_CMDLINE_LINUX=""

# Uncomment to enable BadRAM filtering, modify to suit your needs
# This works with Linux (no patch required) and with any kernel that obtains
# the memory map information from GRUB (GNU Mach, kernel of FreeBSD ...)
#GRUB_BADRAM="0x01234567,0xfefefefe,0x89abcdef,0xefefefef"

# Uncomment to disable graphical terminal (grub-pc only)
#GRUB_TERMINAL=console

# The resolution used on graphical terminal
# note that you can use only modes which your graphic card supports via VBE
# you can see them in real GRUB with the command `vbeinfo'
#GRUB_GFXMODE=640x480

# Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux
#GRUB_DISABLE_LINUX_UUID=true

# Uncomment to disable generation of recovery mode menu entries
#GRUB_DISABLE_RECOVERY="true"

# Uncomment to get a beep at grub start
#GRUB_INIT_TUNE="480 440 1"

Modified /etc/default/grub:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.
# For full documentation of the options in this file, see:
#   info -f grub -n 'Simple configuration'

GRUB_DEFAULT=0
GRUB_TIMEOUT=2
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT=""
GRUB_CMDLINE_LINUX="console=tty1 console=ttyS0,115200"

# Uncomment to enable BadRAM filtering, modify to suit your needs
# This works with Linux (no patch required) and with any kernel that obtains
# the memory map information from GRUB (GNU Mach, kernel of FreeBSD ...)
#GRUB_BADRAM="0x01234567,0xfefefefe,0x89abcdef,0xefefefef"

# Uncomment to disable graphical terminal (grub-pc only)
#GRUB_TERMINAL=console
#GRUB_TERMINAL="console serial"
GRUB_TERMINAL="console serial"
GRUB_SERIAL_COMMAND="serial --speed=115200"

# The resolution used on graphical terminal
# note that you can use only modes which your graphic card supports via VBE
# you can see them in real GRUB with the command `vbeinfo'
#GRUB_GFXMODE=640x480

# Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux
#GRUB_DISABLE_LINUX_UUID=true

# Uncomment to disable generation of recovery mode menu entries
#GRUB_DISABLE_RECOVERY="true"

# Uncomment to get a beep at grub start
#GRUB_INIT_TUNE="480 440 1"

diff:

1
2
3
sudo apt install icdiff
~/.local/bin/icdiff /etc/default/grub.orig   /etc/default/grub
~/.local/bin/icdiff /boot/grub/grub.cfg.orig /boot/grub/grub.cfg

/ubuntu_console/2020-08-01-21-55-23.png
diff /etc/default/grub

diff for /boot/grub/grub.cfg is large, check it yourself.