Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8482127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:44:41+00:00 2026-06-10T19:44:41+00:00

I am trying to set amazon EC2 hostname from the tag Name And found

  • 0

I am trying to set amazon EC2 hostname from the tag “Name”

And found the answer to extract tags from instance data.

ec2-describe-tags \
  --filter "resource-type=instance" \
  --filter "resource-id=$(ec2-metadata -i | cut -d ' ' -f2)" \
  --filter "key=Name" | cut -f5

the result is:

+------------+--------------+------+--------+
| resourceId | resourceType | key  | value  |
+------------+--------------+------+--------+
| i-1xxxxxxx | instance     | Name | dev200 |
+------------+--------------+------+--------+

I can see that I am almost there, but how do I get the value(dev200) from the result above? Then I can use it in:

echo $HOSTNAME > /etc/hostname

p.s. I have BASH on the instance, but I am completely lost in the bash document. can someone point me to the correct paragraph?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:44:43+00:00Added an answer on June 10, 2026 at 7:44 pm

    After some error and trial, got the script working:

    #!/bin/bash
    hostname=`ec2-describe-tags --filter "resource-type=instance" \
      --filter "resource-id=$(ec2-metadata -i | cut -d ' ' -f2)" \
      --filter "key=Name" | grep Name`
    
    IFS="|" read -ra NAME <<< "$hostname"
    hostname=${NAME[4]}
    echo $hostname
    

    Used IFS to get the string parsed into arrays, and luckily I know the 4th element is always the hostname.

    EDIT (20-DEC-2012): In the short time since this was posted, several of the relevant ec2 command line tools have been modified, and flags changed or deprecated (e.g., the -i flag from above no longer seems to work on the current version of ec2metadata). Bearing that in mind, here is the command line script I used to get the current machine’s “Name” tag (can’t speak to the rest of the script):

    ec2-describe-tags --filter "resource-type=instance" --filter "resource-id=$(ec2metadata --instance-id)" | awk '{print $5}'
    

    On Debian/Ubuntu, you need to apt-get install cloud-utils ec2-api-tools to get these working (the later is only on Ubuntu Multiverse).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was recently trying to set up an Amazon EC2 instance to run Rails
I'm trying to set up Hive on Amazon's EMR to pull data from a
I am trying to download a set of .jpg's from Amazon S3 and store
I am trying to install Glassfish on my new Amazon EC2 Linux instance. The
i am trying to run a mapreduce task on amazon ec2. i set all
Im trying to set the certificate friendly name during the certificate request/acceptance process. I
I'm trying to get set up on the Amazon Cloud to run some hadoop
I have setup a new EC2 instance on AWS and I'm trying to get
I'm trying out the free tier on Amazon EC2 and intend to host a
I am trying to set up my application server behind the Amazon Elastic Load

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.