Menu

Networking Fundamentals

Step 1 of 4 LPIC-1 109.1weight 4

Fundamentals of internet protocols

“IPv4 addresses, masks and CIDR, private ranges, TCP, UDP and ICMP, the ports you must know, and what IPv6 changes.”

View .md

Every network problem you will ever debug comes back to a few basics: which address a machine has, which network it thinks it is on, and which protocol and port a service uses. If you can read 192.168.10.77/26 and instantly know the network, the broadcast and how many hosts fit, half the troubleshooting is done before you type a command.

This byte is the theory part of LPIC-1 topic 109. The next bytes put it to work with real configuration and tools. For what happens on top of these protocols (requests, headers, status codes), see the HTTP topic.

IPv4 addresses#

An IPv4 address is 32 bits. People write it as four numbers from 0 to 255, separated by dots. This is called dotted-quad notation, and each number is an octet (8 bits):

text
192      .168      .10       .77
11000000 .10101000 .00001010 .01001101

Every address has two parts. The network part says which network the machine is on. The host part says which machine on that network. The netmask decides where the line between them falls.

Netmasks and CIDR notation#

A netmask is also 32 bits: ones for the network part, zeros for the host part. You can write it in two ways:

  • Dotted-quad mask: 255.255.255.0
  • CIDR prefix: /24, the number of one bits. CIDR stands for Classless Inter-Domain Routing.

So 192.168.10.77/24 and 192.168.10.77 with mask 255.255.255.0 mean the same thing. Linux tools such as ip use the CIDR form.

PrefixNetmaskAddressesUsable hosts
/8255.0.0.016,777,21616,777,214
/16255.255.0.065,53665,534
/20255.255.240.04,0964,094
/24255.255.255.0256254
/25255.255.255.128128126
/26255.255.255.1926462
/27255.255.255.2243230
/28255.255.255.2401614
/30255.255.255.25242
/32255.255.255.2551a single host

The rule: a /n network has 2(32 − n) addresses. Two of them are reserved, so you can give out 2(32 − n) − 2 to hosts:

  • The network address: all host bits 0. It names the network itself.
  • The broadcast address: all host bits 1. A packet sent there goes to every host on the network.

The values that show up in masks are always the same eight: 0, 128, 192, 224, 240, 248, 252, 254, 255. Learn them and you can convert between mask and prefix in your head.

Subnetting#

Subnetting means borrowing host bits to split one network into smaller ones. Take 192.168.10.0/24 and split it into four networks. Four needs 2 extra bits, so each piece is a /26 with 64 addresses:

SubnetNetwork addressFirst hostLast hostBroadcast
192.168.10.0/26.0.1.62.63
192.168.10.64/26.64.65.126.127
192.168.10.128/26.128.129.190.191
192.168.10.192/26.192.193.254.255

A quick way to find the network of any address: find the octet where the mask is not 255 or 0. Subtract that mask value from 256 to get the block size. The network starts at the largest multiple of the block size that fits.

Example: 172.16.45.130/20. The mask is 255.255.240.0. The interesting octet is the third one: 256 − 240 = 16, so networks start at 0, 16, 32, 48 and so on. 45 lies between 32 and 47, so the network is 172.16.32.0/20 and the broadcast is 172.16.47.255.

You can check your math with ipcalc (sudo apt install ipcalc on Debian and Ubuntu). The -b option leaves out the binary columns:

bash
ipcalc -b 192.168.10.77/26
text
Address:   192.168.10.77
Netmask:   255.255.255.192 = 26
Wildcard:  0.0.0.63
=>
Network:   192.168.10.64/26
HostMin:   192.168.10.65
HostMax:   192.168.10.126
Broadcast: 192.168.10.127
Hosts/Net: 62                    Class C, Private Internet

Note

Fedora and RHEL ship a different program also called ipcalc, with other options. There, ipcalc -n -b 192.168.10.77/26 prints NETWORK=192.168.10.64 and BROADCAST=192.168.10.127.

The old address classes#

Before CIDR (1993), the first octet fixed the mask. You still see the names, and ipcalc still prints them:

ClassFirst octetDefault mask
A1 to 126255.0.0.0 (/8)
B128 to 191255.255.0.0 (/16)
C192 to 223255.255.255.0 (/24)
D224 to 239multicast
E240 to 255reserved

Today any prefix length can be used anywhere. “Class C” is just a casual way to say “a /24”.

Private and public addresses#

Public addresses are unique on the whole internet. Private addresses, defined in RFC 1918, are free for anyone to use inside their own network, and routers on the internet never forward them:

RangeCIDRTypical use
10.0.0.0 to 10.255.255.25510.0.0.0/8companies, cloud networks
172.16.0.0 to 172.31.255.255172.16.0.0/12Docker, VPNs
192.168.0.0 to 192.168.255.255192.168.0.0/16home routers

Machines with private addresses reach the internet through NAT (Network Address Translation): the router swaps the private source address for its own public one on the way out, and back on the way in.

A few more special ranges you should recognize:

  • 127.0.0.0/8: loopback. 127.0.0.1 is always “this machine” and never leaves it.
  • 169.254.0.0/16: link-local. A machine gives itself one of these when it asked for DHCP and nobody answered. Seeing it usually means “DHCP failed”.
  • 0.0.0.0: “any address” when a service listens, or “the default route” in a routing table.
Watch the /12 trap: 172.16.0.0/12 covers 172.16 up to 172.31, so 172.20.1.1 is private but 172.32.1.1 is public.

TCP, UDP and ICMP#

IP moves a packet from one address to another. Three protocols ride on top of it, and the exam wants you to know how they differ.

TCP (Transmission Control Protocol) is connection-oriented. Before any data flows, the two sides do a three-way handshake: SYN, SYN-ACK, ACK. Then TCP numbers every byte, acknowledges what arrived, resends what got lost, delivers everything in order and slows down when the network is congested. You pay for that with a bit of delay and overhead. SSH, HTTP, SMTP and IMAP use TCP.

UDP (User Datagram Protocol) is connectionless. It sends each datagram on its own: no handshake, no acknowledgments, no ordering, no resending. Its header is only 8 bytes, against at least 20 for TCP. That makes it fast and light, perfect for short questions and answers (DNS, NTP, SNMP, DHCP) and for live audio and video, where a late packet is worthless anyway.

ICMP (Internet Control Message Protocol) carries control and error messages for IP itself. It has no ports. It has message types instead:

ICMP typeMeaningWho uses it
8Echo requestping sends it
0Echo replythe answer to ping
3Destination unreachable“no route”, “port closed”
11Time exceededtraceroute relies on it
5Redirect“use another gateway”
FeatureTCPUDPICMP
Connectionyes (handshake)nono
Reliable, orderedyesnono
Portsyesyesno (types and codes)
IP protocol number6171 (ICMPv6: 58)
Typical useSSH, HTTP, mailDNS, NTP, SNMP, syslogping, errors, traceroute

The protocol numbers come from /etc/protocols, a small sister file of /etc/services.

Warning

Blocking all ICMP in a firewall looks safe but breaks things: path MTU discovery needs “too big” messages, and IPv6 cannot work at all without ICMPv6.

Ports and /etc/services#

A port is a 16-bit number (0 to 65535) that picks which program on a machine gets the data. TCP and UDP each have their own set: TCP port 53 and UDP port 53 are different doors.

  • 0 to 1023: well-known ports. Only root (or a program with the right capability) may listen on them.
  • 1024 to 49151: registered ports, for things like databases (MySQL is 3306).
  • 49152 to 65535: dynamic ports. Your side of an outgoing connection picks a temporary one. Linux uses 32768 to 60999 by default (see /proc/sys/net/ipv4/ip_local_port_range).

These are the ports the exam lists. Learn them by heart:

PortProtocolService
20TCPFTP data
21TCPFTP control (commands)
22TCPSSH, also scp and sftp
23TCPTelnet (unencrypted remote login)
25TCPSMTP (mail between servers)
53UDP and TCPDNS (TCP for big answers and zone transfers)
80TCPHTTP
110TCPPOP3 (fetch mail)
123UDPNTP (time sync)
139TCPNetBIOS session service (old Windows file sharing, Samba)
143TCPIMAP (read mail on the server)
161UDPSNMP (monitoring queries)
162UDPSNMP traps (alerts sent by devices)
389TCPLDAP (directory)
443TCPHTTPS
465TCPSMTP over TLS (SMTPS, for mail clients)
514UDPsyslog (remote logging); on TCP, the old rsh
636TCPLDAPS (LDAP over TLS)
993TCPIMAPS (IMAP over TLS)
995TCPPOP3S (POP3 over TLS)

A pattern helps: the TLS versions are 443 (HTTP), 465 (SMTP), 636 (LDAP), 993 (IMAP) and 995 (POP3).

Linux keeps this mapping in /etc/services. Each line is a name, a port/protocol and optional aliases:

bash
grep -wE '^(ssh|domain|ntp|imaps)' /etc/services
text
ssh             22/tcp                          # SSH Remote Login Protocol
domain          53/tcp                          # Domain Name Server
domain          53/udp
ntp             123/udp                         # Network Time Protocol
imaps           993/tcp                         # IMAP over SSL

Programs use this file to turn names into numbers and back. That’s why ss shows :ssh instead of :22 unless you add -n. You can also ask through the system’s name service:

bash
getent services 443/tcp
text
https                 443/tcp

Changing /etc/services does not move a service to another port. It only changes the names tools print. The port a daemon listens on is set in that daemon’s own configuration.

IPv6#

IPv4 has about 4.3 billion addresses, and they ran out years ago. NAT stretched them, but IPv6 is the real fix.

Writing IPv6 addresses#

An IPv6 address is 128 bits, written as eight groups of four hexadecimal digits separated by colons:

text
2001:0db8:0000:0000:0000:ff00:0042:8329

Two rules make it shorter:

  1. Drop leading zeros in each group: 0db8 becomes db8, 0042 becomes 42.
  2. Replace one run of all-zero groups with ::. Only once, or nobody could tell how many zeros each :: stands for.
text
2001:db8::ff00:42:8329

The prefix works like CIDR in IPv4: 2001:db8:1:2::/64 means the first 64 bits are the network. A normal LAN subnet in IPv6 is always a /64, leaving 64 bits for hosts. In a URL, put the address in brackets so its colons don’t clash with the port: http://[2001:db8::1]:8080/.

Address types#

Address or prefixMeaning
::1loopback (IPv4’s 127.0.0.1)
::unspecified, “any address”
fe80::/10link-local: every IPv6 interface has one, only valid on that link
fc00::/7 (in practice fd00::/8)unique local: the IPv6 version of private addresses
2000::/3global unicast: public internet addresses
ff00::/8multicast, for example ff02::1 = all nodes on the link
2001:db8::/32reserved for documentation and examples

Because every interface has a fe80:: address, the same link-local address can exist on several interfaces. So you add the interface name after a %: fe80::1%enp0s3.

On a Linux machine you see both families side by side:

bash
ip -br addr
text
lo        UNKNOWN  127.0.0.1/8 ::1/128
enp0s3    UP       192.168.1.50/24 2001:db8:1:2:a00:27ff:fe4e:66a1/64 fe80::a00:27ff:fe4e:66a1/64

What IPv6 changes#

TopicIPv4IPv6
Address size32 bits, dotted decimal128 bits, colon hexadecimal
Broadcastyesnone, multicast is used instead
Finding neighborsARPNDP (Neighbor Discovery Protocol) over ICMPv6
Automatic addressesDHCPSLAAC (the host builds its own address from the router’s prefix), or DHCPv6
Headervariable, with a checksumfixed 40 bytes, no checksum, extra “extension headers”
Fragmentationsender and routerssender only, routers send ICMPv6 “packet too big”
Minimum MTU68 bytes (every host must accept 576-byte packets)1280 bytes
NATcommonnot needed, every device can have a global address
IPsecoptional add-ondesigned in from the start

Most systems run dual stack: IPv4 and IPv6 at the same time, preferring IPv6 when both work. The ping, traceroute and tracepath commands have IPv6 versions (ping6 and friends), covered in basic network troubleshooting.

SLAAC used to build the host part from the network card’s MAC address (that’s the ff:fe in the middle of a00:27ff:fe4e:66a1). Because that lets websites track a laptop across networks, modern systems also make random “privacy” addresses that change over time.

Exam checklist#

  • /n means n network bits. Hosts per network: 2(32 − n) − 2. Know masks /8 to /30 both as prefix and dotted quad.
  • Network address = all host bits 0, broadcast = all host bits 1. Use the block size (256 minus the mask octet) to find them fast.
  • Private ranges: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16. Loopback 127.0.0.0/8, link-local 169.254.0.0/16.
  • TCP: connection, handshake, reliable and ordered. UDP: connectionless, small header, no guarantees. ICMP: control messages, types instead of ports, used by ping and traceroute.
  • Ports 20/21 FTP, 22 SSH, 23 Telnet, 25 SMTP, 53 DNS, 80 HTTP, 110 POP3, 123 NTP, 139 NetBIOS, 143 IMAP, 161/162 SNMP, 389 LDAP, 443 HTTPS, 465 SMTPS, 514 syslog, 636 LDAPS, 993 IMAPS, 995 POP3S.
  • /etc/services maps service names to port/protocol. Editing it does not change where a daemon listens.
  • IPv6: 128 bits, eight hex groups, drop leading zeros, :: only once. ::1 loopback, fe80::/10 link-local, fc00::/7 unique local, ff00::/8 multicast.
  • IPv6 has no broadcast and no ARP (NDP instead), SLAAC for automatic addresses, a fixed 40-byte header, and only the sender fragments.

Byte check

0/3

Answer these 3 questions to commit this byte to your learning path.

  1. A host has the address 172.16.45.130/20. What is its network address?
  2. Which service normally listens on port 636?
  3. Which statement about IPv6 is true?
Search posts and bytes

Optional account

Keep your progress everywhere

Everything on AminBytes is free to read, with or without an account. Sign in only if you want your commits, streak and merged topics saved, on every device.

or

We keep only your name, avatar and which bytes you finished, and you can delete it all anytime. Privacy