LoginSignup
6
5

More than 5 years have passed since last update.

[EC2] SSH 接続時のログインメッセージにインスタンスタイプとかリージョンとか表示する

Posted at

対象は Amazon Linux で動いてる EC2。
SSH接続して作業するときに、このインスタンスってどのインスタンスタイプで動いてたっけ?って知りたいときとかに便利。

こんな感じのシェルスクリプトを /etc/update-motd.d/40-instance-meta とかって名前で保存して実行権限与えておいてください。

/etc/update-motd.d/40-instance-meta
#!/bin/sh
instance_type=$(curl -s http://169.254.169.254/latest/meta-data/instance-type/)
az=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)
region=$(echo ${az} | sed -e 's/[a-z]$//g')

cat << EOF
 INSTANCE TYPE : ${instance_type}
 REGION : ${region} (${az})

EOF

そのあとで update-motd コマンドを root 権限で実行すると、次回 SSH ログイン時からログインメッセージ変わります。あら便利

Last login: Wed Mar 15 15:05:40 2017 from xxx.xxx.xxx.xxx
      ___         _            __
     / _ | __ _  (_)_ _  ___  / /____
    / __ |/  ' \/ /  ' \/ _ \/ __/ _ \
   /_/ |_/_/_/_/_/_/_/_/\___/\__/\___/

https://aws.amazon.com/amazon-linux-ami/2016.09-release-notes/

 Nginx 1.11.10, PHP 7.1.2, Percona MySQL 5.6.35, WP-CLI 1.1.0

 amimoto     https://www.amimoto-ami.com/
 digitalcube https://en.digitalcube.jp/

 INSTANCE TYPE : t2.small
 REGION : ap-northeast-1 (ap-northeast-1c)

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5