aws ec2: how to know public ip from inside ubuntu

2019-05-28 21:43发布

问题:

I created a amazon-web-services instance and used the Ubuntu 14.04 amazon machine image.

now I can ssh into that machine and use the shell to run different commands.

my question is now, how I can find out the public IP from that machine from inside that ssh-session?

I tried using

netstat

and

ifconfig

but cannot find out the public IP I locked in with via ssh.

Is there somebody who can tell me how to find out, the ip I used to ssh into the machine?

Thanks in advance!

回答1:

This question has been ask many time. What you need is Instance Metadata and User Data.

Just do this and refer to the mentioned documentation, you get the public ip address :

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


回答2:

You can't really just get the Public IP from the standard linux command

You can check the web console (but apparently its not something you want) or use the aws CLI

[xxx@IP-xxxx ~]# aws ec2 describe-addresses
{
    "Addresses": [
        {
            "Domain": "vpc",
            "InstanceId": "xxxx",
            "NetworkInterfaceId": "eni-xxxx",
            "AssociationId": "eipassoc-xxxx",
            "NetworkInterfaceOwnerId": "xxxx",
            "PublicIp": "THE PUBLIC IP",
            "AllocationId": "eipalloc-xxxx",
            "PrivateIpAddress": "THE INTERNAL IP the instance knows"
        }
    ]
}


回答3:

The format of your ssh command is sommething like

ssh -i "xxx.pem" ubuntu@ec2-54-252-113-980.ap-souheast-1.compute.amazonaws.com

than the public IP of your instance is 54.252.113.980, the numbers after ubuntu@ec2.

Other command line options for getting the public IP on ubuntu are:

  1. curl ipecho.net/plain ; echo
  2. wget http://ipinfo.io/ip -qO -