Computer NetworksNETWORKS

Why do I only see the private IP address when logged into an AWS EC2 instance

📚Book link: https://pratimuniyal.gumroad.com/l/cracking-the-devops-interview

This is a common question that comes to everyone’s mind. Some of you may have thought about it, while others might have ignored it.

✅ The simple answer to this question is that you don’t see the public IP address directly within the instance because this is how AWS networking is structured, particularly involving public and private IP addresses, NAT, and Elastic IP addresses. Let’s understand this with step by step.

Let’s first start with Private IP address
✅ What is a Private IP address?
A private IP address is assigned to a Virtual Private Cloud subnet instance. This address is only routable within the V P C and is used for communication between instances within the same V P C.

✅ Now, what is a Public IP address?
A public IP address is assigned to an instance to allow communication over the internet. This IP address is routable over the internet and can be used to access the instance from outside the VPC

✅ Now combine both of them and see how it works
When you launch an EC2 instance in a VPC, AWS assigns a private IP address to the instance from the IP range of the subnet. If you choose to associate a public IP address, AWS handles this as follows
1️⃣ AWS assigns a public IP address from its pool. This public IP address is mapped to the private IP address of the instance
2️⃣ AWS uses Network Address Translation to map the public IP address to the private IP address. This allows the instance to be accessible from the internet while keeping its private IP address within the VPC.
3️⃣ When you send a request to the public IP address, AWS routes the request to the private IP address of the instance using NAT. This ensures that the instance receives the request on its private IP address. The instance has an Elastic Network Interface that holds the private IP address. This ENI is configured within the VPC. The public IP address is mapped to the private IP address through the ENI, enabling communication with the instance via the public IP address.

🙋‍♂️ But I still have the same question: why do I still not see the public IP when I log into the instance?

When you log into the instance via SSH or any other method: You are actually connecting to the public IP address, but the NAT process translates this to the private IP address, allowing you to establish the connection. Once logged in, the instance itself only recognizes its private IP address because the public IP address is an external mapping managed by AWS

So is there is anyway I can see public IP associated with the instance, the answer is yes?
You can query the instance metadata to retrieve the public IP address from within the instance.

# Step 1: Get a session token
TOKEN=`curl -X PUT “http://169.254.169.254/latest/api/token” -H “X-aws-ec2-metadata-token-ttl-seconds: 21600″`

# Step 2: Use the token to get the public IPv4 address
curl -H “X-aws-ec2-metadata-token: $TOKEN” “http://169.254.169.254/latest/meta-data/public-ipv4”

source

ipv4

Alice AUSTIN

Alice AUSTIN is studying Cisco Systems Engineering. He has passion with both hardware and software and writes articles and reviews for many IT websites.

One thought on “Why do I only see the private IP address when logged into an AWS EC2 instance

Comments are closed.