How-To set up a serial console on Debian

2 minute read

This tutorial will go over the steps to go through in order to set up a serial console on Debian Linux.

Debian uses sysvinit to handle the booting process, amongst the different task, and as such, there is a few differences between most of the tutorial that you might find on the internet regarding how to set up a serial console.

A Serial Console becomes handy when running a headless server (i.e no keyboard and screen) or if you cannot connect a a server because of a network issue.

In this tutorial, we will set up a serial console on the server, the machine we want an access to. Setting up a serial console client will be covered in another article.

1. Checking the serial devices

In order to find which devices are available on a box, you can run:

$ dmesg | grep tty
serial8250: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
serial8250: ttyS1 at I/O 0x2f8 (irq = 3) is a 16550A
00:09: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
00:0a: ttyS1 at I/O 0x2f8 (irq = 3) is a 16550A

From this output, we can guess that there is 2 serial interface2 (/dev/ttyS0 and /dev/ttyS1). This tutorial considers that you are using device 1 (ttyS0), if you use the second device, you will need to change ttyS0 by ttyS1.

2. Setting up the serial console on the server

On the server, we are going to set up:

  • A serial console on ttyS0
  • Have kernel booting messages output to the serial console
  • Make Grub outputs to the serial console

2.1. The serial console

To set up a serial console, We need to edit the file called /etc/inittab in order to spawn a getty on the serial device. getty will take care of prompting the user for a username and password.

Go and edit /etc/inittab and add:

s0:2345:respawn:/sbin/getty -L 115200 ttyS0 vt102

just below:

1:2345:respawn:/sbin/getty 38400 tty1
2:23:respawn:/sbin/getty 38400 tty2
3:23:respawn:/sbin/getty 38400 tty3
4:23:respawn:/sbin/getty 38400 tty4
5:23:respawn:/sbin/getty 38400 tty5
6:23:respawn:/sbin/getty 38400 tty6

Now, run:

# init q

In order to have sysvinit re-read its configuration and spawn a getty on /dev/ttyS0 .

To authorize root to log in through the serial console, you need to edit /etc/securetty and make sure you have:

ttyS0

2.2. Having grub outputting to ttyS0

grub can be configured to output on the serial console.Edit grub and add:

serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1
terminal --timeout=10 serial console

In the section before the different kernels. This will take care of having grub being displayed one the serial console so you can actually modify grub through the serial console or boot using another kernel.

If using ttyS1, then change it to –unit=1

Also, in order to get the booting messages outputted to the serial console, you can append to your kernel line the following:

console=ttyS0,115200n8 console=tty0

So, finally, the kernel line will look like:

kernel          /boot/vmlinuz-2.6.18-6-686 root=/dev/sda1 ro console=ttyS0,115200n8 console=tty0

That’s it, upon next reboot, you will be able to connect directly to your box using a serial console!.