Translated message

A translation of this page exists in English.

如何在 Red Hat Enterprise Linux 中设置串行终端和/或控制台?

已更新 -

简介

有时,有一个用于调试目的的串行控制台和一个用于无头操作的串行终端会很有帮助。串行控制台将向串行端口发送所有控制台输出。如果正确配置了,串行终端也可让您通过将串行端口作为远程终端来登录到系统。您可以两个都设置或只设置一个。本文将涵盖 RHEL 中串行终端的配置。

内容

串行控制台内核选项配置

首先,要让内核将所有控制台消息输出到您需要的串行端口,来在引导时将 console=ttyS0(请注意,结尾字符是零,而不是字母 O)参数传递传输到内核。这通常是通过引导加载程序来完成的;我们将在我们的示例中使用 GRUB。以下示例将配置系统,来以 115200 波特率将控制台输出发送到串行端口 ttyS0,并将输出发送到常规控制台或"屏幕"tty0

  • 通过在内核行中添加以下内核选项来编辑文件 /boot/grub/grub.conf

    console=ttyS0,115200 console=tty0
    
  • 例如,在 grub.conf 中:

    [root@localhost ~]# cat /boot/grub/grub.conf
    # grub.conf generated by anaconda
    #
    # Note that you do not have to rerun grub after making changes to this file
    # NOTICE:  You have a /boot partition.  This means that
    #          all kernel and initrd paths are relative to /boot/, eg.
    #          root (hd0,0)
    #          kernel /vmlinuz-version ro root=/dev/hda2
    #          initrd /initrd-version.img
    #boot=/dev/hda
    default=0
    timeout=10
    splashimage=(hd0,0)/grub/splash.xpm.gz
    title Red Hat Enterprise Linux AS (2.4.21-27.0.2.ELsmp)
            root (hd0,0)
            kernel /vmlinuz-2.4.21-27.0.2.ELsmp ro root=LABEL=/ console=ttyS0,115200 console=tty0
            initrd /initrd-2.4.21-27.0.2.ELsmp.img
    
  • 备注:关于上面记录的波特率 115200,给定配置的波特率取决于硬件设置。每台服务器都有自己的串行控制台设置,通常在系统 BIOS 中注明。在应用任何此类设置前,建议您还要检查服务器手册。错误配置的波特率设置可能导致在登录时显示垃圾字符。

  • 备注:系统输出的主要控制台将是内核参数中列出的 最后一个 控制台。在上例中,VGA 控制台 tty0 是主控制台,串行控制台是辅助显示。这意味着来自 init 脚本的消息将不会进到串行控制台,因为它是辅助控制台,但引导消息和严重警告将进到串行控制台。如果也需要在串行控制台上看到 init 脚本信息,应该通过交换控制台参数的顺序使其成为主控制台:

    console=tty0 console=ttyS0,115200
    
  • grub.conf 中任何更改的设置都将在下次系统重启后生效。

  • 另外,还必须将串行端口添加到 Red Hat Enterprise Linux 4、5 和 6 中的 /etc/securetty 文件中,以允许通过串行端口进行 root 登录。ttyS0 应添加到列表底部的新行上:

    tty9
    tty10
    tty11
    ttyS0
    
  • Red Hat Enterprise Linux 7 默认在 /etc/securetty 中有一个 ttyS0 条目。

RHEL 4 和 RHEL 5 中的串行终端配置

此配置会将串行端口设置为远程终端,从而允许通过串行控制台登录。为此,我们需要为串行端口生成一个 agetty 进程。

  • 这可以通过在 /etc/inittab 文件中添加以下行来实现:

    co:2345:respawn:/sbin/agetty ttyS0 115200 vt100
    
  • 这个选项应添加到名为 # Run gettys in standard runlevels 的部分中:

    # Run gettys in standard runlevels
    1:2345:respawn:/sbin/mingetty tty1
    2:2345:respawn:/sbin/mingetty tty2
    3:2345:respawn:/sbin/mingetty tty3
    4:2345:respawn:/sbin/mingetty tty4
    5:2345:respawn:/sbin/mingetty tty5
    6:2345:respawn:/sbin/mingetty tty6
    co:2345:respawn:/sbin/agetty ttyS0 115200 vt100
    
  • 完成此编辑后,运行以下命令:

    # init q
    

RHEL 6 中的串行终端配置

对于 RHEL6,如果主控制台是串口(回想一下,最后一个 console= 参数是主端口),则 upstart(init 进程)会自动启动一个 agetty 进程。

  • 要在不是主控制台的串行端口上启动 agetty,请创建一个具有以下内容的新 /etc/init/serial-ttyS0.conf 文件:

    start on stopped rc RUNLEVEL=[2345]
    stop on runlevel [S016]
    
    respawn
    exec /sbin/agetty /dev/ttyS0 115200 vt100-nav
    
  • 然后,通过重启或通过运行来启动新的 agetty

    [root@localhost ~]# initctl start serial-ttyS0
    
  • 注意 :如果键盘在新 agetty 上无法正常工作,您可能需要从内核参数中删除 console=ttyS0

    • 一些背景信息

      • 在系统引导时,它会启动 udev 来处理硬件设备。/lib/udev/rules.d/10-console.rules 文件告知 udev 检查设备的数量,包括 /dev/console,并针对每个设备运行 /lib/udev/console_check 帮助程序:

        # Console initialization - keyboard, font, etc.
        KERNEL=="tty0",         RUN+="/lib/udev/console_init %k"
        
        # Check and set up serial and serial-like consoles if necessary
        KERNEL=="console",      RUN+="/lib/udev/console_check %k"
        KERNEL=="ttySG*",       RUN+="/lib/udev/console_check %k"
        KERNEL=="xvc*",         RUN+="/lib/udev/console_check %k"
        KERNEL=="hvsi*",        RUN+="/lib/udev/console_check %k"
        KERNEL=="hvc*",         RUN+="/lib/udev/console_check %k"
        
      • 如果 console_check 确定 /dev/console 是一个串行控制台(通过对其运行一些 ioctl()系统调用),那么它运行这个命令:

        /sbin/initctl emit --no-wait fedora.serial-console-available DEV=ttyS0 SPEED=115200
        
      • 这个命令向 init 守护进程发送 fedora.serial-console-available 事件。init 守护进程是通过 /etc/init/serial.conf 为此事件进行配置的:

        start on fedora.serial-console-available DEV=* and stopped rc RUNLEVEL=[2345]
        stop on runlevel [S016]
        
        instance $DEV
        respawn
        pre-start exec /sbin/securetty $DEV
        exec /sbin/agetty /dev/$DEV $SPEED vt100-nav
        
      • init 守护进程通过从 serial.conf 中的 exec 行运行命令来启动 agetty :

        /sbin/agetty /dev/ttyS0 115200 vt100-nav
        
      • 这在串行控制台上显示登录提示。

  • 要检查串行控制台的状态,请使用 initctl 命令查询 init 守护进程的作业:

    [root@localhost ~]# initctl list | grep serial
    serial (ttyS0) start/running, process 1254
    
  • 或者:

    [root@localhost ~]# initctl status serial DEV=ttyS0
    serial (ttyS0) start/running, process 1254
    
  • 注意上面的 DEV=ttyS0 参数。如果没有此参数,initctl 会返回一个不太清楚的错误消息:

    [root@localhost ~]# initctl status serial
    initctl: Unknown parameter: DEV
    
  • 由于 /etc/init/serial.conf 中的作业定义,DEV=ttyS0 参数是必需的,如上所示。

RHEL 7 中的串行终端配置

  • /etc/default/grub 中添加或更新(如果已存在)以下行:

    GRUB_CMDLINE_LINUX_DEFAULT="console=tty0 console=ttyS0,115200n8"
    GRUB_TERMINAL=serial
    GRUB_SERIAL_COMMAND="serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1"
    
    • GRUB_CMDLINE_LINUX_DEFAULT 仅将此配置应用到默认菜单条目,使用 GRUB_CMDLINE_LINUX 将它应用到所有菜单条目。
    • 注意 :上面的每行类型应只在 /etc/default/grub 文件中出现一次。 如果行已存在,则只修改它,而不是添加同一行的第二个副本。 也就是说,文件中应该只存在一行 GRUB_CMDLINE_LINUX_DEFAULT ,等等。
  • 运行 grub2-mkconfig -o 命令 重建 /boot/grub2/grub.cfg 文件,如下所示:

    • 在基于 BIOS 的机器上:~]# grub2-mkconfig -o /boot/grub2/grub.cfg
    • 在基于 UEFI 的机器上:~]# grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg
  • RHEL 7 系统管理员指南 26.9 中提供了更多信息。通过串行控制台的 GRUB 2

Red Hat Enterprise Linux 8

  • GRUB 引导参数通过 grub2-editenv 命令进行编辑。首先,检索当前参数:

    # grub2-editenv - list | grep kernelopts
    kernelopts=root=/dev/mapper/rhel_example-root ro crashkernel=auto resume=/dev/mapper/rhel_example-swap
    rd.lvm.lv=rhel_example/root rd.lvm.lv=rhel_example/swap 
    
  • 从这里,复制整个行并附加串行控制台参数,并将其传递给上述命令;

    grub2-editenv - set "kernelopts=root=/dev/mapper/rhel_example-root ro crashkernel=auto resume=/dev/mapper/rhel_example-swap rd.lvm.lv=rhel_example/root rd.lvm.lv=rhel_example/swap console=tty0 console=ttyS0,115200"
    
  • 现在应设置控制台参数。需要重新启动才能使参数生效。可以列出参数来确认参数是否已设置:

    # grub2-editenv - list | grep kernelopts
    kernelopts=root=/dev/mapper/rhel_example-root ro crashkernel=auto resume=/dev/mapper/rhel_example-swap
    rd.lvm.lv=rhel_example/root rd.lvm.lv=rhel_example/swap console=tty0 console=ttyS0,115200
    

Red Hat Enterprise Linux 9

  • 使用 grubby 命令修改内核参数。

  • 检索当前参数:

       # grubby --info=ALL|grep -i args
    
      # grubby --info=ALL|grep -i args
       args="ro crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root      rd.lvm.lv=rhel/swap 
    
  • 更新参数 "console=tty0 console=ttyS0,115200"

      # grubby --update-kernel=ALL --args="console=tty0 console=ttyS0,115200"
    
  • 现在应设置控制台参数。需要重新启动才能使参数生效。可以列出参数来确认参数是否已设置:

     # grubby --info=ALL|grep -i args
      args="ro crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap console=tty0 console=ttyS0,115200"  
     # reboot
     # cat /proc/cmdline
    

从串行控制台控制 GRUB

也可以告知 grub 使用串行控制台而不是 VGA 控制台。这允许中断引导过程并选择不同的内核或添加内核参数,例如,要引导至单用户模式。

  • 要将 GRUB 配置为使用串行控制台,注释掉启动镜像,并在 grub.conf 中添加 serialterminal 选项:

    [root@localhost ~]# cat /boot/grub/grub.conf
    # grub.conf generated by anaconda
    #
    # Note that you do not have to rerun grub after making changes to this file
    # NOTICE:  You have a /boot partition.  This means that
    #          all kernel and initrd paths are relative to /boot/, eg.
    #          root (hd0,0)
    #          kernel /vmlinuz-version ro root=/dev/hda2
    #          initrd /initrd-version.img
    #boot=/dev/hda
    default=0
    timeout=10
    #splashimage=(hd0,0)/grub/splash.xpm.gz
    serial --unit=0 --speed=115200
    terminal --timeout=5 serial console
    title Red Hat Enterprise Linux AS (2.4.21-27.0.2.ELsmp)
            root (hd0,0)
            kernel /vmlinuz-2.4.21-27.0.2.ELsmp ro root=LABEL=/ console=ttyS0,115200 console=tty0
            initrd /initrd-2.4.21-27.0.2.ELsmp.img
    
  • grub.conf 中任何更改的设置都将在下次系统重启后生效。

Comments