By using this site you agree to the use of cookies and related technologies.

Learn more

Multiple IPs with one EC2 instance

How to have multiple public IPs with one AWS EC2 Instance

Edited: 2016-10-04 18:15

If you login to the console in amazon web services you will find that it is possible to assign more than one external IP to one instance, to do this, you will need to right-click on a running instance, and then click on Networking->Manage Private IP Addresses, and note the VPC subnet size (in this case /20):

eth0: eni-97987gb99 -

You need to know the subnet size when adding the private IP in Ubuntu/linux.

You can now go to Elastic IPs and add allocate a new IP address, once you are done, right-click on the address and click on Associate Address, then choose one of your running instances.

Once you have assigned another IP to your instance you will also need to add the IP address to your network inside your operating system. On Ubuntu this is pretty simple, simply start by SSHing into your instance like you normally would, then enter the following command:

sudo ip addr add dev eth0

Note. the /20 part you see after the IP is the subnet size, you will need to replace this with the one of your instance – which you should have noted earlier. Finally, replace with the private IP address pointing to the Elastic IP you just assigned (you can find the private IP in the Elastic IP section of your AWS console).

Configuring the Apache to listen to multiple IPs

Before the web server will accept incoming requests on its newly added public IP, it may also need to have its VHOSTs configured to handle multiple IPs.

Note. If you have already setup your VHOST using name based virtual hosting, then you may be able to skip this part.

With the Apache HTTP server, virtual hosts are added in the VHOST configuration file, which is located at:


So if you want to accept requests for a given domain name, you can simply use the (*) wildcard in the VirtualHost block, and then enter the domain name as the ServerName. I.e.:

# ***********
# ***********
<VirtualHost *:80>
    DocumentRoot "/var/www/"
    ErrorLog ${APACHE_LOG_DIR}/
    CustomLog ${APACHE_LOG_DIR}/ common
    <Directory "/var/www/">
        Options FollowSymLinks
        AllowOverride All

        Order allow,deny
        Deny from all

Using the (*) wildcard name based method is much simpler than using the IP based method, and it avoids the problem of having to update your .conf file whenever you make changes to your IP configuration.

IP and Name based virtual hosting

When the server receives a request it will first look for a VirtualHost block that matches the IP of the request, if there is more than one matching block, it will move on and compare the ServerName and ServerAlias directives of the blocks with the host header of the request. Assuming the host header in the request matches either the ServerName or ServerAlias, and the match was unique, the server can finally process the matched VirtualHost block, and send it's response.

It is possible to mix IP based VHOST blocks with name based.

VHOST blocks that are purely IP based do not need a ServerName or ServerAlias directive in their blocks. So if you wanted to deliver a certain site based on the IP the request was received on, you would create a block matching the private IP that corresponds with the public IP. I.e.:

    # The rest of the configuration...
    # blah blah..

Blocks that are either partly IP based or name based should contain at least a ServerName each, which corresponds to the host you wish to match.

Name based virtual hosting is simpler than IP based, since you usually only need to setup the DNS of your domain names, making it to point to the public IP of your web server.

Final notes

1. Remember that IPV6 adresses should be specified in square brackets:

<VirtualHost [2001:db8::a00:20ff:fea7:ccea]:80>
    # The rest of the configuration...
    # blah blah..

2. You may have seen the Apache listen directive mentioned in other examples on the internet, but it should generally not be necessary to mess with that when adding extra IPs to your server.