Skip to content

How do I change the primary private IP address of my EC2 instance?

2 minute read
1

I want to change the private IP address of my Amazon Elastic Compute Cloud (Amazon EC2) instance.

Resolution

When you launch an EC2 instance in a subnet, Amazon EC2 assigns a primary private IP address from the subnet's IPv4 address range to the instance. If you don't specify a primary private IP address, then Amazon EC2 assigns an available IP address in the subnet's IPv4 address range to the instance. Amazon EC2 assigns a default elastic network interface (eth0) to the primary private IPv4 address of every instance.

You can't change the primary IP address of an instance after you launched the instance. All private IPv4 IP addresses, whether primary or secondary, remain associated with the network interface of the instance until you terminate the instance. For more information, see Private IPv4 addresses.

You can launch a new instance and then assign the preferred IP address to the instance. Complete the following steps:

  1. Create an Amazon Machine Image (AMI) of your instance.
  2. Launch a new instance from the AMI.
  3. To assign the preferred IP address to your new instance from the same subnet, edit the Advanced network configuration when you launch the instance. Then for Primary IP, enter the IP address that you want to use.

You can also exchange the IP address from an existing EC2 instance to another instance. Change the network interface termination behavior of the instance that has the IP address you want to use. By default, Delete on instance termination is turned on for EC2 instances.

To exchange the IP address of an existing EC2 instance to another instance, complete the following steps:

  1. Open the Amazon EC2 console.
  2. Choose Network Interfaces, and then choose Actions.
  3. Choose Change Termination Behavior, and then uncheck the checkbox for Delete on termination.
  4. Create an AMI of the instance, and then terminate the instance.
  5. Launch a new instance from the AMI.
  6. In the launch wizard, for Network Settings, choose the same virtual private cloud (VPC) and subnet that your terminated instance used.
  7. Expand Advanced network configuration. Then, under Network interfaces, select the terminated instance's network interface from the dropdown menu.
  8. Launch the new instance.

Related information

Amazon EC2 instance IP addressing

AWS OFFICIALUpdated 7 months ago