Network Addressing, DHCP, and NAT
IP addresses and subnets
An IPv4 address identifies a network interface. A subnet prefix separates the network portion from
the host portion. In 192.168.1.10/24, /24 means that the first 24 bits identify the network. The
equivalent subnet mask is 255.255.255.0.
Prefixes examples:
| Prefix | Mask | Typical use |
|---|---|---|
/8 | 255.0.0.0 | Very large network |
/16 | 255.255.0.0 | Large private network |
/24 | 255.255.255.0 | 256-address IPv4 subnet |
/25 | 255.255.255.128 | 128-address IPv4 subnet (half of a /24) |
/30 | 255.255.255.252 | Four addresses, usually two usable hosts |
For a normal IPv4 subnet, the first address identifies the network and the last is the broadcast
address. The remaining addresses can be assigned to hosts. A /24 therefore has 254 usable host
addresses, not 256.
RFC 1918 reserves these private ranges:
10.0.0.0/8172.16.0.0/12192.168.0.0/16
Private addresses are used inside local networks and are not routed directly across the public Internet.
Static addressing
DHCP is not required, addresses can be configured by hand on each host: IP, subnet prefix, default gateway, and DNS servers, set directly in the OS network settings instead of being leased. This is common for servers, routers, and printers that need a stable, predictable address.
Without DHCP handing out addresses automatically, the network administrator has to track address assignments manually (usually a spreadsheet or IPAM tool) to avoid two hosts claiming the same IP, which causes a conflict. Static configuration also means moving a device to another subnet requires reconfiguring it by hand, and there's no automatic lease renewal or expiry to reclaim unused addresses.
DHCP
The Dynamic Host Configuration Protocol gives a host the information it needs to use a network: an IP address, subnet prefix, default gateway, and usually DNS servers. It uses UDP. Servers listen on port 67 and clients use port 68.
The initial exchange is commonly remembered as DORA:
- Discover - the client broadcasts
DHCPDISCOVERbecause it does not have an address yet. - Offer - a server proposes an address in
DHCPOFFER. - Request - the client selects an offer with
DHCPREQUEST. - Acknowledge - the server confirms the lease with
DHCPACK.
The first messages use 0.0.0.0 as the client's source address and 255.255.255.255 as the
broadcast destination. The lease later has to be renewed before it expires.
NAT
Network Address Translation lets many private hosts share a public IPv4 address. A home router rewrites the source address and port of outgoing connections, then records the mapping in a table:
public IP:port <-> private IP:portExample:
| Private connection | Public mapping | Destination |
|---|---|---|
192.168.1.10:51514 | 97.126.15.199:40001 | 142.250.186.78:443 |
192.168.1.20:51515 | 97.126.15.199:40002 | 104.244.42.129:443 |
The different public source ports let the router distinguish simultaneous connections. A port is 16
bits wide, so the range is 0 through 65535; port 0 is reserved, leaving 65535 commonly usable
values.
The private-side source ports (51514, 51515 above) are ephemeral ports - temporary ports the
OS picks from a high range for the lifetime of one outgoing connection, then frees for reuse. They
are separate from well-known ports like 67/68 (DHCP) or 443 (HTTPS), which a service listens
on at a fixed number.
NAT is primarily an address-conservation and connection-tracking mechanism. It is not a substitute for a firewall, although the state table often prevents unsolicited inbound connections by default.