Network Fundamentals · Topic 2 of 6
IP Addresses, Subnets, Routes and Gateways
Learn IPv4 and IPv6 addresses, subnet masks and prefix lengths, routing tables and default gateways, and how a host decides where to send a packet.
- 5 min
- An IPv4 address has 32 bits and an IPv6 address has 128 bits.
- The mask or prefix length marks the network part of an address.
- A /24 has 254 usable host addresses.
What it is
An IP address identifies a device at Layer 3, and routes tell routers and hosts how to reach other networks.
The subnet mask or prefix length splits an address into a network part and a host part. A default gateway is the router that a host uses to leave its own network.
Every API call, SSH session and web request depends on these basics, so troubleshooting starts here.
How it works
A host compares the destination to its own network, and a router compares it to its routing table.
IPv4 addresses
An IPv4 address has 32 bits, written as four decimal numbers called octets, for example 192.168.10.5. Each octet runs from 0 to 255.
The subnet mask shows which bits are the network. A prefix length writes the same information as a count of bits.
| Prefix | Mask | Hosts per subnet |
|---|---|---|
| /24 | 255.255.255.0 | 254 |
| /25 | 255.255.255.128 | 126 |
| /26 | 255.255.255.192 | 62 |
| /30 | 255.255.255.252 | 2 |
The first address in a subnet is the network address, and the last is the broadcast address. The ones in between are usable for hosts.
For example, with 192.168.10.5/24:
- The network address is
192.168.10.0. - The broadcast address is
192.168.10.255. - The usable hosts run from
192.168.10.1to192.168.10.254.
Private addresses
Private IPv4 ranges are not routed on the internet. They are 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. NAT lets such hosts reach the internet through a public address.
Two special ranges are worth knowing. 127.0.0.0/8 is loopback, and 127.0.0.1 means this device. 169.254.0.0/16 is link local, and a host that cannot reach a DHCP server may give itself an address from it.
IPv6 addresses
An IPv6 address has 128 bits, written as eight groups of four hexadecimal digits, for example 2001:db8::1. Leading zeros can be dropped, and one run of zero groups can be replaced with a double colon.
IPv6 uses prefix lengths only, and a typical subnet is a /64. The address ::1 is loopback.
The default gateway
A host first checks whether the destination is in its own subnet.
- If it is, the host sends the frame directly to that device's MAC address.
- If not, the host sends the packet to its default gateway, and the router carries it onward.
If the gateway is missing or wrong, the host reaches local devices but nothing beyond them.
Routing tables
A router looks up the destination address in its routing table and picks the entry with the longest matching prefix. That is the most specific route.
R1# show ip route
S* 0.0.0.0/0 [1/0] via 192.0.2.2
C 192.168.10.0/24 is directly connected, GigabitEthernet0/0
C 192.168.20.0/24 is directly connected, GigabitEthernet0/1
C 192.0.2.0/24 is directly connected, GigabitEthernet0/2
S 10.50.0.0/16 [1/0] via 192.168.20.2
This IOS XE output shows connected routes marked C, static routes marked S, and a default route 0.0.0.0/0. A packet for 10.50.1.9 matches 10.50.0.0/16, and it is sent to 192.168.20.2. A packet for any other address uses the default route.
Routes come from three sources: directly connected networks, static routes that an administrator enters, and routing protocols such as OSPF and BGP.
R1(config)# ip route 10.50.0.0 255.255.0.0 192.168.20.2
R1(config)# ip route 0.0.0.0 0.0.0.0 192.0.2.2
The first command adds a static route to 10.50.0.0/16. The second sets a default route.
Checking a host
A Linux host can show its addresses and routes.
$ ip address show
$ ip route show
default via 192.168.10.1 dev eth0
192.168.10.0/24 dev eth0 proto kernel scope link src 192.168.10.5
The line starting with default is the default gateway. Windows has ipconfig and macOS has ifconfig for the addresses.
In automation
Scripts often need to work with subnets. Python's standard ipaddress module checks and calculates them.
import ipaddress
net = ipaddress.ip_network("192.168.10.0/24")
print(net.num_addresses)
print(ipaddress.ip_address("192.168.10.5") in net)
The first line prints 256, and the second prints True.
A host sends to the gateway when the destination is not in its own subnet. A router chooses the route with the longest prefix.
What to remember for the exam
Seven facts cover this topic.
- An IPv4 address has 32 bits and an IPv6 address has 128 bits.
- The mask or prefix length marks the network part of an address.
- A /24 has 254 usable host addresses.
- The private IPv4 ranges are 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16.
- A default gateway is the router that a host uses for destinations outside its subnet.
- A router picks the longest matching prefix in its routing table.
- The default route is 0.0.0.0/0.
Common mistakes
Four mistakes are typical.
Using the wrong gateway. The host reaches the local network but nothing else.
Counting the network and broadcast addresses as hosts. A /24 has 254 usable hosts, not 256.
Picking the shortest prefix. Routers prefer the most specific route.
Mixing up the mask and the gateway. The mask sizes the subnet, and the gateway is a router address.
A gateway must be in the same subnet as the host. If it is not, the host cannot reach it.
Check yourself
Work through these three questions.
How many usable host addresses does a /24 subnet have?
It has 254, because the network and broadcast addresses are not usable.
What does a host do when the destination is outside its own subnet?
It sends the packet to its default gateway.
Which route does a router choose when several match?
The one with the longest matching prefix, which is the most specific.
Topic 2 of 6 in Network Fundamentals
Test yourself on Network Fundamentals
The full Cisco CCNA 200-901 DEVASC question bank has 808 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by Cisco. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.