AWS EC2 开启串口

开启条件

aws 的 ec2 并不是所有的实例都能开启串口控制台,进入 AWS 控制台选择实例并点击连接,在 EC2 标签下检查是否可以进行连接。不行的情况下会有如下提示:EC2 串行控制台不支持此实例类型。要使用 EC2 Serial Console 连接到此实例的串行端口,实例必须使用在 AWS Nitro System 上构建的实例类型。您可以将实例类型更改为支持的虚拟化实例类型或裸机实例类型。

根据提示可知只有 AWS Nitro System 上的实例才支持串口控制台,因此如果想开启串口控制台必须将实例关停,并更改实例规格,让实例被调度到 AWS Nitro System 上,这样才能完成串口控制台的开启。

开启流程

Ubuntu 实例上开启串口控制台流程如下:

一、配置修改

To configure GRUB on an Ubuntu instance

  • 1、Connect to your instance.

  • 2、Add or change the following options in /etc/default/grub.d/50-cloudimg-settings.cfg:

    • Set GRUB_TIMEOUT=1.
    • Add GRUB_TIMEOUT_STYLE=menu.
    • Add GRUB_TERMINAL=“console serial”.
    • Remove GRUB_HIDDEN_TIMEOUT.
    • Add GRUB_SERIAL_COMMAND=“serial –speed=115200”.

The following is an example of /etc/default/grub.d/50-cloudimg-settings.cfg. You might need to change the configuration based on your system setup.

# Cloud Image specific Grub settings for Generic Cloud Images
# CLOUD_IMG: This file was created/modified by the Cloud Image build process

# Set the recordfail timeout
GRUB_RECORDFAIL_TIMEOUT=0

# Do not wait on grub prompt
GRUB_TIMEOUT=1
GRUB_TIMEOUT_STYLE=menu

# Set the default commandline
GRUB_CMDLINE_LINUX_DEFAULT="console=tty1 console=ttyS0 nvme_core.io_timeout=4294967295"

# Set the grub console type
GRUB_TERMINAL="console serial"
GRUB_SERIAL_COMMAND="serial --speed 115200"
  • 3、Apply the updated configuration by running the following command.
[ec2-user ~]$ sudo update-grub

二、应急情况

如果已经无法连上实例,则可以通过修改用户数据的方式在启动实例启动时执行操作,具体流程如下:1、选择实例 2、选择操作–实例设置–编辑用户数据增加用户执行脚本。数据内容可以参考如何执行用户脚本,也可参考如下配置,在 #!/bin/bash 下执行需要执行的指令。

Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0

--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"

#cloud-config
cloud_final_modules:
- [scripts-user, always]

--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

#!/bin/bash
systemctl disable --now wg-quick@wg0
--//--

此配置在实例执行 cloud-init 流程完成之后都要执行 systemctl disable --now wg-quick@wg0 操作,在应急时能起到关键效果。

Infee Fang
Infee Fang
互联网二手搬砖工