aws ec2: how to know public ip from inside ubuntu instance [closed]

旧时模样 提交于 2019-12-10 11:25:14

问题


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 -


来源:https://stackoverflow.com/questions/37271627/aws-ec2-how-to-know-public-ip-from-inside-ubuntu-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!