centos7.7/CentOS7打开console

概述

我的工控机有4个网口、一个console口,安装了Centos7.7的系统,默认情况下console口是关闭状态的,应用程序接收、输出数据。这里介绍如何打开console口,通过console口可以登陆系统shell。

方法

直接给出方法,主要要修改两个地方:

修改 grub 文件,增加的是最后三行


[root@localhost ~]# vi /etc/default/grub 

GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet"
GRUB_DISABLE_RECOVERY="true"
GRUB_TERMINAL="console serial"
GRUB_SERIAL_COMMAND="serial --speed=9600 --unit=0 --word=8 --parity=no --stop=1"
GRUB_CMDLINE_LINUX_DEFAULT="console=tty1 console=ttyS0,9600"

该文件修改后,需要通过下面的命令使之生效:

grub2-mkconfig -o /boot/grub2/grub.cfg

关闭一个服务

systemctl mask plymouth-start.service

需要注意的是,这个服务通过下面的命令好像无法关闭

systemctl disable plymouth-start.service 

这个服务不关闭的话,会出现一个问题:用户名输入后,在输入密码的第一个字符的时候,输入内容就换行了,即密码无法输入。

简单介绍

grub文件修改后,相当于启用了console(并且指定ttyS0以及波特率等),系统会在启动阶段通过console口输出启动信息;系统启动后,init进程会启动agetty进程给出登陆提示:

root      2965     1  0 14:25 ttyS0    00:00:00 /sbin/agetty --keep-baud 115200,38400,9600 ttyS0 vt220

提示如下:

CentOS Linux 7 (Core)
Kernel 3.10.0-1127.13.1.el7.x86_64 on an x86_64

localhost login: 

当用户在console输入用户名、并敲击回车后:

CentOS Linux 7 (Core)
Kernel 3.10.0-1127.13.1.el7.x86_64 on an x86_64

localhost login: root
Password:

agetty进程唤起如下进程,接收用户名、密码的输入:

root      2965     1  0 14:25 ttyS0    00:00:00 /bin/login --

当用户在console继续输入密码后,console口对应的ttyS0就被bash接管了,即进入了shell环境:

root      2988  2965  2 14:27 ttyS0    00:00:00 -bash

用户侧就进入了shell环境:

CentOS Linux 7 (Core)
Kernel 3.10.0-1127.13.1.el7.x86_64 on an x86_64

localhost login: root
Password: 
Last login: Thu Jun 24 14:24:22 from 169.254.100.101
[root@localhost ~]# 
[root@localhost ~]# 

更进一步介绍

getty的启动

[root@localhost ~]# cat /lib/systemd/system/serial-getty@.service
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Serial Getty on %I
Documentation=man:agetty(8) man:systemd-getty-generator(8)
Documentation=http://0pointer.de/blog/projects/serial-console.html
BindsTo=dev-%i.device
After=dev-%i.device systemd-user-sessions.service plymouth-quit-wait.service getty-pre.target
After=rc-local.service

# If additional gettys are spawned during boot then we should make
# sure that this is synchronized before getty.target, even though
# getty.target didn't actually pull it in.
Before=getty.target
IgnoreOnIsolate=yes

[Service]
ExecStart=-/sbin/agetty --keep-baud 115200,38400,9600 %I $TERM
Type=idle
Restart=always
UtmpIdentifier=%I
TTYPath=/dev/%I
TTYReset=yes
TTYVHangup=yes
KillMode=process
IgnoreSIGPIPE=no
SendSIGHUP=yes

[Install]
WantedBy=getty.target
[root@localhost ~]# 

root进入bash shell

[root@localhost ~]# cat /etc/passwd | grep root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]#