Skip to content

21. Networking Basics

A solid understanding of networking is essential for administering Linux servers, configuring microservices, debugging container connectivity, and hardening cloud infrastructure.

  • 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).
  • 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.0 or /24 in 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 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)

hostnamectl (part of systemd) manages the system hostname persistently:

Terminal window
# View current hostname and system architecture
hostnamectl status
# Set a new persistent hostname (takes effect immediately)
sudo hostnamectl set-hostname server1.example.com

Static IP-to-hostname mappings defined locally. Checked before external DNS:

# Example /etc/hosts entries
127.0.0.1 localhost
192.168.2.100 file-server file
10.0.0.33 db-server

3. 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 myhostname

This 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.1
nameserver 8.8.8.8

[!NOTE] On modern distributions using NetworkManager or systemd-resolved, /etc/resolv.conf is dynamically generated. Changes should be made via nmcli or resolvectl to 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.

Terminal window
# List all network interfaces and their link state (UP/DOWN, MTU, MAC)
ip link show
# Enable or disable an interface
sudo ip link set eth0 up
sudo ip link set eth0 down
Terminal window
# Display IP addresses assigned to all interfaces
ip addr show
# Show IP addresses for a specific interface
ip addr show dev eth0
Terminal window
# View current kernel routing table and default gateway
ip route show
# Example: 'default via 192.168.1.1 dev eth0 proto static metric 100'

Fast, modern replacement for netstat that inspects socket buffers and port states:

Terminal window
# 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 connections
ss -s
Terminal window
# Test ICMP echo response (4 packets)
ping -c 4 8.8.8.8
# Trace path across network hops to the destination
traceroute google.com
# or without root privileges:
tracepath google.com

Inspect live packet headers directly on the wire:

Terminal window
# 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 analysis
sudo tcpdump -i eth0 -w /tmp/traffic.pcap

21.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 |