21. Networking Basics
A solid understanding of networking is essential for administering Linux servers, configuring microservices, debugging container connectivity, and hardening cloud infrastructure.
21.1 Core Networking Concepts
Section titled “21.1 Core Networking Concepts”Network Interfaces & MAC Addresses
Section titled “Network Interfaces & MAC Addresses”- Network Interface: The operating system representation of a physical network interface controller (NIC) or virtual interface. Common naming conventions include:
eth0: Traditional legacy Ethernet naming.enp0s3/ens33: Modern predictable network interface names (based on bus topology or firmware index).lo: Local loopback interface (127.0.0.1), allowing local services to communicate with each other.
- MAC Address: A unique 48-bit physical hardware identifier burned into the NIC. Operates at Layer 2 (Data Link layer).
IP Addresses & Subnetting
Section titled “IP Addresses & Subnetting”- IPv4 (32-bit): Expressed as four dotted-decimal octets (e.g.,
192.168.1.50). - IPv6 (128-bit): Hexadecimal notation (e.g.,
2001:0db8:85a3::7334) designed to replace IPv4 without requiring NAT. - Subnetting & CIDR: A subnet mask (e.g.,
255.255.255.0or/24in CIDR notation) partitions the IP address into a Network ID and a Host ID. Devices within the same subnet communicate directly via ARP and Layer 2 frames; devices in different subnets must traverse a router.
Routing & The Default Gateway
Section titled “Routing & The Default Gateway”- Routing Table: Kernel routing rules that dictate which interface and gateway forward outbound packets based on destination IP.
- Default Gateway: The IP address of the upstream router that handles all traffic destined outside the local subnet (e.g., internet destinations).
21.2 Hostname & Name Resolution Architecture
Section titled “21.2 Hostname & Name Resolution Architecture”When a program requests a connection to a hostname (e.g., db-server), Linux resolves the name into an IP address using a defined resolution chain.
Application requests 'db-server' ↓ /etc/nsswitch.conf (hosts: files dns) ┌───────┴───────┐ ↓ ↓ /etc/hosts /etc/resolv.conf (Static) (Upstream DNS)1. Hostname Management (hostnamectl)
Section titled “1. Hostname Management (hostnamectl)”hostnamectl (part of systemd) manages the system hostname persistently:
# View current hostname and system architecturehostnamectl status
# Set a new persistent hostname (takes effect immediately)sudo hostnamectl set-hostname server1.example.com2. Local Host Resolution (/etc/hosts)
Section titled “2. Local Host Resolution (/etc/hosts)”Static IP-to-hostname mappings defined locally. Checked before external DNS:
# Example /etc/hosts entries127.0.0.1 localhost192.168.2.100 file-server file10.0.0.33 db-server3. Resolution Precedence (/etc/nsswitch.conf)
Section titled “3. Resolution Precedence (/etc/nsswitch.conf)”Governs the order of name resolution services. The hosts: directive typically reads:
hosts: files dns myhostnameThis instructs the system to query local /etc/hosts (files) first, and fall back to DNS (dns) if the record is not found.
4. Upstream Nameserver Configuration (/etc/resolv.conf)
Section titled “4. Upstream Nameserver Configuration (/etc/resolv.conf)”Specifies the recursive DNS servers used for external domain resolution:
nameserver 1.1.1.1nameserver 8.8.8.8[!NOTE] On modern distributions using NetworkManager or
systemd-resolved,/etc/resolv.confis dynamically generated. Changes should be made vianmcliorresolvectlto prevent them from being overwritten.
21.3 Modern Network Management Commands (iproute2)
Section titled “21.3 Modern Network Management Commands (iproute2)”The legacy net-tools package (ifconfig, route, netstat) is deprecated. Modern Linux administration standardizes on the iproute2 toolset.
1. ip link (Layer 2 Interface State)
Section titled “1. ip link (Layer 2 Interface State)”# List all network interfaces and their link state (UP/DOWN, MTU, MAC)ip link show
# Enable or disable an interfacesudo ip link set eth0 upsudo ip link set eth0 down2. ip addr (Layer 3 IP Addresses)
Section titled “2. ip addr (Layer 3 IP Addresses)”# Display IP addresses assigned to all interfacesip addr show
# Show IP addresses for a specific interfaceip addr show dev eth03. ip route (Routing Table)
Section titled “3. ip route (Routing Table)”# View current kernel routing table and default gatewayip route show# Example: 'default via 192.168.1.1 dev eth0 proto static metric 100'4. ss (Socket Statistics)
Section titled “4. ss (Socket Statistics)”Fast, modern replacement for netstat that inspects socket buffers and port states:
# Display all listening (-l) TCP (-t) and UDP (-u) ports with numeric IPs (-n) and PIDs (-p)sudo ss -tulpn
# Summary statistics of all active socket connectionsss -s21.4 Diagnostics & Packet Capture
Section titled “21.4 Diagnostics & Packet Capture”1. ping & traceroute
Section titled “1. ping & traceroute”# Test ICMP echo response (4 packets)ping -c 4 8.8.8.8
# Trace path across network hops to the destinationtraceroute google.com# or without root privileges:tracepath google.com2. Network Packet Capture (tcpdump)
Section titled “2. Network Packet Capture (tcpdump)”Inspect live packet headers directly on the wire:
# Capture raw packets on interface eth0 with numeric addresses (-n)sudo tcpdump -i eth0 -n
# Filter packets for a specific port (e.g., HTTP traffic)sudo tcpdump -i eth0 -n port 80
# Capture and write packets to a file for Wireshark analysissudo tcpdump -i eth0 -w /tmp/traffic.pcap21.5 Virtual Machine & Hypervisor Network Modes
Section titled “21.5 Virtual Machine & Hypervisor Network Modes”When deploying Linux inside virtual machines (KVM, VMware, VirtualBox), understanding the hypervisor networking mode is critical:
| Mode | VM to Internet | VM to Host | VM to LAN Devices | Typical Use Case | |||||| | NAT | Yes | Yes | No | Safe internet browsing, sandboxed dev | | Bridged | Yes | Yes | Yes | Production servers, LAN services | | Host-Only | No | Yes | No | Isolated host-to-VM private communication | | Internal | No | No | Only other VMs | Multi-tier isolated VM test labs |
21.6 Key Linux Network Configuration Files
Section titled “21.6 Key Linux Network Configuration Files”| Path | Primary Function |
|||
| /etc/hosts | Static local IP-to-hostname mappings |
| /etc/hostname | Persistent system hostname |
| /etc/resolv.conf | DNS nameserver resolvers |
| /etc/nsswitch.conf | Name resolution lookup precedence |
| /etc/NetworkManager/system-connections/ | NetworkManager persistent profile files |
| /etc/firewalld/ | Persistent firewall zone and rule definitions |
| /var/log/syslog or /var/log/messages | System and network daemon log events |