Assigning a static IP Address to a Linux VPC Instance

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.

_1

 

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.

1

2

3

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.

4

5

6

Click to select existing security group

Choose the security group that was created in Step 1

7

Choose the one just created and click review and launch instance.

8

And click Launch

9

Select an existing key pair and click to Launch Instance

10

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.

 11

Navigate to Network Interfaces and click Create Network Interface

12

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)

13

Next right click on the interface and click Attach

14

15

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.

16

Navigate to VPC Dashboard > Elastic IPs and Allocate New Address

17

Click Yes Allocate

18

Select the public IP address and choose Associate Address

Under Associate With  select Network interface

19

20

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.

 21

Step 3 SSH into the Server

23

Doing ifconfig shows the first interface but proves that ubuntu does not even know about the other interface.

24

25

26

As you can see there is only one network interface configured eth0

This is how it is configured

27

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.

28

sudo  vi /etc/network/interfaces.d/eth1.cfg

29

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.

30

Now it is time to bring up the interface with:

sudo ifup eth1

31

The interface is up and it has the correct IP address.

Now the routing table needs to be modified.

32

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

33

sudo ip rule add from 172.31.1.10/32 tab 2 priority 200

34.JPG

Now go to elastic IPs  and associate the public address with the static interface

35

Associate it with the Network Interface connected to the Static IP address.

Now the server has a static IP address.