When you create an instance with AWS by default it is assigned a DHCP address. This is not always a good thing and for more control over your environment it is a good idea for instances to get the IP address that you want. In this procedure I am going to spin up an instance of Ubuntu and assign it a static IP address.
I have already configured VPC with two subnets that are routed to the internet.
Step 1 is to create a security group in the VPC environment.
I edited the rules to allow SSH and all ICMP from my computer.
Step 2 is to go into EC2 and create an Instance I use Ubuntu in this example.
Make sure it is in the correct subnet in this case subnet a.
This is really hard to reverse so it is a good idea to get this right in the first place.
Click to select existing security group
Choose the security group that was created in Step 1
Choose the one just created and click review and launch instance.
And click Launch
Select an existing key pair and click to Launch Instance
When you create an instance it gets a randomly assigned IP address. If you want more control a manual static address is a better idea.
To do this you need to create another network card which has a static IP and assign that with the instance.
Navigate to Network Interfaces and click Create Network Interface
Add a description
Assign it to the correct Subnet
And type in a static IP address (1 is actually reserved so I used 172.31.1.10)
Next right click on the interface and click Attach
Select the instance that you want to attach the interface to.
Now the instance has the static network adapter attached.
However the linux instance does not know this yet.
Next you need to associate an elastic IP address this one needs to be associated with the network adapter that was created with the DHCP assigned IP address.
Navigate to VPC Dashboard > Elastic IPs and Allocate New Address
Click Yes Allocate
Select the public IP address and choose Associate Address
Under Associate With select Network interface
Under Network interface select the interface that has the Dynamic Address not the one with the static address created earlier.
The reason for this is that the script that created the instance can set up the configuration files to point to that interface.
Step 3 SSH into the Server
Doing ifconfig shows the first interface but proves that ubuntu does not even know about the other interface.
As you can see there is only one network interface configured eth0
This is how it is configured
A configuration file needs to be created for eth1 a quick way to do the is using cp to copy eth0 to eth1 and then modify eth1 with vi.
sudo vi /etc/network/interfaces.d/eth1.cfg
Modify this to eth1 and change the comment to secondary interface.
It is fine that it says dhcp because this is a DHCP reserved address.
Now it is time to bring up the interface with:
sudo ifup eth1
The interface is up and it has the correct IP address.
Now the routing table needs to be modified.
As you can see the default route points to eth0 this need to be changed. So use the following command:
sudo ip route add default via 172.31.1.1 dev eth1 tab 2
sudo ip rule add from 172.31.1.10/32 tab 2 priority 200
Now go to elastic IPs and associate the public address with the static interface
Associate it with the Network Interface connected to the Static IP address.
Now the server has a static IP address.