Shardool Singh
Shardool Singh

Reputation: 81

How does an EC2 instance have both a private and a public IP with only one network interface

I was particularly interested as to how does an EC2 instance have both a public IP and a private IP, while I login to an EC2 instance and I only see one network interface via the ifconfig command?

Upvotes: 4

Views: 4200

Answers (2)

abiydv
abiydv

Reputation: 621

The AWS Docs explain it well -

Each instance that receives a public IP address is also given an external DNS hostname; for example, ec2-203-0-113-25.compute-1.amazonaws.com. We resolve an external DNS hostname to the public IP address of the instance outside the network of the instance, and to the private IPv4 address of the instance from within the network of the instance. The public IP address is mapped to the primary private IP address through network address translation (NAT).

Upvotes: 3

Kush Vyas
Kush Vyas

Reputation: 6099

Let me try to explain you in the way I understand this,

Suppose you have a machine (Local Desktop) you are working on in your Office/School.

The Private IP Address is the IP that is not reachable outside of your network :

10.0.0.0 – 10.255.255.255 Class A

172.16.0.0 – 172.31.255.255 Class B

192.168.0.0 – 192.168.255.255 Class C

Now your machine would be behind a SWITCH or Firewall like many others which would have public IP (Static IP in case of ISP Terminology) so when a someone needs to communicate with you from outside they will communicate with you with your public ip , the switch will have job to convert and route that request to your machine.

So Despite your machine having only one Network Card you can say you have one local address and one that is public ip.

Now to Say it in AWS Terminology as Per AWS Documentation :

A private IPv4 address is an IP address that's not reachable over the Internet. You can use private IPv4 addresses for communication between instances in the same network (EC2-Classic or a VPC).

When you launch an instance, AWS allocates a primary private IPv4 address for the instance. Each instance is also given an internal DNS hostname that resolves to the primary private IPv4 address; for example, ip-10-251-50-12.ec2.internal. You can use the internal DNS hostname for communication between instances in the same network, but AWS can't resolve the DNS hostname outside the network that the instance is in.

Each instance in a VPC has a default network interface (eth0) that is assigned the primary private IPv4 address.

Where as a Public IP address is an IPv4 address that's reachable from the Internet. You can use public addresses for communication between your instances and the Internet.

AWS resolves an external DNS hostname to the public IP address of the instance outside the network of the instance, and to the private IPv4 address of the instance from within the network of the instance. The public IP address is mapped to the primary private IP address through network address translation (NAT). For more information about NAT, see RFC 1631: The IP Network Address Translator (NAT).

This Explains how AWS Translates Public IP into Private IP , Do Read the AWS Documentation as it will further clear your doubt.

Hope this clears your doubt

Upvotes: 2

Related Questions