Ravindra BagaleCourses & study guides मराठी Track your progress

AWS Cloud

Public vs Private IP Address Explained

Let's start. In the previous chapter we learnt what an IP address is, how to count IPv4 addresses, and the difference between static and dynamic IP. Now we answer one simple question: if there are only about 429 crore IPv4 addresses, how is the whole world still online? On the way we will see your home router, ports, the web server, SSH, IP classes, localhost, and why free WiFi is never really free. We finish with the reason every part of an IP stops at 255. Don't worry, you will get it slowly.

In this class

  • Recap: 4,294,967,296 IPv4 addresses
  • Why dynamic IP alone is not enough
  • Public IP and private IP
  • Talking without internet, by distance
  • How your router does NAT
  • Why IPv4 has not run out yet
  • Router public IP: static or dynamic?
  • WiFi and mobile data together
  • Mobile hotspot: your phone as a router
  • Ports, and the default ports 80 and 443
  • Web server and /var/www/html
  • SSH on port 22
  • URL syntax: protocol rules
  • IP classes A to E, and CIDR
  • How many devices fit in a network
  • Private ranges and cloud work
  • Who hands out IP addresses
  • Loopback, 127.0.0.1 and localhost
  • HTTP vs HTTPS, and free WiFi
  • Binary: why each part stops at 255
  • Homework

Classroom line

Does anyone have a question on what we did yesterday?

1. Recap: IPv4 and the 4,294,967,296 problem

Everything in this chapter grows from one problem, so first we restate that problem.

An IPv4 address has 4 parts, each from 0 to 255, so it runs from 0.0.0.0 to 255.255.255.255.

  1. One part: 256 values (0 to 255).
  2. Two parts: 256 × 256 = 65,536.
  3. Three parts: 256 × 256 × 256 = 16,777,216.
  4. Four parts: 256 × 256 × 256 × 256 = 4,294,967,296, about 429 crore.

The world has 800+ crore people, and each person uses more than one device. So IPv4 addresses are short.

How we handled it so far.

Who gets it Why
Static IP servers clients must find the server at the same address every time; the domain name (bought from a registrar such as GoDaddy) points to it through DNS
Dynamic IP clients: phones, laptops given while you are online, taken back when you disconnect, then given to someone else

The app on your phone does not keep the server's IP inside it. It keeps a domain name, and DNS turns that name into the server's static IP.

2. Why dynamic IP alone is not enough

Even if every phone gives its IP back at night, there are still far more devices online at the same moment than 429 crore addresses. Reuse over time is not enough.

Classroom line

Dynamic IP does not completely solve the IP problem... so IPs were again split into two types: Public IP and Private IP.

So now we have two pairs of words. Static and dynamic tell you how long an address stays with a device. Public and private tell you where the address works.

3. Public IP and private IP

If every phone, laptop and TV in every home needed its own public IP, IPv4 would have finished long ago. Private IPs let a whole house share one public IP.

Two independent choices for one address

Think of two questions you can ask about an address. Can it be used on the public internet? Who decides when the device gets a different one? For example, your laptop can get a private, dynamic address from the router. Your home printer can have a private, reserved address so other people in the house can keep finding it. A single lab EC2 server can use a public, fixed address allocated to the AWS account.

Take the same example address, 192.168.0.20, in two houses. Each local network knows its own device with that address. If the two houses later join their networks through a VPN, a router may have no way to know which house you intended. Cloud engineers plan different private ranges before joining networks for exactly this reason.

Public IP Private IP
Works where on the whole internet only inside your own network (home, office, AWS VPC)
Who gives it your internet provider (ISP) your router (or AWS inside a VPC)
Unique? unique in the whole world the same numbers are reused in every home
Example 3.3.3.3 192.168.0.2, 192.168.0.3

How it looks at home.

  1. The ISP line (Jio Fiber, Airtel Fiber, or a local cable operator) comes into your home router.
  2. For this example, the ISP gives the router one public IPv4 address, say 3.3.3.3.
  3. Inside the house, the router is the gateway at 192.168.0.1.
  4. The router gives your devices private IPs: phone 192.168.0.2, second phone 192.168.0.3, laptop 192.168.0.4, TV 192.168.0.5.
  5. 192.168.0.0 is the network address. It names the whole network and is never given to a device or the router.
  6. The circle around the router and the devices is your private network.
Public IP and private IP at home Internet Google 8.8.8.8 ISP Jio Fiber / Airtel ISP line Private network 192.168.0.0/24 Home router (gateway) Public: 3.3.3.3 Private: 192.168.0.1 Phone 192.168.0.2 Phone 2 192.168.0.3 Laptop 192.168.0.4 TV 192.168.0.5 192.168.0.0 = network address, never given to a device

Figure 1. Your home: the ISP gives the router one public IP (3.3.3.3). Inside the private network the router is 192.168.0.1 and the devices are .2, .3, .4, .5.

Do devices on a private IP get internet? Yes. Your phone on 192.168.0.2 watches reels every day. A private IP cannot travel on the public internet by itself, so the router swaps it for its own public IP on the way out (this swap is called NAT) and swaps it back on the way in. We trace that swap step by step in section 5.

Ravindra Bagale's Tip

Many beginners write the router as 192.168.0.0. .0 is the network address. The router is usually 192.168.0.1 (some brands use 192.168.1.1), and devices start from .2.

4. Talking without internet: it depends on distance

Before we see how the internet works, ask a smaller question: suppose the ISP cable is cut. Can two devices still talk?

A question. Two friends sit in the same room. One wants to send a file to the other. There is no internet and no data pack. How?

  1. SMS? No. SMS needs a recharge and a mobile network.
  2. Bluetooth, mobile hotspot, or file-sharing apps like ShareIt? Yes, these work without internet.

How far each one reaches (rough typical values):

Distance What works Note
about 1 m infrared old phones and TV remotes; must point at each other
about 10 m Bluetooth file sharing, earphones
about 100 m WiFi, mobile hotspot, LAN a local network; devices get private IPs
500 m to 1 km walkie-talkie radio voice only, no files; it is not IP networking
several km your own cable copper LAN cable runs about 100 m per run; for longer, fibre cable
anywhere in the world the internet when laying your own cable is not possible

Classroom line

If you are rich, you can lay a cable even 10 kilometres long.

No internet? Distance decides Infrared ~1 m · old phones, TV remote Bluetooth ~10 m · file share, earphones WiFi / hotspot / LAN ~100 m · private IPs Walkie-talkie ~1 km · radio voice only, not IP Your own cable km · fibre for long runs Internet anywhere · mostly fibre on land and under sea

Figure 2. How far devices can talk without internet: infrared about 1 m, Bluetooth about 10 m, WiFi about 100 m, your own cable for longer, and the internet when you can't lay a cable.

So what is the internet? It is mostly cables: fibre cables on land and under the sea, joining the networks of ISPs, companies and cloud providers. The mobile tower or your WiFi is only the last wireless hop to your phone.

5. How your phone reaches Google through the router (NAT)

Your phone has a private IP, yet it gets answers from Google. Something has to translate. That something is the router, and the trick is NAT (Network Address Translation).

Inside the home, devices talk to each other directly over WiFi. Anything going outside stops at the router first. When you search "weather Pune" on Google, the packet does not go straight to Google. It goes to the router.

Step by step (suppose numbers):

  1. Your phone 192.168.0.2 searches "weather Pune". It builds a packet: From 192.168.0.2:50001, To 8.8.8.8:443 (here, suppose Google is 8.8.8.8).
  2. The packet goes to the router, 192.168.0.1. Inside the house, the router finds your phone by its MAC address; the MAC is used only inside the local network.
  3. The router writes one line in its NAT table: inside 192.168.0.2:50001 ↔ outside 3.3.3.3:40001.
  4. The router changes the From address to its public IP: From 3.3.3.3:40001, To 8.8.8.8:443.
  5. The packet travels over the internet to Google.
  6. Google answers: From 8.8.8.8:443, To 3.3.3.3:40001. Google only ever sees 3.3.3.3.
  7. The router looks up 3.3.3.3:40001 in the NAT table and finds 192.168.0.2:50001.
  8. The router changes the To address back to 192.168.0.2:50001 and hands the answer to your phone.

Two people searching at the same time. Your sister on 192.168.0.3 searches "cricket score" at the same moment. The router writes a second line: 192.168.0.3:50001 ↔ 3.3.3.3:40002. Both answers come back to the same public IP 3.3.3.3, but on different ports, so the router never mixes up your weather with her cricket score.

Inside (private IP:port) Outside (public IP:port) Search
192.168.0.2:50001 3.3.3.3:40001 weather Pune
192.168.0.3:50001 3.3.3.3:40002 cricket score
NAT: the router swaps private IP:port for publicIP:port Your phone Sister's phone 192.168.0.2 192.168.0.3 "weather Pune" "cricket score" Router · public 3.3.3.3 NAT table Inside IP:port Outside IP:port 192.168.0.2:50001 3.3.3.3:40001 192.168.0.3:50001 3.3.3.3:40002 Same public IP, different ports, so answers never get mixed MAC address used only inside the home Google 8.8.8.8:443 Out: From 192.168.0.2:50001 → router → From 3.3.3.3:40001, To 8.8.8.8:443 Back: From 8.8.8.8:443, To 3.3.3.3:40001 → table lookup → To 192.168.0.2:50001

Figure 3. The router's NAT table: each private IP:port is swapped for the public IP 3.3.3.3 with its own port. The answer from 8.8.8.8 comes back to that port and is handed to the right phone.

A packet's trip, with the IP swap Phone 192.168.0.2 From 192.168.0.2 · To 8.8.8.8 Router 3.3.3.3 swap here From 3.3.3.3 · To 8.8.8.8 Internet Google 8.8.8.8 From 8.8.8.8 · To 3.3.3.3 Router From 8.8.8.8 · To 192.168.0.2

Figure 4. Animation: the packet leaves the phone From 192.168.0.2, the router swaps it to From 3.3.3.3, it reaches 8.8.8.8, and the answer comes back the same way.

Ravindra Bagale's Tip

In interviews many candidates say "the router remembers a request ID". Say it correctly: the router keeps a NAT table that maps private IP and port to public IP and port. This same idea comes back in AWS as the NAT Gateway for private subnets.

6. Why IPv4 has not run out yet: every home reuses the same private IPs

429 crore addresses, 800+ crore people, many devices each, and still everyone is online. Here is the trick.

  1. In my home, the router has public IP 3.3.3.3, and my devices are 192.168.0.2, 192.168.0.3, …
  2. In your home, the router has public IP 5.5.5.5, and your devices are also 192.168.0.2, 192.168.0.3, …
  3. There is no clash, because private IPs never leave their own network. Only the routers' public IPs go on the internet.
  4. So crores of homes use the same 192.168.0.x numbers again and again.
  5. Mobile companies do the same at a bigger scale: many phones share one public IP (carrier-grade NAT).
Every home reuses the same private IPs Internet sees only public IPs My home Public 3.3.3.3 192.168.0.1 192.168.0.2 · 192.168.0.3 Your home Public 5.5.5.5 192.168.0.1 192.168.0.2 · 192.168.0.3

Figure 5. Two homes use the same private IPs 192.168.0.2 and .3 without any clash. On the internet only their public IPs, 3.3.3.3 and 5.5.5.5, are seen.

How you can see it yourself. On the same Wi-Fi, compare the public IPv4 seen by the same test service. Devices using the same IPv4 NAT path usually share the same outward address. A VPN, mobile data or an IPv6 result can make the comparison look different. You will test this in the homework.

How many devices fit on one home router? A home network like 192.168.0.x (in AWS terms, a /24) has 256 addresses, .0 to .255:

  1. 192.168.0.0 is the network address. Not usable.
  2. 192.168.0.255 is the broadcast address. Not usable.
  3. That leaves 254 usable addresses, .1 to .254.
  4. The router takes one (.1), so about 253 devices can join.

7. Is the router's public IP static or dynamic?

A common question: is the router's public IP static or dynamic?

  1. Most home routers get a dynamic public IP. Today it may be 3.3.3.3; after a restart or a few days, it may change.
  2. The main reason: ISPs save scarce IPv4 addresses, and dynamic is cheaper for everyone.
  3. If you need a fixed one (for example, to run a server from home), you can buy a static IP from your ISP.
  4. Being a client does not force an IP to be dynamic. Dynamic is simply the cheaper and more common choice for homes.

8. WiFi and mobile data, both on

Everyone keeps both on. Which one does the phone use?

  1. If WiFi is connected and working, the phone prefers WiFi.
  2. If the WiFi has no internet, the phone falls back to mobile data.
  3. On mobile data alone, your phone gets its address from the mobile company. Often this is also a private address behind a shared public IP (carrier-grade NAT).

9. Mobile hotspot: your phone becomes a router

When you turn on hotspot for a friend, your phone does exactly what the home router does.

  1. Your phone broadcasts its own WiFi name.
  2. It gives private IPs to the devices that connect.
  3. It sends their traffic out through its own mobile-data address, using NAT, just like the home router.
  4. If your friend searches "cricket score", the website sees your phone's public address, not your friend's private IP.

How many devices? A hotspot allows only a few devices, often about 5 to 10, set by the phone maker. A phone's CPU, battery and RAM are not built to serve many devices like a real router.

Privacy on someone's hotspot. The hotspot owner's phone carries all your traffic. On plain HTTP they could read what you send. On HTTPS the content is encrypted. We come back to this in section 20.

10. Ports: the IP reaches the server, the port reaches the app

One server can run many programs at once: a website, a database, a remote-login service. The IP only takes you to the machine. Something must say which program inside it.

A listening port and a connection are different

Picture one lab Linux server running a website on TCP 80 and SSH on TCP 22. When the web process starts, it tells the operating system where to accept incoming web connections. We call that listening. Linux uses the protocol, address and port to hand the connection to the right program. A browser can fail to open the site while SSH still works. The server is reachable, but the two connections ask for different services.

Two students can visit the same website at the same time. Both connections use destination port 80. Their devices choose source ports for their own connections, so the operating system can distinguish their traffic. TCP and UDP also have separate port spaces. When you write a cloud rule, say TCP 80 so both parts are clear.

A port is a number from 0 to 65,535 (that is 65,536 ports) that the operating system gives to a program on the network.

  1. The browser opens http://3.3.3.3/login.html (or https://instagram.com/login.html; suppose this as an example).
  2. If you typed a name, DNS first turns the name into the IP.
  3. The request reaches the server 3.3.3.3. The IP's job is done here.
  4. The port number says which program on 3.3.3.3 should get the request.

Building and flat. The IP is the building address; the port is the flat number inside the building. The courier needs both.

Ravindra Bagale's Tip

A network port is not a hardware pin on the CPU and not the USB port on your laptop. It is a logical number in the operating system (TCP or UDP). Think building = IP, flat number = port.

11. Default ports: 80 and 443

Nobody types :80 in the browser, yet it works. The browser adds it for you.

You type The browser actually uses
http://3.3.3.3 http://3.3.3.3:80
https://3.3.3.3 https://3.3.3.3:443
http://3.3.3.3:8080 port 8080, because you typed it

So http://3.3.3.3:80 and http://3.3.3.3 open the same page.

Common ports you will use in this course:

Port Program
22 SSH (remote login to the server)
80 HTTP (website)
443 HTTPS (secure website)
3306 MySQL database
9000 php-fpm (PHP behind Nginx)
Whiteboard list: http 80, https 443, ssh 22.

Whiteboard: Default ports to remember: HTTP uses 80, HTTPS uses 443 and SSH uses 22.

12. The web server: who listens on 80 and 443?

Opening port 80 is not enough. A program must be running and listening on it, or the browser gets nothing.

That program is the web server. Examples: Apache, Nginx, Tomcat, IIS, Gunicorn, Uvicorn.

How a request for login.html is served (Apache):

  1. The browser asks http://3.3.3.3/login.html.
  2. The request reaches port 80 on 3.3.3.3.
  3. Apache is listening on port 80.
  4. Apache's default web folder (document root) is /var/www/html.
  5. Apache sends back the file /var/www/html/login.html.

On Amazon Linux, Nginx's default web folder is /usr/share/nginx/html instead.

You will do this yourself in the lab:

  1. sudo yum install httpd -y
  2. sudo service httpd start
  3. Put your page at /var/www/html/login.html.
  4. Open http://<your-server-IP>/login.html in the browser.

13. SSH on port 22: managing the whole server

Visitors only need your website files. You, the admin, need the whole server: install software, create folders, read logs.

SSH stands for Secure Shell. It runs on port 22 and gives you a command line on the server from your laptop.

How the drawing works:

  1. The server 3.3.3.3 is a big box. The website is one small folder inside it, /var/www/html.
  2. Visitors come through port 80 or 443 and only reach that folder.
  3. The admin opens PowerShell on the laptop and connects with SSH on port 22.
  4. Now the admin is inside the whole server and can run commands, for example mkdir ravi.
One server, three doors (ports) IP = building address · port = flat number Visitor browser → 80 http://3.3.3.3:80 Visitor browser → 443 https://3.3.3.3:443 Admin laptop → 22 PowerShell ssh ec2-user@3.3.3.3 Server 3.3.3.3 80 / 443 /var/www/html website files (login.html) web server: Apache / Nginx 22 everything else: all files, all sites, software, settings, logs $ mkdir ravi SSH (Secure Shell) = the whole server

Figure 6. One server, 3.3.3.3. Visitors reach only the website folder through ports 80 and 443. The admin reaches the whole server through SSH on port 22.

Why SSH needs extra care.

What was hacked What is affected
The website that site and its data
SSH the whole server: every file, every site on it, every setting

That is why a hacked SSH login is the most dangerous kind. In AWS, protect it:

  1. In the security group, allow port 22 only from My IP, not from everywhere (0.0.0.0/0).
  2. Log in with a key pair, not a password.
  3. Keep the private key file safe and never share it.

Classroom line

Any question here?

14. URL syntax: a protocol is a set of strict rules

A protocol is a set of rules both sides follow exactly. One wrong character and the browser does not understand you.

Read http://3.3.3.3:80/login.html piece by piece:

  1. http is the protocol. Not fttp, not htp.
  2. :// is a colon and two slashes.
  3. 3.3.3.3 is the server's IP (or a domain name).
  4. : separates the IP from the port.
  5. 80 is the port.
  6. /login.html is the file you want.
Reading a URL piece by piece http protocol :// colon + 2 slashes 3.3.3.3 IP or domain : colon 80 port /login.html file path http://3.3.3.3:80/login.html Not fttp, not a semicolon, not a comma. One wrong character and it doesnot work.

Figure 7. Every piece of a URL has a fixed place: protocol, colon and two slashes, IP or name, colon, port, then the file path.

Handwritten URL http://3.3.3.3:80/login.html, with the protocol part bracketed and the IP address underlined.

Whiteboard: Reading a URL piece by piece: protocol (http), then ://, then the IP address (3.3.3.3), then : and the port (80), then / and the path (login.html).

Classroom line

After HTTP you must write a colon... a semicolon won't work, a comma won't work... you must write two slashes.

15. IP classes A to E

In the early internet, IPs were handed out in fixed-size blocks called classes. The first part of the IP tells you the class. You will still hear these names in interviews and at home routers.

Look only at the first part of the IP:

Class First part Network and host parts Used for
A 0 to 127 (usable 1 to 126) N.H.H.H very large networks
B 128 to 191 N.N.H.H medium networks
C 192 to 223 N.N.N.H small networks, like a home
D 224 to 239 – multicast (one sender, many receivers)
E 240 to 255 – reserved, experimental

N is the network part (same for every device in that network). H is the host part (different for each device).

  1. 0.x.x.x is reserved, and 127.x.x.x is inside class A but reserved for loopback (section 19). So class A hosts use 1 to 126.
  2. Only classes A, B and C are given to normal devices.

Worked examples:

IP First part Class
10.0.0.5 10 A
127.0.0.1 127 A range, but reserved for loopback
172.16.4.20 172 B
192.168.0.2 192 C
224.0.0.5 224 D (multicast)
The first part of the IP decides the class Class A 0 – 127 (hosts 1 – 126) 127 = loopback (127.0.0.1) Class B 128 – 191 C 192–223 D 224 – 239 · multicast E 240 – 255 · reserved Network (N) and host (H) parts A  N . H . H . H 10.0.0.5 B  N . N . H . H 172.16.4.20 C  N . N . N . H 192.168.0.2 Today: CIDR /8, /16, /24 (AWS VPC uses CIDR)

Figure 8. The first part of the IP decides the class: A 0 to 127 (127 is loopback), B 128 to 191, C 192 to 223, D 224 to 239 multicast, E 240 to 255 reserved.

How it is done today: CIDR. Classes are history. Modern networks, and AWS VPC, use CIDR: a slash number says how many bits are the network part. /8 is like class A, /16 like class B, /24 like class C, but you can choose any size, for example /20.

16. How many devices fit in a network

When you make a network in AWS you choose its size. So you must know how many devices each size holds.

Count the addresses that an AWS subnet can use

Modern network ranges use a slash such as /24, called a CIDR prefix. IPv4 has 32 bits. If 24 bits name the network, eight bits remain, giving 2^8 = 256 addresses in the block. A /28 leaves four bits, so its block has 2^4 = 16 addresses.

AWS reserves the first four and the last IPv4 addresses in each normal VPC subnet. For 10.0.2.0/28, the whole block runs from 10.0.2.0 to 10.0.2.15. AWS reserves .0 through .3 and .15. The usable addresses are .4 through .14, which is 11 addresses. Keep two numbers in your notebook: the block has 16 addresses and EC2 can use 11 of them. This avoids carrying the home-network subtraction rule into AWS.

  1. A home router uses a class C style network, 192.168.0.x. Only the last part changes, 0 to 255: 256 addresses, 254 usable (.0 network, .255 broadcast).
  2. A class B style network changes the last two parts: 256 × 256 = 65,536 addresses.
  3. A home router is not built for that many devices. Big offices and malls use enterprise network equipment.
  4. In AWS, a /24 subnet has 256 addresses, and AWS keeps 5 of them in every subnet, so 251 are usable for your servers.
Network Addresses Usable
192.168.0.0/24 (home) 256 254 (253 devices plus the router)
AWS subnet /24 256 251 (AWS keeps 5)
/16 (class B style) 65,536 65,534

17. Private IP ranges, and why cloud work is mostly private

You must recognise a private IP on sight, because in AWS almost every server you build will have one.

These three ranges are private (RFC 1918). Everything else, except the reserved blocks, is public and handed out through ISPs and cloud providers.

Range Class style Example
10.0.0.0 to 10.255.255.255 A 10.0.1.25
172.16.0.0 to 172.31.255.255 B 172.31.5.10
192.168.0.0 to 192.168.255.255 C 192.168.0.2

Watch out: 172.15.0.1 and 172.32.0.1 are not private. Only 172.16 to 172.31 is.

Classroom line

Our whole working life goes in private IPs... most of our work stays in private IPs.

How a big app uses them. Suppose an app like Instagram runs on 100,000 servers (a "suppose" number to learn with):

  1. Only the front-door servers and load balancers face the internet with public IPs.
  2. The app servers and databases talk to each other on private IPs.
  3. In AWS this becomes a VPC with public subnets (front door) and private subnets (everything else).

18. Who hands out IP addresses?

If every country or company just picked any IP, two machines could get the same address and the internet would break.

Classroom line

These IPs don't belong to any one country... they are the whole world's property.

The chain:

  1. IANA manages the whole IP address space for the world.
  2. IANA gives large blocks to five regional registries. For India and the Asia-Pacific region it is APNIC.
  3. APNIC gives blocks to ISPs and cloud providers (Jio, Airtel, AWS and others).
  4. The ISP gives one IP from its block to your router or phone.
  5. IPv4 has run out at the registries, so IPv4 blocks are now also bought and sold between companies on a transfer market.

Does an IP tell your location? The numbers inside an IP do not mean ISP, state or district. But because registries record which ISP got which block, location services map whole blocks to a rough city and ISP. So your public IP can reveal roughly where you are and which ISP you use.

19. Loopback and localhost: 127.0.0.1

On one server, programs often need to talk to each other: the web server to PHP, PHP to the database. Sending that out to the internet and back would be slow and unsafe.

Test the service from the right machine

On the EC2 server, localhost means the EC2 server. In a terminal on your laptop, localhost means the laptop. If the website opens from curl http://127.0.0.1/ in SSH but you cannot open the public address from your browser, you have proved that the web process answered locally. Now check which address the program listens on, then its port, the instance’s security group and the public route. If the local request fails too, start with the web service itself.

Read the two prompts before doing the test. One is your laptop and the other is inside EC2. Label your results with the machine and address you used so you can repeat the same check later.

The whole range 127.0.0.0 to 127.255.255.255 is loopback: traffic sent there never leaves the machine. 127.0.0.1 is the one everyone uses, and localhost is its name.

How it works on one server:

  1. A visitor reaches Nginx on port 80.
  2. Nginx passes PHP work to php-fpm at 127.0.0.1:9000. (On Amazon Linux php-fpm may use a socket file instead of port 9000; the idea is the same.)
  3. php-fpm asks MySQL at 127.0.0.1:3306.
  4. All of this stays inside the server. Same IP, different ports.
  5. You could use 127.0.0.2 or 127.0.0.3, but normally everyone uses 127.0.0.1 and separates programs by port.
Loopback: programs on one server talk on 127.0.0.1 Visitor port 80 Server 3.3.3.3 · localhost = 127.0.0.1 Nginx :80 127.0.0.1:9000 php-fpm :9000 127.0.0.1:3306 MySQL :3306 never leaves the machine · same IP, different ports

Figure 9. Inside one server, Nginx (80), php-fpm (9000) and MySQL (3306) talk to each other on 127.0.0.1. That traffic never leaves the machine.

Try it on your server: curl http://localhost shows the page your web server is hosting, without going through the internet.

20. HTTP vs HTTPS: what others can see

On someone's hotspot or a cafe's WiFi, every packet of yours passes through their device. What they can read depends on HTTP or HTTPS.

Have the browser verify the site before sending a login

HTTPS uses TLS, the protocol that sets up an encrypted connection. A website certificate names the host it is valid for. The browser checks that name and whether the certificate is trusted. If the lab service has a certificate for shop.example.com and you enter the raw IP, the browser can see a name mismatch even though the server and port are correct. If a warning appears, stop and check the name and certificate before entering a password.

After the secure connection is established, details such as the HTTP request path and the submitted form are protected in transit. The network can still observe where traffic goes at the IP level and when it moves. In the browser, compare the addresses of a test page loaded over HTTP and HTTPS. Use a public practice page or your own lab and keep real passwords out of the exercise.

HTTP HTTPS
Data travels as plain text encrypted
Port 80 443
Needs nothing extra an SSL/TLS certificate on the server
WiFi owner can read your password? yes no, only scrambled data

Example. You log in to a site on cafe WiFi.

  1. On http://example.com, your username and password travel as plain text. The WiFi owner could read them.
  2. On https://example.com, the same login travels encrypted. The WiFi owner sees only scrambled data.
  3. Payment and shopping apps (PhonePe, Paytm, Flipkart, Amazon) use HTTPS, so public WiFi is far safer than it used to be. Still, be careful.
Cafe WiFi: HTTP vs HTTPS http://example.com (port 80) Laptop Cafe WiFi owner watching user=ravi  password=Ravi@123 ← readable Website https://example.com (port 443) Laptop SSL/TLS Cafe WiFi owner watching x9#Lq!7v%Tz@2k ← scrambled; owner sees only "example.com" Website

Figure 10. Animation: on HTTP the password crosses the cafe WiFi as readable text; on HTTPS it crosses as scrambled data, and the WiFi owner sees only the site name.

21. Free WiFi in the mall: who is paying?

Classroom line

Nobody buys anyone tea or vada pav for free, so who gives free WiFi without any benefit?

What the free WiFi owner can see, even with HTTPS:

  1. The From and To IPs of every packet, because the router needs them to deliver it.
  2. Which sites you visit, mainly from your DNS lookups and from the site name your browser sends at the start of an HTTPS connection.
  3. When and how much you use.

What they cannot see with HTTPS: your messages, passwords and page content.

An IP alone does not always tell the app, because many sites share the same big content-delivery servers. The lesson stays: free WiFi can see which sites, not what you send.

22. See your own phone's traffic

Seeing your own packets makes all of this chapter's ideas real.

Packet-capture apps on Android (for example "PCAPdroid" or older "Packet Capture" apps) create a local VPN on your phone and show your own phone's connections: which apps talk to which servers, and on which ports.

  1. Install a packet-capture app from the Play Store on your own phone.
  2. Start a capture.
  3. Open a few apps.
  4. See which IPs and ports each app uses, and which connections are HTTP or HTTPS.

Use it only on your own phone. Watching other people's traffic without their permission is not okay.

23. Why each part of an IP stops at 255: binary

So far we have said "no part goes above 255". Now we see why.

Humans count in decimal (0 to 9). Machines understand only binary: 0 and 1. So every IP is stored in binary.

The question: how many zeros should each part use? 4, 8, 16 or 32? The answer is 8 bits (8 binary digits) for each part. 8 bits = 1 byte.

  1. 0.0.0.0 in binary is 00000000.00000000.00000000.00000000.
  2. The next IP, 0.0.0.1, is 00000000.00000000.00000000.00000001.
  3. An IPv4 address is 4 parts × 8 bits = 32 bits.

Classroom line

This is the quality of our studying... this all happens because of rote learning. If you understand it, this never happens.

Counting in binary, 0 to 15:

Decimal Binary Decimal Binary
0 0 8 1000
1 1 9 1001
2 10 10 1010
3 11 11 1011
4 100 12 1100
5 101 13 1101
6 110 14 1110
7 111 15 1111
Two handwritten columns: decimal 0 to 8 with binary 0 to 1000, and decimal 9 to 15 with binary 1001 to 1111.

Whiteboard: Counting in binary: decimal 0 to 15 next to their binary forms, from 0, 1, 10, 11 up to 1111. Four bits give 16 values, 0 to 15.

The biggest 8-bit number. Fill all 8 bits with 1: 11111111. Each place has a value, starting from the right: 1, 2, 4, 8, 16, 32, 64, 128.

128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255

Eight vertical strokes (bits set to 1) on a line, with place values 128 64 32 16 8 4 2 1 written above them.

Whiteboard: One IP part is 8 bits, with place values 128, 64, 32, 16, 8, 4, 2, 1. Set every bit to 1 and add them: 128+64+32+16+8+4+2+1 = 255. That is why each part stops at 255.

8 bits, all 1 = 255 128 1 64 1 32 1 16 1 8 1 4 1 2 1 1 1 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255 one part = 8 bits = 1 byte · IPv4 = 4 × 8 = 32 bits 0.0.0.1 = 00000000.00000000.00000000.00000001 256 = 1 00000000 ← needs a 9th bit, so never in an IP

Figure 11. Animation: eight bits, place values 128 to 1. When all eight are 1, they add up to 255. 256 would need a 9th bit.

So:

  1. One part has 8 bits, so it can hold 0 (00000000) to 255 (11111111).
  2. 256 in binary is 100000000. That needs a 9th bit, which an IPv4 part does not have.
  3. That is why 256.0.7.8 is never an IP address.

24. Homework: try at home

Try at home

Test 1: many devices, one public IP

  1. Turn on the mobile hotspot on your phone.
  2. Ask 2 or 3 friends to connect to it.
  3. Everyone opens Google and searches "my IP".
  4. Compare the public IPv4 results using the same test service. On the same translated IPv4 path the outward address can be shared. Check for IPv6, a VPN or a separate cellular path if the answers differ.
  5. That is NAT: private IPs behind one public IP.

Test 2: find your own private IP

  1. On a Windows laptop, open Command Prompt and run ipconfig.
  2. Look for IPv4 Address (for example 192.168.0.4) and Default Gateway (for example 192.168.0.1).
  3. On Linux, run ip addr and look at the line starting with inet.
  4. Check: does your IP fall in one of the three private ranges?
  5. Now search "my IP" on Google. The public IP you see is different from your private IP.

Test 3 (optional): localhost

  1. If a web server is running on your laptop or server, open http://localhost.
  2. On your AWS server you can run curl http://localhost.
  3. The page comes from the same machine, through 127.0.0.1.

Ravindra Bagale's Tip

Many beginners search "my IP" and write down the 192.168.x.x address from ipconfig as their public IP. They are two different things: ipconfig shows the private IP your router gave you; Google shows the public IP of your router.

Common errors and fixes

Error / symptomFix
I put a private IP in the browser from outsidePrivate ranges (10.x, 172.16–31.x, 192.168.x) are not reachable from the internet. Use the instance public IP or a VPN/bastion.
SSH times out but ping worksPort 22 is closed in the Security Group, or you are using the wrong IP/user.
Two apps on the same server fightThey need different ports (or different hostnames). Only one process can listen on a given IP:port.
I opened port 80 but https failsHTTPS is port 443. Open 443 too, or stay on http for practice.
Class A/B/C confusionFor modern work, think CIDR and private ranges first; classful names are history for interviews.

FAQ

Q. Why do we still use IPv4?

NAT and private ranges let many devices share few public addresses. IPv6 exists, but most labs still start on IPv4.

Q. What is localhost / 127.0.0.1?

It always means this same machine. Traffic never leaves the computer.

Q. Public IP vs Elastic IP?

Public IP can change on stop/start. Elastic IP is a static public IPv4 you allocate in AWS.

Q. Which ports must I remember?

22 SSH, 80 HTTP, 443 HTTPS, 3306 MySQL/MariaDB are the ones you meet first.

Recap

In short

  1. IPv4 has 4,294,967,296 addresses, about 429 crore, for 800+ crore people. Dynamic IP alone does not solve it.
  2. Public IPs work on the internet; private IPs work only inside your network and are reused in every home.
  3. Your router is 192.168.0.1, devices start at .2, and .0 is the network address.
  4. The router does NAT: private IP:port ↔ public IP:port, so private devices still reach the internet.
  5. A /24 home network has 254 usable addresses; with the router, about 253 devices.
  6. Without internet, distance decides: infrared, Bluetooth, WiFi, your own cable. The internet itself is mostly fibre.
  7. The IP reaches the server; the port (0 to 65,535) reaches the program. 80 HTTP, 443 HTTPS, 22 SSH, 3306 MySQL, 9000 php-fpm.
  8. SSH is Secure Shell; a hacked SSH login exposes the whole server.
  9. Classes: A, B, C for hosts, D multicast, E reserved. Today AWS uses CIDR (/16, /24).
  10. Private ranges: 10.x, 172.16 to 172.31, 192.168.x. IANA → APNIC → ISPs hand out IPs.
  11. 127.0.0.1 (localhost) never leaves the machine.
  12. HTTPS hides content, not which sites you visit. Free WiFi is never fully free.
  13. 8 bits of 1 = 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255. That is why no part goes above 255.

Samjla ka? Do the hotspot "my IP" test tonight. Kalel tumhala, halu halu kalel. In the next chapter we go deeper.


Ravindra Bagale, trainer: linkedin.com/in/ravindra-bagale. All IPs on this page are dummy numbers for learning. Instagram on 100,000 servers is a "suppose" example, not a fact about Instagram.

Worked example: two homes can have the same private address

Picture two students with the same roll number in different classes. The number works when you know which classroom you are in. A private address works the same way inside a network.

  1. Write down two homes, each with a laptop at 192.168.1.20. Each laptop can browse because its own router forwards and translates traffic for it.
  2. Now suppose one router shows 100.72.4.9 as its WAN IP. That falls inside the shared 100.64.0.0/10 range. The ISP can be translating again between the router and the public internet. This is carrier-grade NAT.
  3. Compare the router's WAN address with the address reported by a trusted IP-check service. A mismatch suggests an upstream translation or proxy. Check with your ISP before expecting inbound access.
Interview question: is every non-private address publicly reachable?

No. Some ranges have special purposes, and routing and firewall rules still decide reachability. In this example the ISP shared range is separate from the three private ranges you learned in this chapter.

Reference 1 · Reference 2

Practise this chapter

Interview questions and answers

Why can both a private address and a public address be dynamic?

Dynamic describes how the address is assigned and when it might change. Private describes a reserved address range used inside networks. A home router can lease a private address to your phone; the provider can also assign a changeable public address on the outward path.

SSH opens but the website times out. Does SSH prove the web port is open?

SSH proves its own service and network path work on its port, typically TCP 22. Check whether the web process listens on the intended interface and port. Then check the rule for that web port and whether local HTTP works.

Check your understanding

Choose an answer, then compare your reasoning with the explanation.

0 of 3 answered correctly
1. Both your phone and your laptop open a website which sees one public IPv4 address. What lets the home router send each reply back to the right device?

Answer and explanation

A. A translation mapping for each connection

The router tracks the original local address and port and the translated address and port. It uses that mapping to forward each reply.

2. An AWS IPv4 subnet is 10.0.2.0/28. How many addresses can normally be assigned to resources?

Answer and explanation

D. 11

A /28 leaves four bits, giving 16 addresses. AWS reserves the first four and the last one, leaving 11.

3. A local web page works on EC2 at http://127.0.0.1:80/, but a laptop cannot open the instance’s public address. What should you check next?

Answer and explanation

A. The listener address and outside network path to TCP 80

The local request proved the service answered on loopback. The public request still needs a listener on the appropriate interface and an allowed network path.

Apply the concepts

  1. Imagine two devices share one home Wi-Fi connection. Sketch their two private addresses and the public-facing side of the router.

    Check your result

    The sketch uses two different local addresses and shows how the public side can be shared. You can explain why replies still reach the right device.

  2. In your notebook classify 10.20.0.3, 172.20.0.4 and 172.32.0.4 against the private IPv4 ranges from the lesson.

    Check your result

    The first two are RFC 1918 private addresses. 172.32.0.4 is outside the RFC 1918 172.16–172.31 block.

  3. On your lab host, suppose the local website gives 200 but the public request times out. Explain what you have already verified and what you inspect next.

    Check your result

    You identify a working local listener and then check public addressing, listening interfaces and network permissions for the chosen port.

Practice Tasks

Practice checks run in your browser. They check the answer for this exercise; they do not create resources or connect to your AWS account.

About this scenario

London Legal LLP is a law business in London. Oliver Hughes looks after its IT and cloud work. They count money in GBP and run their AWS resources in the eu-west-2 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 12

Easy How many addresses in a /16?

Picking a VPC size needs this count.

London Legal LLP plans the VPC 10.49.0.0/16. How many IP addresses does it hold in total?

  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Easy Create a VPC

A VPC is your own private network in AWS. Everything else lives inside it.

Create a VPC with the CIDR block 10.5.0.0/16.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium First usable IP for a server

The first 4 addresses are reserved by AWS.

What is the first IP address a server can get in 10.49.20.0/24 on AWS?

  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Do these VPCs overlap?

Peering fails when ranges overlap.

Can VPC 10.49.0.0/16 peer with VPC 10.49.128.0/17? Answer yes or no.

  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Find the VPC router address

AWS reserves base+1 for the router.

Which address does AWS reserve for the VPC router in subnet 10.49.4.0/24?

  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Usable IPs in an AWS /24 subnet

AWS keeps 5 addresses in every subnet.

How many addresses can London Legal LLP use for servers in subnet 10.49.1.0/24 on AWS?

  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Usable IPs in a /28

/28 is the smallest AWS subnet.

Subnet 10.49.9.0/28 on AWS. How many usable IPs?

  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Create a subnet in one AZ

Subnets split the VPC range and each one lives in exactly one AZ.

In vpc-05432ea314deadbee, create subnet 10.5.1.0/24 in eu-west-2a.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium CloudFormation: web security group

Opening only 443 keeps the server safer.

Template with an AWS::EC2::SecurityGroup that allows TCP 443 from 0.0.0.0/0. Add a GroupDescription.

  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Fill in each property with the value from the task.
  4. Click Check answer. Key order does not matter.

Hard Range of a /26

Knowing the range stops subnets from overlapping.

Write the first and last address of 10.49.3.64/26 (like a.b.c.d - e.f.g.h).

  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Hard Pick the smallest prefix

Right-sizing saves address space.

A subnet must hold 100 servers on AWS. What is the largest prefix number (smallest subnet) that fits? Answer like /25.

  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Hard Split a /24 into two

Public and private subnets often split one block.

Split 10.49.8.0/24 into two equal subnets. Write both CIDRs separated by a comma.

  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Quick revision cards

Revise this lesson in two minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens its lesson.

Comparison card: HTTP vs HTTPS. HTTP: Web protocol on port 80. Data travels as plain text. HTTPS: HTTP with TLS encryption, on port 443.
Comparison: HTTP vs HTTPS · Download PNG
Revision card: Public vs private IP. There are not enough IPv4 addresses for every device to have a public one. Public IPs work on the internet. Private IPs work only inside a network and are reused everywhere. Your router uses NAT: many private devices share one public IP. Laptop 192.168.1.5 goes out as router 49.36.10.4.
Revision card: Public vs private IP · Download PNG
Revision card: Private IP ranges. VPCs and home networks must use addresses that never clash with the internet. RFC 1918 reserves three private ranges. Pick one for your VPC CIDR; 10.0.0.0/16 is common. The three:
Revision card: Private IP ranges · Download PNG
Revision card: Loopback: 127.0.0.1. You need to test a server from itself without any network. 127.0.0.1 (localhost) always means "this machine". curl localhost on the server checks the web server, skipping SGs. Test:
Revision card: Loopback: 127.0.0.1 · Download PNG
Revision card: IP classes and CIDR. Networks need a way to say which part of an IP is the network. Old classes: A /8, B /16, C /24. CIDR lets any prefix like /20. Hosts = 2^(32 - prefix). /24 = 256 IPs; /20 = 4096.
Revision card: IP classes and CIDR · Download PNG
Comparison: Public IP vs Private IP. Public IP: A unique address on the internet. Private IP: An address that works only inside one network.
Comparison: Public IP vs Private IP · Download PNG
Interview card: How do many home devices share one public IP?. The router does NAT: it swaps private IPs for its public IP and tracks each connection. This is why IPv4 has not run out yet. 3 phones + laptop › one IP 49.36.10.4.
Interview card: How do many home devices share one public IP? · Download PNG
Interview card: What is 127.0.0.1?. The loopback address: it always means this same machine. Traffic never leaves the computer, so it tests services alone. curl localhost on EC2 bypasses the SG.
Interview card: What is 127.0.0.1? · Download PNG
Interview card: How many hosts fit in a /26 network?. 64 addresses: 2^(32-26). Usable is fewer: 62 normally, 59 in an AWS subnet. 10.0.0.0/26 = 10.0.0.0 to 10.0.0.63.
Interview card: How many hosts fit in a /26 network? · Download PNG
Interview card: Is free public Wi-Fi safe for logging in?. Only on HTTPS sites; HTTP traffic can be read by others on the network. HTTPS encrypts between you and the site. Check the lock icon and https:// before typing a password.
Interview card: Is free public Wi-Fi safe for logging in? · Download PNG

More cards: Ravi Sir's Cards.