DotDauz
DotDauz

Reputation: 11

How to show IPv4Address in Azure VM?

I setup two VMs (Ubuntu 18.04) in an availability set under a load balancer in Azure (also installed nginx). I then deployed a piece of Node JS code which includes:

global.AddressIpv4

I was expecting it to show the public IP address. However, it keeps showing the private IP address 10.0.0.6.

I'm not sure where to setup this up in Azure. I already put a public IP address to the network interface.

Any ideas why the private IP address keeps showing up and not the public one?

Upvotes: 0

Views: 429

Answers (1)

Sam Cogan
Sam Cogan

Reputation: 4334

The VM's network interface does not have any awareness of what it's public IP is, this is all handled by the SDN in Azure outside of the VM, the VM just gets traffic routed to it.

However, you can obtain the VMs public IP (assuming it is directly assigned and not assigned to a load balancer) using the metadata service. You can call a local URL on the VM and receive a JSON response with information on the VM, including the public IP.

In bash you can make the following call:

curl -H Metadata:true "http://169.254.169.254/metadata/instance/network?api-version=2017-08-01"

and it will return this:

{
  "interface": [
    {
      "ipv4": {
        "ipAddress": [
          {
            "privateIpAddress": "10.1.0.4",
            "publicIpAddress": "X.X.X.X"
          }
        ],
        "subnet": [
          {
            "address": "10.1.0.0",
            "prefix": "24"
          }
        ]
      },
      "ipv6": {
        "ipAddress": []
      },
      "macAddress": "000D3AF806EC"
    }
  ]
}

You can also use this to find out the VMs region, size etc. Full details here.

Upvotes: 1

Related Questions