
It was a Tuesday. I did what I’ve done a thousand times: I logged into an Amazon EC2 instance using its public IP, a respectable 52.95.110.21. Routine stuff. Once inside, muscle memory took over, and I typed ip addr to see the network configuration.
And then I blinked.
inet 10.0.10.147/24.
I checked my terminal history. Yes, I had connected to 52.95.110.21. So, where did this 10.0.10.147 character come from? And more importantly, where on earth was the public IP I had just used? For a moment, I felt like a detective at a crime scene where the main evidence had vanished into thin air. I questioned my sanity, my career choice, and the very fabric of the TCP/IP stack.
Suppose you’ve ever felt this flicker of confusion, congratulations. You’ve just stumbled upon one of AWS’s most elegant sleights of hand. The truth is, the public IP doesn’t exist inside the instance. It’s a ghost. A label. A brilliant illusion.
Let’s put on our detective hats and solve this mystery.
Meet the real resident
Every EC2 instance, upon its creation, is handed a private IP address. Please think of this as its legal name, the one on its birth certificate. It’s the address it uses to talk to its neighbors within its cozy, gated community, the Virtual Private Cloud (VPC). This instance, 10.0.10.147, is perfectly happy using this address to get a cup of sugar from the database next door at 10.0.10.200. It’s private, it’s intimate, and frankly, it’s nobody else’s business.
The operating system itself, be it Ubuntu, Amazon Linux, or Windows, is blissfully unaware of any other identity. As far as it’s concerned, its name is 10.0.10.147, and that’s the end of the story. If you check its network configuration, that’s the only IP you’ll see.
# Check the network interface, see only the private IP
$ ip -4 addr show dev eth0 | grep inet
inet 10.0.10.147/24 brd 10.0.10.255 scope global dynamic eth0
So, if the instance only knows its private name, how does it have a public life on the internet?
The master of disguise
The magic happens outside the instance, at the edge of your VPC. Think of the AWS Internet Gateway as a ridiculously efficient bouncer at an exclusive club. Your instance, wanting to go online, walks up to the bouncer and whispers, “Hey, I’m 10.0.10.147
And I need to fetch a cat picture from the internet.“
The bouncer nods, turns to the vast, chaotic world of the internet, and shouts, “HEY, TRAFFIC FOR 52.95.110.21
, COME THIS WAY!”
This translation trick is called 1-to-1 Network Address Translation (NAT). The Internet Gateway maintains a secret map that links the public IP to the private IP. The public IP is just a mask, a public-facing persona. It never actually gets assigned to the instance’s network interface. It’s all handled by AWS behind the scenes, a performance so smooth you never even knew it was happening.
When your instance sends traffic out, the bouncer (Internet Gateway) swaps the private source IP for the public one. When return traffic comes back, the bouncer swaps it back. The instance is none the wiser. It lives its entire life thinking its name is 10.0.10.147
, completely oblivious to its international fame as 52.95.110.21
.
Interrogating the local gossip
So, the instance is clueless. But what if we, while inside, need to know its public identity? What if we need to confirm our instance’s public IP for a firewall rule somewhere else? We can’t just ask the operating system.
Fortunately, AWS provides a nosy neighbor for just this purpose: the Instance Metadata Service. This is a special, link-local address (169.254.169.254
) that an instance can query to learn things about itself, things the operating system doesn’t know. It’s the local gossip line.
To get the public IP, you first have to get a temporary security token (because even gossip has standards these days), and then you can ask your question.
# First, grab a session token. This is good for 6 hours (21600 seconds).
$ TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
# Now, use the token to ask for the public IP
$ curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/public-ipv4
52.95.110.21
And there it is. The ghost in the machine, confirmed.
A detective’s field guide
Now that you’re in on the secret, you can avoid getting duped. Here are a few field notes to keep in your detective’s journal.
The clueless security guard. If you’re configuring a firewall inside your instance (like iptables
or UFW), remember that it only knows the instance by its private IP. Don’t try to create a rule for your public IP; the firewall will just stare at you blankly. Always use the private IP for internal security configurations.
The persistent twin: What if you need a public IP that doesn’t change every time you stop and start your instance? That’s what an Elastic IP (EIP) is for. But don’t be fooled. An EIP is just a permanent public IP that you own. Behind the scenes, it works the same way: it’s a persistent mask mapped via NAT to your instance’s private IP. The instance remains just as gloriously ignorant as before.
The vanishing act. If you’re not using an Elastic IP, your instance’s public IP is ephemeral. It’s more like a hotel room number than a home address. Stop the instance, and it gives up the IP. Start it again, and it gets a brand new one. This is a classic “gotcha” that has broken countless DNS records and firewall rules.
The case is closed
So, the next time you log into an EC2 instance and see a private IP, don’t panic. You haven’t SSH’d into your smart fridge by mistake. You’re simply seeing the instance for who it truly is. The public IP was never missing; it was just a clever disguise, an elegant illusion managed by AWS so your instance can have a public life without ever having to deal with the paparazzi. The heavy lifting is done for you, leaving your instance to worry about more important things. Like serving those cat pictures.