How do I add a second Elastic IP address to CentOS or RHEL with a single elastic network interface to an EC2 instance and have it persist during bootup?

When you add a second Elastic IP address to an elastic network interface, that Elastic IP address is lost when the interface is rebooted. To make the second Elastic IP address persist during the reboot, you must create a second interface configuration file (ICF).

ICFs control the software interfaces for individual network devices. The system uses these files as it boots to determine what interfaces to bring up and how to configure them.

The default ICF is /etc/sysconfig/network-scripts/ifcfg-eth0. When there are two Elastic IP addresses on a single interface, the second Elastic IP address becomes ":1"—that is, /etc/sysconfig/network-scripts/ifcfg-eth0:1.

Create a second interface configuration file

1.    Attach two Elastic IP addresses to the elastic network interface from the Amazon Elastic Compute Cloud (Amazon EC2) console. For more information, see Multiple IP Addresses.

2.    Use the touch command to create the ifcfg-eth0:1 file for the second Elastic IP address in the /etc/sysconfig/network-scripts/ directory:

$ sudo touch /etc/sysconfig/network-scripts/ifcfg-eth0:1

3.    Add the following parameters to the ifcfg-eth0:1 file:

DEVICE=eth0:1
BOOTPROTO=static
NETMASK=255.255.255.0
ONBOOT=yes
TYPE=Ethernet
IPADDR=172.31.34.195

Note: For IPADDR, this is the private IP address associated with the second Elastic IP address you associated with the interface. After you select your instance, you can find the private IP address from the EC2 console under Secondary private IPs.

Change the context of the second ICF to match the default ICF

1.    To view the security context of the ifcfg-eth0:1 file, use the –Z option with the ls command:

$ ls -Z ifcfg-eth*

2.    Change the user to system_u using the –u option with the chcon command:

$ sudo chcon -u system_u ifcfg-eth0:1

3.    Change the type to net_conf_t using the –t option with the chcon command:

$ sudo chcon -t net_conf_t ifcfg-eth0:1

4.    Compare the two files by running the following command:

$ ls -Z ifcfg-eth0*
-rw-r--r--. root root system_u:object_r:net_conf_t:s0  ifcfg-eth0
-rw-r--r--. root root system_u:object_r:net_conf_t:s0  ifcfg-eth0:1

Bring up the interface

1.    Bring up the second interface by running the ifup command:

$ sudo ifup eth0:1

2.    If there are issues with the second ICF, run the ethtool command to be sure that the second ICF is being detected: 

$ ethtool eth0:1

The output should look similar to this:

Settings for eth0:1:
Link detected: yes

If the output is not as expected, be sure that the second interface is present after running the ifup command. Then, review the ICF file to be sure that it is correct, and reload it.

Reboot the instance

Reboot your instance by running the reboot command.


Did this page help you? Yes | No

Back to the AWS Support Knowledge Center

Need help? Visit the AWS Support Center

Published: 2018-08-10