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
Do you need the method to work against an attacker who has root on the host, and is trying to spoof you into thinking that it's an EC2 instance for his own malicious purposes? If you do, then it will be much harder. –  Mike Scott Jun 22 at 19:01

6 Answers 6

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

Changed Hannes' answer to avoid error messages and include example usage in script:

if [ -f /sys/hypervisor/uuid ] && [ `head -c 3 /sys/hypervisor/uuid` == ec2 ]; then
    echo yes
    echo no

This doesn't work in Windows instances. Advantage over curl is that it's close to instantaneous on both EC2 and non-EC2.

share|improve this answer
test -f /sys/hypervisor/uuid -a `head -c 3 /sys/hypervisor/uuid` == ec2 && echo yes

but I don't know how portable this is across distributions.

share|improve this answer
Well, it's certainly not going to work on Windows EC2 instances. –  ceejayoz Jun 4 at 3:24
I prefer this method since it doesn't involve a network interaction which can hang for all kinds of reasons. Using timeouts for an HTTP exchange is not guaranteed to prevent hangs. I don't care about Windows instances. –  Hannes Jun 11 at 1:34
That is exactly what I needed! Way better than curl'ing something, thanks! –  qwertzguy Jun 22 at 18:38
Consider using the full UUID, just in case some other vendor's hypervisor UUID also starts with "ec2". The chance of that happening is 1 in 4096 which is not negligible. –  Hannes Jun 23 at 2:07
Actually, comparing the entire UUID does not work since I have seen multiple different hypervisor UUIDs in the wild. They all start with "ec2" though, so this answer works as is. –  Hannes Aug 19 at 20:16

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.