Manage the IPv4 addresses for your EC2 instances - Amazon Elastic Compute Cloud

Manage the IPv4 addresses for your EC2 instances

You can assign a public IPv4 address to your instance when you launch it. You can view the IPv4 addresses for your instance in the console through either the Instances page or the Network Interfaces page.

View the IPv4 addresses

You can use the Amazon EC2 console to view the public and private IPv4 addresses of your instances. You can also determine the public IPv4 and private IPv4 addresses of your instance from within your instance by using instance metadata. For more information, see Use instance metadata to manage your EC2 instance.

The public IPv4 address is displayed as a property of the network interface in the console, but it's mapped to the primary private IPv4 address through NAT. Therefore, if you inspect the properties of your network interface on your instance, for example, through ifconfig (Linux) or ipconfig (Windows), the public IPv4 address is not displayed. To determine your instance's public IPv4 address from an instance, use instance metadata.

To view the IPv4 addresses for an instance using the command line

You can use one of the following commands. For more information about these command line interfaces, see Access Amazon EC2.

To determine your instance's IPv4 addresses using instance metadata
  1. Connect to your instance. For more information, see Connect to your EC2 instance.

  2. Use the following command to access the private IP address.

    IMDSv2
    [ec2-user ~]$ TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` \ && curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/local-ipv4
    IMDSv1
    [ec2-user ~]$ curl http://169.254.169.254/latest/meta-data/local-ipv4
    Tools for Windows PowerShell
    PS C:\> Invoke-RestMethod http://169.254.169.254/latest/meta-data/local-ipv4
  3. Use the following command to access the public IP address. If an Elastic IP address is associated with the instance, the value returned is that of the Elastic IP address.

    IMDSv2
    [ec2-user ~]$ TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` \ && curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/public-ipv4
    IMDSv1
    [ec2-user ~]$ curl http://169.254.169.254/latest/meta-data/public-ipv4
    Tools for Windows PowerShell
    PS C:\> Invoke-RestMethod http://169.254.169.254/latest/meta-data/public-ipv4

Assign a public IPv4 address during instance launch

Each subnet has an attribute that determines whether instances launched into that subnet are assigned a public IP address. By default, nondefault subnets have this attribute set to false, and default subnets have this attribute set to true. When you launch an instance, a public IPv4 addressing feature is also available for you to control whether your instance is assigned a public IPv4 address; you can override the default behavior of the subnet's IP addressing attribute. The public IPv4 address is assigned from Amazon's pool of public IPv4 addresses, and is assigned to the network interface with the device index of eth0. This feature depends on certain conditions at the time you launch your instance.

Considerations
  • You can unassign the public IP address from your instance after launch by managing the IP addresses associated with a network interface. For more information about public IPv4 addresses, see Public IPv4 addresses.

  • You cannot auto-assign a public IP address if you specify more than one network interface. Additionally, you cannot override the subnet setting using the auto-assign public IP feature if you specify an existing network interface for eth0.

  • Whether you assign a public IP address to your instance during launch or not, you can associate an Elastic IP address with your instance after it's launched. For more information, see Elastic IP addresses. You can also modify your subnet's public IPv4 addressing behavior. For more information, see Modify the public IPv4 addressing attribute for your subnet.

To assign a public IPv4 address during instance launch using the console

Follow the procedure to launch an instance, and when you configure Network Settings, choose the option to Auto-assign Public IP.

To enable or disable the public IP addressing feature using the command line

You can use one of the following commands. For more information about these command line interfaces, see Access Amazon EC2.

  • Use the --associate-public-ip-address or the --no-associate-public-ip-address option with the run-instances command (AWS CLI)

  • Use the -AssociatePublicIp parameter with the New-EC2Instance command (AWS Tools for Windows PowerShell)