aws ec2: how to know public ip from inside ubuntu

2019-05-28 21:38发布

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!

3条回答
对你真心纯属浪费
2楼-- · 2019-05-28 22:22

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
查看更多
We Are One
3楼-- · 2019-05-28 22:22

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 -
查看更多
Deceive 欺骗
4楼-- · 2019-05-28 22:34

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"
        }
    ]
}
查看更多
登录 后发表回答