如何获取 Amazon EC2 实例的辅助私有 IP 地址

ben*_*ikt 3 amazon-ec2 amazon-web-services amazon-vpc

在我的实例启动期间,我会自动分配一个辅助私有 IPv4 地址。要配置 Ubuntu 以识别这个辅助 IP,我需要运行

ip addr add 10.0.1.15/24 dev eth0
Run Code Online (Sandbox Code Playgroud)

To access the primary private IP address I have found this command (described in the AWS Docs and in figaros question):

curl http://169.254.169.254/latest/meta-data/local-ipv4
Run Code Online (Sandbox Code Playgroud)

But how can I get the secondary IP address to run the ip command automatically on startup? For Amazon Linux Systems there's the ec2-net-utils package wich can handle this automatically.

小智 6

You can get secondary address with 2 commands.

Get mac address of your network interface

MAC=`curl http://169.254.169.254/latest/meta-data/mac` 
Run Code Online (Sandbox Code Playgroud)

Get all IP addresses for this mac address

curl http://169.254.169.254/latest/meta-data/network/interfaces/macs/${MAC}/local-ipv4s
Run Code Online (Sandbox Code Playgroud)