Mandeep Singh
Mandeep Singh

Reputation: 8234

How to assign multiple public IPs to an AWS EC2 instance?

I have an m4.4xlarge instance to which I initially assigned an Elastic IP. The security group of this instance allows SSH access and also allows access to the web app on port 8000.

Now I clicked on the EC2 instance, chose: Actions > Networking > Manage IP addresses. And then I assigned a new private IP.

Then I created a new Elastic IP address and mapped it to the newly assigned private IP of the network interface. Now I can see in the EC2 instance description that Elastic IPs is showing both old and new Elastic IP. But the IPv4 Public IP field is still showing the old IP address only.

While I am still able to SSH to the instance using the old Elastic IP, I am not able to do so using the new Elastic IP. Also, I am not able to access the web app on port 8000 using new Elastic IP. How can I accomplish this ?

Upvotes: 2

Views: 4069

Answers (2)

Xin Xin
Xin Xin

Reputation: 1

You need config second IP addr in your OS, for example CentOS,if the primary network interface is eth0, then you need add eth0:1 as following:

sudo  vi /etc/sysconfig/network-scripts/ifcfg-eth0:1

DEVICE=eth0:1
Type=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=none
IPADDR=10.0.0.30
PREFIX=24

Then, reboot your EC2 instance, eg. sudo reboot.

Upvotes: 0

garfield101
garfield101

Reputation: 41

Here is the script I wrote for making it work with additional network interface and making the change persistent on RHEL/Centos -

#!/bin/bash
# On AWS With multiple network cards with the default route tables the outbound public traffic keeps going out via the default interface
# This can be tested by running tcpdump on default interface and then sending a ping to the 2nd interface
# The second address will try to send return traffic via the 1st interface
# To fix this need to create a rule to direct traffic from second address through the 2nd network interface card
# Also creating a systemd service that will create the rules and routes on boot and also
# adding to the network.service so the script is also called when starting network


    # User inputs
    INTERFACE1="eth0"
    INTERFACE2="eth1"
    IP1=10.0.0.70/32
    IP2=10.0.5.179/32
    ROUTER1=10.0.0.1
    ROUTER2=10.0.5.1
    # End of user inputs

    if [[ $EUID != "0" ]]
    then
        echo "ERROR. You need root privileges to run this script"
        exit 1
    fi


    # Create the file that will be called by the systemd service
    rm -rf /usr/local/src/routes.sh
    cat << EOF > /usr/local/src/routes.sh
    #!/bin/bash
    # Adding the routes for the 2nd network interface to work correctly
    ip route flush tab 1 >/dev/null 2>&1
    ip route flush tab 2 >/dev/null 2>&1
    ip rule del priority 500 >/dev/null 2>&1
    ip rule del priority 600 >/dev/null 2>&1
    ip route add default via $ROUTER1 dev $INTERFACE1 tab 1
    ip route add default via $ROUTER2 dev $INTERFACE2 tab 2
    ip rule add from $IP1 tab 1 priority 500
    ip rule add from $IP2 tab 2 priority 600
    EOF
    chmod a+x /usr/local/src/routes.sh
    # End of file with new routes and rules


    # Create a new systemd service
    rm -rf /etc/systemd/system/multiple-nic.service
    cat << EOF > /etc/systemd/system/multiple-nic.service
    [Unit]
    Description=Configure routing for multiple network interface cards
    After=network-online.target network.service
    [Service]
    ExecStart=/usr/local/src/routes.sh
    [Install]
    WantedBy=network-online.target network.service
    EOF
    # End of new systemd service
    echo "New systemd service - multiple-nic.service created"

    systemctl enable multiple-nic.service

    systemctl restart network
    echo "Network restarted successfully"

Upvotes: 4

Related Questions