Using Amazon CLI, is there a way to get the public ip address of the current EC2? I'm just looking for the single string value, so not the json response describe-addresses returns.
Asked
Active
Viewed 1e+01k times
6 Answers
168
The AWS Command-Line Interface (CLI) can be used to return information on any/all Amazon EC2 instances, eg:
$ aws ec2 describe-instances --instance-ids i-0c9c9b44b --query 'Reservations[*].Instances[*].PublicIpAddress' --output text
54.232.200.77
If you are seeking information about the EC2 instance from which you are executing the command, then the current IP address can be obtained via the instance metadata service:
$ curl http://169.254.169.254/latest/meta-data/
ami-id
ami-launch-index
ami-manifest-path
block-device-mapping/
hostname
iam/
instance-action
instance-id
instance-type
local-hostname
local-ipv4
mac
metrics/
network/
placement/
profile
public-hostname
public-ipv4
public-keys/
reservation-id
security-groups
services/
So, the private IP address is available via:
$ curl http://169.254.169.254/latest/meta-data/local-ipv4
172.31.10.221
The public IP address is available via:
$ curl http://169.254.169.254/latest/meta-data/public-ipv4
54.232.200.77

John Rotenstein
- 241,921
- 22
- 380
- 470
-
1I have added two public ip address and I have used curl http://169.254.169.254/latest/meta-data/public-ipv4 command but it showing the IP address that I have added recently. Is there a way to list all public ip address in an instance? – Techiescorner Nov 27 '16 at 13:55
-
@Techiescorner you can run something like `curl http://169.254.169.254/latest/meta-data/network/interfaces/macs/09:34:54:0a:9b:4a/public-ipv4s` to get the public IP for each interface using their mac address (you can get that from `ifconfig-a` ) – Iceman Feb 26 '19 at 08:00
-
Duke's suggestion below to hit checkip.amazonaws.com is simpler and requires no access to instance profile or IAM credentials, which may be restrcted in some environments.(running on CI provider for instance) – Eddie Aug 14 '19 at 15:59
-
On Windows I has to use double quotes for it to work on command prompt – Yahya Uddin Jan 05 '21 at 22:48
109
curl http://checkip.amazonaws.com
this returns the public ip address.

Abel Callejo
- 13,779
- 10
- 69
- 84

Duke Dougal
- 24,359
- 31
- 91
- 123
-
Was there an official documentation from AWS about this web service? Or is it some of those hacky ones? By the way **https** also works for that web service. Great answer there! – Abel Callejo May 25 '19 at 22:10
-
This is really interesting. Https is considerably slower than http, and, of course, both are much slower than curling http://169.254.169.254 (which does not allow for https). – Vinay Jul 20 '19 at 07:47
-
They have documented it in a small note here https://docs.aws.amazon.com/cli/latest/userguide/cli-services-ec2-sg.html#configuring-a-security-group – Tummala Dhanvi Feb 11 '21 at 11:04
-
For me, today, on a generally restricted AWS server, only [https://checkip.amazonaws.com](https://checkip.amazonaws.com) (with https, not http) worked, and not the http version. – phhu Jun 28 '23 at 17:50
14
If you are inside the instance -
$ curl icanhazip.com
162.202.17.123
here is one more way
$ curl -s ifconfig.me
162.202.17.123
these methods are not limited to just AWS.

markroxor
- 5,928
- 2
- 34
- 43
-
1Personally, I try and avoid these websites as I don't have control over what they capture and log around a public address. Better to use a controlled endpoint under your own infrastructure, aws cli, or curl on 169.254.169.254 as per the other answers. – Ari Oct 12 '20 at 00:34
-
1They'll also get sick of paying for the domain name and hosting one day, and your service will suddenly stop working because it can't resolve the random stranger's domain name and free service that you built a dependency on. – jbg Nov 15 '20 at 03:50
-
-
1
Get attached InstanceID with PublicIP.
aws ec2 describe-network-interfaces --query NetworkInterfaces[*].[Attachment.[InstanceId],Association.[PublicIp]] --output=json

adiga
- 34,372
- 9
- 61
- 83

Raghvendra Soni
- 11
- 1
0
Some of the ec2 linux images (e.g. debian 10) include a pre-installed ec2metadata cli tool which can retrieve the current vm's public ip among other metadata.
ec2metadata --public-ipv4