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:

# ip addr add dev eth0 x2.x2.x2.x2/s

That’s it! The IP should now work.

In order to persist the change through a reboot edit /etc/network/interfaces:

auto lo
iface lo inet loopback
auto eth0 inet dhcp
iface eth0 inet dhcp
post-up ip addr add dev eth0 x2.x2.x2.x2/s

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:

auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
post-up ip addr add dev eth0 x2.x2.x2.x2/s

auto eth1
iface eth1 inet dhcp

Restart networking by running this on the shell:

# /etc/init.d/networking restart

Load the current routing data by running this on the shell:

# ip route show

The response will look something like this:

default via g.g.g.g dev eth0
<some_ip>/<some_number> dev eth0  scope link
<some_ip>/<some_number> dev eth0  proto kernel  scope link  src x1.x1.x1.x1
<some_ip>/<some_number> dev eth1  proto kernel  scope link  src x3.x3.x3.x3

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:

# ip route add default via g.g.g.g dev eth0 tab 1
# ip rule add from x1.x1.x1.x1/32 tab 1 priority 500
# ip route add default via g.g.g.g dev eth1 tab 2
# ip rule add from x3.x3.x3.x3/32 tab 2 priority 600

The third IP should now be active.

Persist the changes by editing /etc/network/interfaces:

auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
post-up ip route add default via g.g.g.g dev eth0 tab 1
post-up ip rule add from x1.x1.x1.x1/32 tab 1 priority 500
post-up ip addr add dev eth0 x2.x2.x2.x2/s

auto eth1
iface eth1 inet dhcp
post-up ip route add default via g.g.g.g dev eth1 tab 2
post-up ip rule add from x3.x3.x3.x3/32 tab 2 priority 600

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:

# ip addr add dev eth1 x4.x4.x4.x4/s
# ip rule add from x4.x4.x4.x4/32 tabe 2 priority 700

and similarly to /etc/network/interfaces:

auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
post-up ip route add default via g.g.g.g dev eth0 tab 1
post-up ip rule add from x1.x1.x1.x1/32 tab 1 priority 500
post-up ip addr add dev eth0 x2.x2.x2.x2/s

auto eth1
iface eth1 inet dhcp
post-up ip route add default via g.g.g.g dev eth1 tab 2
post-up ip addr add dev eth1 x4.x4.x4.x4/s
post-up ip rule add from x3.x3.x3.x3/32 tab 2 priority 600
post-up ip rule add from x4.x4.x4.x4/32 tab 2 priority 700

Final Notes

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.