9
8

More than 5 years have passed since last update.

AWS:EC2のプロンプト部分($PS1)がわかりにくいのでNAME Tagに変える

Last updated at Posted at 2015-06-12

わかりにくいんじゃー
ってことで、NAME Tagを使うように修正です。
※サーバにIAMロール付けておいて下さい。describe-instancesが打てれば良いです。

/etc/profile.d/term.sh
export AWS_DEFAULT_REGION=ap-northeast-1

INSID=`curl http://169.254.169.254/latest/meta-data/instance-id 2>/dev/null`
HNAME=`aws ec2 describe-instances --output json | jq -r '.Reservations[].Instances[] |
  select(.InstanceId == "'${INSID}'") |
  [
    ( .Tags[]| select(.Key == "Name") | .Value )
  ] | @csv' | cut -d \" -f 2`

export PS1="[\u@\${HNAME} \W]\\$ "

上記をprofileで読みこめば、以下のようになります。

[ec2-user@ip-192-168-X-XXX ~]$

[ec2-user@myserverName ~]$

といった具合です。

9
8
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
9
8