How do I add a second Elastic IP address to an elastic network interface attached to my EC2 instance running CentOS 6 or RHEL 6?
Last updated: 2020-07-30
How do I add a second Elastic IP address to an elastic network interface attached to my Amazon Elastic Compute Cloud (Amazon EC2) instance running CentOS 6 or RHEL 6 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 you reboot the interface. 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 two Elastic IP addresses exist 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 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: IPADDR uses 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 in the Amazon 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 verify detection of the second ICF:
$ ethtool eth0:1
The output appears similar to the following:
Settings for eth0:1: Link detected: yes
If the output isn't as expected, run the ifup command and verify that the second interface is present. 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.