Author Topic: [How-To] - Enable Serial Com Port in CentOS 7  (Read 14546 times)

Offline kek

  • Administrator
  • Newbie
  • *****
  • Posts: 5
  • Karma: +2/-0
  • me. admin?
    • View Profile
[How-To] - Enable Serial Com Port in CentOS 7
« on: November 29, 2017, 11:19:37 PM »
[How-To] - Enable Serial Com Port in CentOS 7

In this post I want to share how you enable the serial console to work at boot and have the server send the login screen both to the regular monitor and to the serial com connection in CentOS 7. If you need to know more about what it is or why you should use a serial connection for your servers see my other post: What is a Serial Console, and why would we use it?, but in short is goes something like this:

you connect to your headless server using SSH or WebGUI over IP \ DNS but you messed up some configurations and you are no longer able to access your server over the network, and now you have to find a keyboard and monitor to access it and restore it. and that in it self can be a hassle and if you had serial connection enabled on a rs-232 com port you would only need to connect a console cable to it and do the troubleshooting needed, not to say most of the networking gear like routers and switches you see in business and enterprise environment need to be configured over console connection before they are deployed and you can use SSH or WebGUI over IP \ DNS.

Before I begin the configurations I will make two assumptions:
1. You have a clean and fresh install of CentOS 7 using LVM partitioning.
2. That your server have a working RS-232 Console Port installed and are recognized by the kernel and the drivers are installed.

To make the serial console available at boot we need to adjust the boot loader of the system to send the output to both the console port and the monitor, and to do this you need to login to the system whit a user that has sudo or root access.[/size]

Hardware Information:
Now that you are logged in to your system you want to first check that your Serial Comport is installed, to do this type the command: sudo dmesg | grep tty

Output should look like this example:
Code: [Select]

   [kek@centos7 ~]# sudo dmesg | grep tty
    [    0.000000] console [tty1] enabled
    [    0.000000] console [ttyS0] enabled
    [    1.891572] 00:05: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A

In this example I have one input \ output (I\O) console hardware port in the server and that is ttyS0 whit the full path of /dev/ttyS0, note this down as we need it later in the configuration

Now we need to check what LVM labels where given to the system if you used the easy installer option, in most cases it uses the hostname as labels, but since it can break the system if we do not get this part correct we better check as we do not want to do any unnessecary work or troubleshooting, to check LVM labels of your partitions run the command: sudo lvscan

Output should look similar to this:
Code: [Select]

   [kek@centos7 ~]# sudo lvscan
    ACTIVE      '/dev/cl_centos7/swap' [  2.00 GiB] inherit
    ACTIVE      '/dev/cl_centos7/root'  [<17.00 GiB] inherit

What we need to note from this command is the cl_centos7/root and cl_centos7/swap labeles as we need this later to get the device mapper string to point to the correct hard drive partitions for booting.

System Configuration:
Now that we have all the needed hardware information we are ready to configure the serial console and the boot loader, to do this you need to edit the following configuration file: /etc/sysconfig/grub to do this use your favourite text editor like vim or nano (not installed by default), command is: sudo vi /etc/sysconfig/grub

The file should look similar to this before editing:
Code: [Select]

GRUB_CMDLINE_LINUX=" crashkernel=auto rhgb quiet"

You would need to make some changes to this file, as you can see it contains no information about the serial connection or the terminal settings to use, in my setup I use the following configuration:

Code: [Select]

GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_TERMINAL="console serial"
GRUB_SERIAL_COMMAND="serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1"
GRUB_CMDLINE_LINUX="crashkernel=auto rhgb quiet"
GRUB_CMDLINE_LINUX_DEFAULT="console=tty1 console=ttyS0,115200"

The settings in here should be self explainatory and should be generic egnouh to cover 90% of all serial adapters, but the general descripton of them are:
GRUB_TERMINAL is set to both Console and Serial enabling it for both on screen monitor and serial output.
GRUB_SERIAL_COMMAND sets the value of what speed it should run and how it should communicate whit the remote device these settings needs to match at both sides to make a connection.
GRUB_CMDLINE_LINUX specifies where the swap and root partition is stored for the LVM so the system can boot, this is why we looked up the disk information.
GRUB_CMDLINE_LINUX_DEFAULT tells the boot loader to where it should send the information where tty1 is the monitor and ttyS0 is the Serial port.

At this point you are nearly done, just a few commands left to run as the majority of the configuration is the get the grub config correct, now we just need to enable our Serial connection using the command:
 stty -F /dev/ttyS0 ispeed 115200

You may get an error saying it could not run all of the settings, but do not worry about that for now, next up would be to generate a new bootloader file for grub whit the settings we just saved in
/etc/sysconfig/grub we do that by running the following command: grub2-mkconfig -o /boot/grub2/grub.cfg

Now you are ready to connect the serial console cable and connect from your workstation or laptop using something like screen or putty depending on OS you are using, the console will at this point connect to a black screen as it has not active console running at the moment, and if you are at a blank window whit no errors then you can run the command: sudo /sbin/reboot on your CentOS 7 server and you should see the grub and boot process both on the monitor and the serial terminal window.