Aprelius logo
uptime: 00:00:00
Network

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:

PrefixMaskTypical use
/8255.0.0.0Very large network
/16255.255.0.0Large private network
/24255.255.255.0256-address IPv4 subnet
/25255.255.255.128128-address IPv4 subnet (half of a /24)
/30255.255.255.252Four 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/8
  • 172.16.0.0/12
  • 192.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:

  1. Discover - the client broadcasts DHCPDISCOVER because it does not have an address yet.
  2. Offer - a server proposes an address in DHCPOFFER.
  3. Request - the client selects an offer with DHCPREQUEST.
  4. 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:

text
public IP:port <-> private IP:port

Example:

Private connectionPublic mappingDestination
192.168.1.10:5151497.126.15.199:40001142.250.186.78:443
192.168.1.20:5151597.126.15.199:40002104.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.

References