Score:0

how to get the ip of my Ubuntu instance in AWS

lu flag

How can I see the public IP address assigned to an EC2 instance, from within (i.e. after connecting) the EC2 instance? I can see the public IP address that is assigned to it, simply by looking in the Management Console.

From AWS document, we can get the public ip address using the following curl 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

but looks like it doesn't work.

$ 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/

Any idea how to get the public ip in EC2 instance?

Score:1
ug flag
  • For private ip address, you can use Ubuntu command like ifconfig or ip addr command to get it. You can also get the ip address for one specific interface with ip addr show dev <interface name>. More info is explained in this post.

    Here is one example:

    $ ip addr  show dev eth0
    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9001 qdisc mq state UP group default qlen 1000
        link/ether 02:3f:4d:eb:52:e7 brd ff:ff:ff:ff:ff:ff
        inet 10.1.0.143/24 brd 10.1.0.255 scope global dynamic noprefixroute eth0
           valid_lft 2166sec preferred_lft 2166sec
    
  • For public ip address, you can use aws ec2 command. This command will list the details about your instance like disk info, network interface, aws instance type etc. It is very helpful to get more info about your ec2 instance. You might need install awscli before run this command. The following example will list the instance details based on the private ip address filter.

    aws ec2 describe-instances --filters Name=private-ip-address,Values=10.6.10.199
    
NotTheDr01ds avatar
vn flag
Welcome to Ask Ubuntu! Not a bad first answer honestly (probably better than my first answer, IIRC), but I think you might have missed an important detail in the original question -- *"public IP ... from **within** (i.e. after connecting)"*. You provide info on how to get the *private* IP from *within* and the *public* IP from *without*. IIRC, [@FedKad's answer](https://askubuntu.com/a/1443201/1165986) is the right way to do this, using the "magic" http://169.254.169.254/latest/meta-data URI.
NotTheDr01ds avatar
vn flag
Looking forward to more answers from you in the future though - You do take the time to explain things well. Keep it up! Giving you an upvote for the quality of the answer even though I don't believe it addresses this particular question.
aiziyou avatar
ug flag
Thanks a lot for the comments. I will keep it in mind. Thanks.
Score:1
cn flag

A simpler curl command would be:

$ curl -s http://169.254.169.254/latest/meta-data

and will return the following output:

ami-id
ami-launch-index
ami-manifest-path
block-device-mapping/
events/
hostname
identity-credentials/
instance-action
instance-id
instance-life-cycle
instance-type
ipv6
local-hostname
local-ipv4
mac
metrics/
network/
placement/
profile
public-hostname
public-ipv4
public-keys/
reservation-id
security-groups
services/

As you see here, the URL to get the "public IPv4" would be:

$ curl -s http://169.254.169.254/latest/meta-data/public-ipv4

Some other (shell script) examples:

localIP=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)
publicIP=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4)
inst_type=$(curl -s http://169.254.169.254/latest/meta-data/instance-type)
av_zone=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)

On the other hand, to get the local IPv4 address and the IPv6 address (AWS does not provide an API to query the IPv6 address) with a system command, you can use:

$ hostname -I

Note that there is no way to get the public IPv4 unless you ask for it on an external server, like the "AWS server" (http://169.254.169.254/) or servers on Internet (as provided in the other answer).

Score:0
in flag

I generally use an external service for this. If you would like the IPv4 address for your server to be recorded in a file named ip_v4.txt, you can do this:

wget -q -nv https://api.ipify.org -O ip_v4.txt

If you would like the IPv6 address in a file named ip_v6.txt, you can do this:

wget -q -nv https://api64.ipify.org -O ip_v6.txt

Naturally, you may need to customise this to output the address in the manner that you'd like.

This gets around the issues of using Amazon's various APIs and provides something a little more universal that can work on any Linux-based server, regardless of where it is hosted.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.