- Newest
- Most votes
- Most comments
- When an EC2 instance is started, what actually provides the IP addresses that are assigned to it?
According to the document, a public IP address is assigned to your instance from Amazon's pool of public IPv4 addresses.
A public IP address is assigned to your instance from Amazon's pool of public IPv4 addresses, and is not associated with your AWS account. When a public IP address is disassociated from your instance, it is released back into the public IPv4 address pool, and you cannot reuse it.
- How can I see the public IP address assigned to an EC2 instance, from within (i.e. after connecting) the EC2 instance?
Instance metadata can be used to retrieve public IP addresses.
curl http://169.254.169.254/latest/meta-data/public-ipv4
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html
I got the private ip address with the help of this post in my Ubuntu instance.
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9001 qdisc mq state UP group default qlen 1000
link/ether 02:93:07:be:da:d9 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.76/24 brd 10.0.0.255 scope global dynamic eth0
valid_lft 2915sec preferred_lft 2915sec
inet6 fe80::93:7ff:febe:dad9/64 scope link
valid_lft forever preferred_lft forever
It is same with the ip address in the following command.
TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` && curl -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/local-ipv4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 56 100 56 0 0 28000 0 --:--:-- --:--:-- --:--:-- 28000
* Trying 169.254.169.254:80...
* TCP_NODELAY set
* Connected to 169.254.169.254 (169.254.169.254) port 80 (#0)
> GET /latest/meta-data/local-ipv4 HTTP/1.1
> Host: 169.254.169.254
> User-Agent: curl/7.68.0
> Accept: */*
> X-aws-ec2-metadata-token: AQAAAEKVNCUakqrrHX0SIzCtzRcmpmaNNl2ypoguN1KR6NpvRlCnoQ==
>
* Mark bundle as not supporting multiuse
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Accept-Ranges: bytes
< Content-Length: 9
< Content-Type: text/plain
< Date: Tue, 29 Nov 2022 11:37:08 GMT
< Last-Modified: Wed, 23 Nov 2022 18:36:36 GMT
< X-Aws-Ec2-Metadata-Token-Ttl-Seconds: 21600
< Connection: close
< Server: EC2ws
<
* Closing connection 0
10.0.0.76
But I can not get the public ip address with the curl command.
curl http://169.254.169.254/latest/meta-data/public-ipv4
Looks like there is no public-ipv4 under meta-data directory.
$ TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` && curl -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 56 100 56 0 0 28000 0 --:--:-- --:--:-- --:--:-- 56000
* Trying 169.254.169.254:80...
* TCP_NODELAY set
* Connected to 169.254.169.254 (169.254.169.254) port 80 (#0)
> GET /latest/meta-data HTTP/1.1
> Host: 169.254.169.254
> User-Agent: curl/7.68.0
> Accept: */*
> X-aws-ec2-metadata-token: AQAAAEKVNCUB3KPoodSB0SFUkkn1aSah2MRUxAVQQx_3SeVBllVoFA==
>
* Mark bundle as not supporting multiuse
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Accept-Ranges: bytes
< Content-Length: 324
< Content-Type: text/plain
< Date: Tue, 29 Nov 2022 11:29:43 GMT
< Last-Modified: Wed, 23 Nov 2022 18:36:36 GMT
< X-Aws-Ec2-Metadata-Token-Ttl-Seconds: 21600
< Connection: close
< Server: EC2ws
<
ami-id
ami-launch-index
ami-manifest-path
auth-identity-credentials/
block-device-mapping/
events/
hostname
iam/
identity-credentials/
instance-action
instance-id
instance-life-cycle
instance-type
local-hostname
local-ipv4
mac
metrics/
network/
placement/
profile
public-keys/
reservation-id
security-groups
services/
Relevant content
- asked a year ago
- AWS OFFICIALUpdated 3 months ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated 3 years ago
Thanks takakuni for your response to my query. I tried what you provided, but that didn't work for me - and I emphasize "for me"!!! The great news, is that using the URL that you did provide, I went off in a different direction, and was able to locate something that did absolutely work, so you did at least get me started. Here's what I used on my RHEL EC2 instances to retrieve the public IP address for the instance:
dig +short myip.opendns.com @resolver1.opendns.com or curl http://checkip.amazonaws.com
Both of those commands absolutely provided the information that I was seeking.
Again, I'm not saying that what you provided doesn't work, it just didn't work for me. Thanks again, for your efforts!!!
Thanks for the reply. I'm glad you solved the problem, I had missed the fact that it was running on RHEL. Next time I answer a similar case, I will take care of the OS as well. Thank you very much.