Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I would like to run some scripts on hosts which are EC2 instances but I don't know how to be sure that the host is really an EC2 instance.

I have made some tests, but this is not sufficient:

  • Test that binary ec2_userdata is available (but this will not always be true)
  • Test availability of "" (but will this be always true ? and what is this "magical IP" ?)
share|improve this question
It's an APIPA address actually, which is quite odd to use as a reference for a critical service like meta data retrieval. –  Matthieu Cerda Jan 4 '13 at 9:52
The IP ranges of EC2s are public (though varying from time to time). If you keep up with a current list you can check the instances IP against that ranges. –  Karma Fusebox Jan 4 '13 at 10:30
Don't rely on if you want EC2 and only EC2 - EC2-alike systems like Eucalyptus also support it. engage.eucalyptus.com/customer/portal/articles/… –  ceejayoz Jul 2 '13 at 14:21

4 Answers 4

up vote 3 down vote accepted

Well actually, there is a very simple way to detect if the host is an EC2 instance: check the reverse lookup of your public IP. The EC2 reverses are quite hard to miss.

Also, if you did not modify it, the hostname should be your reverse, making it further easy to spot it.

You might also use the "magical IP" you talked about, as it is indeed the standard way to get EC2 Instance tags, however, if you are not on a EC2 network, you will have to wait for a timeout, which is generally not desirable...

If these methods are not enough, just do a whois of your IP and check if you are within and Amazon EC2 IP block.

EDIT: You may use this small shell bit:

LOCAL_HOSTNAME=$(hostname -d)
if [[ ${LOCAL_HOSTNAME} =~ .*\.amazonaws\.com ]]
        echo "This is an EC2 instance"
        echo "This is not an EC2 instance, or a reverse-customized one"

Careful though, [[ is a bashism. You may also use a Python or Perl uniline, YMMV.

share|improve this answer
this doesn't work in a VPC or an environment where you have changed the hostname; eg. if your machines are in domain.local –  Preflightsiren Jan 28 '13 at 9:28
the hostname bit is just bound to fail. –  Dan Pritts Feb 13 '14 at 19:28

Look for the metadata by the EC2 internal domain name instead of IP, which will return a fast DNS failure if you're not on EC2, and avoids IP conflicts or routing issues:

curl -s http://instance-data.ec2.internal && echo "EC2 instance!" || echo "Non EC2 instance!"

On some distros, very basic systems, or very early at installion stages curl is not available. Using wget instead:

wget -q http://instance-data.ec2.internal && echo "EC2 instance!" || echo "Non EC2 instance!"
share|improve this answer
Unfortunately, appears to fail in VPC! –  Yuki Feb 6 at 1:35

Hostnames are likely to change, run a whois against your public IP:

if [[ ! -z $(whois $(curl -s shtuff.it/myip/short) | grep -i amazon) ]]; then 
  echo "I'm Amazon"
  echo "I'm not Amazon"

or hit the AWS meta-data url

if [[ ! -z $(curl -s ]]; then 
  echo "I'm Amazon"
  echo "I'm not Amazon"
share|improve this answer
Add a --connect-timeout 1 to the second curl statement to fail quickly if you're not running on EC2. –  Jonathan Oliver Sep 25 '13 at 12:27
FWIW, using the metadata URL can indicate it's running as a cloud instance, but cannot conclusively determine if it is specifically EC2. OpenStack and Eucalyptus also use the same metadata URI. I know this is picking nits, but for my work, which cloud provider matters. –  EmmEff Feb 20 '14 at 15:53

Perhaps you can use "facter":

"Facter is a cross-platform library for retrieving simple operating system facts, like operating system, linux distribution, or MAC address."


For example, if we take a look to the ec2 fact (facter-1.6.12/lib/facter/ec2.rb):

require 'facter/util/ec2'
require 'open-uri'

def metadata(id = "")
    split("\n").each do |o|
    key = "#{id}#{o.gsub(/\=.*$/, '/')}"
    if key[-1..-1] != '/'
      value = open("{key}").read.
      symbol = "ec2_#{key.gsub(/\-|\//, '_')}".to_sym
      Facter.add(symbol) { setcode { value.join(',') } }

def userdata()
    value = open("").read.split
    Facter.add(:ec2_userdata) { setcode { value } }
  rescue OpenURI::HTTPError

if (Facter::Util::EC2.has_euca_mac? || Facter::Util::EC2.has_openstack_mac? ||
    Facter::Util::EC2.has_ec2_arp?) && Facter::Util::EC2.can_connect?

  Facter.debug "Not an EC2 host"
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.