Bobcares

Send user-data output to console logs on an EC2 instance running RHEL 7/8

by | Sep 27, 2021

Want to send user-data output to console logs on an EC2 instance running RHEL 7/8? We can help you.

Here, at Bobcares, we assist our customers with several AWS queries as part of our AWS Support Services.

Today, let us see how we can log the user-data invocation and then ship it to the console logs.

 

Send user-data output to console logs on an EC2 instance running RHEL 7/8

We add code to the user-data bash script to troubleshoot issues on the EC2 instance bootstrap without having to access the instance through SSH.

This script will redirect all the output both to the /var/log/user-data.log and to /dev/console.

When we execute the code, we can see the user-data invocation logs in the console.

Send user-data output to console logs on an EC2 instance running RHEL 7/8

Moving ahead, let us see the steps our Support Techs employ to add the code for our customers.

1. Initially, we open the Amazon EC2 console.

2. Then, we launch a new instance and SSH into it.

3. After that, we edit the GRUB_CMDLINE_LINUX line in /etc/default/grub.

In addition, we change “console=ttyS0,115200n8 console=tty0” to “console=tty1 console=ttyS0“.

  • RHEL 7:
# cat /etc/default/grub
GRUB_TIMEOUT=1
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="console=tty1 console=ttyS0 net.ifnames=0 rd.blacklist=nouveau crashkernel=auto"
GRUB_DISABLE_RECOVERY="true"
  • RHEL 8:
# cat /etc/default/grub
GRUB_TIMEOUT=1
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="console=tty1 console=ttyS0 net.ifnames=0 rd.blacklist=nouveau nvme_core.io_timeout=4294967295 crashkernel=auto"
GRUB_DISABLE_RECOVERY="true"
GRUB_ENABLE_BLSCFG=true

4. Once done, we recreate the /boot/grub2/grub.cfg file:

  • RHEL 7:
# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-1062.1.2.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-1062.1.2.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-026767dbe06a4910a5ce3bd0def903c0
Found initrd image: /boot/initramfs-0-rescue-026767dbe06a4910a5ce3bd0def903c0.img
done
  • RHEL 8:
# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
done

5. Eventually, we stop the instance.

6. Now we need to create a new AMI from the modified instance.

7. We launch a new instance from the new AMI.

8. Then we go ahead and enter the following command to redirect the user-data output console:

#!/bin/bash -xe
exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
yum -y update
echo "Hello from user-data!"

As we can see, the following line redirects the user-data output:

exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1

Our sample console output will look like the following:

  • RHEL 7:
subscription-manager.x86_64 0:1.24.26-3.el7_8
subscription-manager-rhsm.x86_64 0:1.24.26-3.el7_8
subscription-manager-rhsm-certificates.x86_64 0:1.24.26-3.el7_8
sudo.x86_64 0:1.8.23-9.el7
systemd.x86_64 0:219-73.el7_8.8
systemd-libs.x86_64 0:219-73.el7_8.8
systemd-sysv.x86_64 0:219-73.el7_8.8
teamd.x86_64 0:1.29-1.el7
tuned.noarch 0:2.11.0-8.el7
tzdata.noarch 0:2020a-1.el7
util-linux.x86_64 0:2.23.2-63.el7
yum.noarch 0:3.4.3-167.el7
yum-utils.noarch 0:1.1.31-54.el7_8

Replaced:
iwl7265-firmware.noarch 0:22.0.7.0-72.el7

Complete!
+ echo 'Hello from user-data!'
Hello from user-data!
  • RHEL 8:
Installed:
grub2-tools-efi-1:2.02-82.el8_2.1.x86_64

kernel-4.18.0-193.13.2.el8_2.x86_64

kernel-core-4.18.0-193.13.2.el8_2.x86_64

kernel-modules-4.18.0-193.13.2.el8_2.x86_64

linux-firmware-20191202-97.gite8a0f4c9.el8.noarch

Complete!
+
echo 'Hello from user-data!'
Hello from user-data!

[Need further assistance? We’d be glad to help]

 

Conclusion

In short, in this article, we saw how our Support Techs add the code to the user-data bash script.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.