For an Amazon EC2 instance, IPs are associated through a network interface. You can have multiple IPs per interface and multiple interfaces per instance, but you are limited depending on the instance type. For example, a t2.micro instance can have 2 interfaces and 2 IPs per interface.
The easiest way to add an IP is to add it to the existing network interface, but when you max out the number of IPs on the interface, the next IP will have to be added to a new interface. I’ll go over the process as if I’m adding IPs to a t2.micro instance. I believe once you see the process for adding the first four IPs to such an instance, you should also be able to see a pattern emerge that should work for any number of interfaces or IPs.
Note that I will only go over adding the private IP to the instance. Once the private IP is setup, the associated public IP, if there is one, should just work.
All commands will probably need to be run with root privs.
Let’s refer to the current primary private IP associated with the instance as x1.x1.x1.x1.
Adding the Second IP
In the Amazon EC2 console, go to the Network Interfaces page. Select the interface associated with the instance. There should only be one at this point. Click “Assign new IP”r and then “Yes, Update.” You should now see the new IP listed in Secondary private IPs. Make a note of this IP. We’ll refer to it going forward as x2.x2.x2.x2. Also make a note of the subnet mask that appears at the line up top that looks like “eth0 - eni&heelip; - ip.ip.ip.ip/s” You’re interested in the /s part of this line.
Now ssh into your server. Simply run:
That’s it! The IP should now work.
In order to persist the change through a reboot edit /etc/network/interfaces:
Adding the Third IP
Since we’ve maxed out the IPs for the first network interface on our micro instance, we’ll need to create a new network interface. This can be done under Network Interfaces in AWS.
Once you’ve created a new interface, a private IP should already be associated with it. Let’s refer to this IP as x3.x3.x3.x3.
Add the new interface to /etc/network/interfaces:
Restart networking by running this on the shell:
Load the current routing data by running this on the shell:
The response will look something like this:
The only data we care about is g.g.g.g in the “default via” line. Make a note of it.
Run the following in the shell:
The third IP should now be active.
Persist the changes by editing /etc/network/interfaces:
Adding the Fourth IP
At this point, you should be getting the hang of things. Add an additional private IP to the second network interface in the Amazon EC2 console. This will be similar to how you added the second IP to the first network interface.
On the server, run the following in the shell:
and similarly to /etc/network/interfaces:
Whew, we’ve now gotten all our IPs associated to the instance. I wanted to make a quick note that while I prefer this order of adding IPs, there aren’t any set rules. You could add the second IP by adding a new network interface and follow the steps for adding the third IP in my post.