Batch 5: Protocols & Security Networking Apache & NGINX Cloud ← Full TOC

← Networking Index

🔢 IP Addressing & Subnetting — IPv4, IPv6, CIDR, subnetting, NAT, private ranges

IP addressing is the foundation of all network communication. IPv4 uses 32-bit addresses, IPv6 uses 128-bit. CIDR notation specifies network size. Subnetting divides large networks into smaller segments for security and performance.

📋 Reference

ItemSyntax / ValueDescription
IPv432-bit address: 192.168.1.100Dotted decimal notation — 4 octets
IPv4 classesA: 1-126, B: 128-191, C: 192-223Historical classful addressing — now replaced by CIDR
Private IPv410.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16RFC 1918 — not routed on public internet
127.0.0.0/8127.0.0.1 = localhostLoopback range — never leaves host
169.254.0.0/16APIPA — auto-configured when no DHCPLink-local — also AWS EC2 metadata endpoint
CIDRClassless Inter-Domain Routing: 192.168.1.0/24Slash notation specifies network prefix length
Subnet mask/24 = 255.255.255.0 = 16,777,216 hostsDefines which part is network vs host
/8/8 = 255.0.0.0 — 16M hostsLarge /8 block — class A
/16/16 = 255.255.0.0 — 65,534 hostsMedium /16 block — class B
/24/24 = 255.255.255.0 — 254 hostsCommon subnet — class C
/25/25 — 126 hosts — splits /24 in halfHalf a /24
/28/28 — 14 hosts — 16 IPs, 14 usableSmall subnet — AWS security groups default
/30/30 — 2 hosts — point-to-point linksMinimal subnet for router links
/32/32 — single hostSingle IP address — host route
Network addressFirst IP in subnet — 192.168.1.0 in /24Not assignable to a host
BroadcastLast IP in subnet — 192.168.1.255 in /24Sends to all hosts in subnet
Usable hosts2^(32-prefix) - 2Subtract network and broadcast addresses
NATNetwork Address TranslationMultiple private IPs share one public IP
PATPort Address Translation (NAPT)Most common NAT — maps port numbers too
IPv6128-bit address: 2001:db8::1Colon-hex notation — :: compresses zeros
IPv6 typesGlobal unicast, link-local fe80::/10, loopback ::1Public, local, loopback addresses
IPv6 notation2001:0db8:0000:0000:0000:0000:0000:0001 = 2001:db8::1Compress leading zeros and consecutive zero groups
Dual stackSystem has both IPv4 and IPv6 addressesTransition strategy — most modern systems
CIDR aggregation10.0.1.0/24 + 10.0.2.0/24 = 10.0.0.0/22Summarize routes — reduces routing table size

💡 Example

# IP Addressing and Subnetting Reference

# ── CIDR Cheat Sheet ──────────────────────────────────────────
# Prefix  Mask              Hosts    Subnets from /24
# /24     255.255.255.0     254      1
# /25     255.255.255.128   126      2
# /26     255.255.255.192   62       4
# /27     255.255.255.224   30       8
# /28     255.255.255.240   14       16
# /29     255.255.255.248   6        32
# /30     255.255.255.252   2        64
# /32     255.255.255.255   1 host   -

# ── Subnet Calculator (Python) ────────────────────────────────
import ipaddress

def subnet_info(cidr: str):
    net = ipaddress.IPv4Network(cidr, strict=False)
    print(f"CIDR:        {net}")
    print(f"Network:     {net.network_address}")
    print(f"Broadcast:   {net.broadcast_address}")
    print(f"Netmask:     {net.netmask}")
    print(f"Wildcard:    {net.hostmask}")
    print(f"Hosts:       {net.num_addresses - 2} usable")
    print(f"First host:  {list(net.hosts())[0]}")
    print(f"Last host:   {list(net.hosts())[-1]}")
    print(f"Prefix len:  /{net.prefixlen}")
    return net

print("=== 192.168.1.0/24 ===")
net = subnet_info("192.168.1.0/24")

# Check if IP is in subnet
ip = ipaddress.IPv4Address("192.168.1.100")
print(f"\n{ip} in {net}: {ip in net}")
print(f"8.8.8.8   in {net}: {ipaddress.IPv4Address('8.8.8.8') in net}")

# Divide /24 into subnets
print("\n=== Split 192.168.1.0/24 into /26 subnets ===")
for i, subnet in enumerate(net.subnets(new_prefix=26)):
    hosts = list(subnet.hosts())
    print(f"  Subnet {i+1}: {subnet}  first={hosts[0]}  last={hosts[-1]}")

# Private address check
for addr in ["10.0.0.1","172.16.5.1","192.168.1.1","8.8.8.8","203.0.113.1"]:
    ip = ipaddress.IPv4Address(addr)
    print(f"  {addr:15} private={ip.is_private}  loopback={ip.is_loopback}")

# IPv6
print("\n=== IPv6 ===")
ipv6 = ipaddress.IPv6Network("2001:db8::/32")
print(f"IPv6 network: {ipv6}")
print(f"Addresses:    {ipv6.num_addresses:,}")
print(f"Link-local:   {ipaddress.IPv6Address('fe80::1').is_link_local}")
print(f"Loopback:     {ipaddress.IPv6Address('::1').is_loopback}")

# CIDR aggregation (supernetting)
nets = [ipaddress.IPv4Network(n) for n in ["10.0.0.0/24","10.0.1.0/24","10.0.2.0/24","10.0.3.0/24"]]
supernet = ipaddress.collapse_addresses(nets)
print("\nAggregated routes:", list(supernet))
# python3 ipaddr.py

← OSI Model & TCP/IP  |  🏠 Index  |  DNS & Name Resolution →