13
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

EC2インスタンスのbashプロンプトに便利情報を表示する

Last updated at Posted at 2016-04-28

EC2インスタンスにssh接続して作業する際、デフォルトだとこんな感じで、

[ec2-user@ip-10-10-11-123 ~]$ sl

どのインスタンスにログインしているか分かりづらいですよね。でもhostnameを変えるのは色々副作用もあるし、なんだか微妙です。

そこで!

set_prompt.sh
instance_id=$(curl -s 169.254.169.254/latest/meta-data/instance-id)
az=$(curl -s 169.254.169.254/latest/meta-data/placement/availability-zone)
region=${az%?}
tagged_name=$(aws ec2 describe-instances --region $region --instance-ids $instance_id --query 'Reservations[].Instances[].[Tags[?Key==`Name`].Value]' --output text)

if [ $? -eq 0 ]; then
  export PS1="[\u@\h (\[\e[1m\]$instance_id\[\e[m\], \[\e[1m\]$tagged_name\[\e[m\]) \W]\$ "
else
  export PS1="[\u@\h (\[\e[1m\]$instance_id\[\e[m\]) \W]\$ "
fi

こんなスクリプトを /etc/profile.d/ 以下に置くなり、 ~/.bashrc に追記するなりしてみましょう。

[ec2-user@ip-10-10-11-123 (i-1234567890abcdef1, oreno_instance) ~]$ sl

インスタンスのIDと、Name タグに設定している値が表示されました。これなら分かりやすいですね!

余談

sl はAmazon Linuxの標準リポジトリにもあります。必ずインストールしましょう。

[ec2-user@ip-10-10-11-123 (i-1234567890abcdef1, oreno_instance) ~]$ sudo yum install sl
Loaded plugins: priorities, update-motd, upgrade-helper
amzn-main/latest                                                                                                                         | 2.1 kB     00:00
amzn-updates/latest                                                                                                                      | 2.3 kB     00:00
amzn-updates/latest/updateinfo                                                                                                           | 307 kB     00:00
amzn-updates/latest/primary_db                                                                                                           | 375 kB     00:00
8 packages excluded due to repository priority protections
Resolving Dependencies
--> Running transaction check
---> Package sl.x86_64 0:5.02-1.6.amzn1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================================================================================================
 Package                         Arch                                Version                                       Repository                              Size
================================================================================================================================================================
Installing:
 sl                              x86_64                              5.02-1.6.amzn1                                amzn-main                               13 k

Transaction Summary
================================================================================================================================================================
Install  1 Package

Total download size: 13 k
Installed size: 15 k
Is this ok [y/d/N]:
13
15
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
13
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?