dotlinux guide

Linux Networking Basics: What Every Sysadmin Should Know

In the modern IT landscape, Linux serves as the backbone of servers, cloud infrastructure, edge devices, and everything in between. At the heart of its functionality lies networking—a critical pillar that enables communication between systems, services, and users. Whether you’re managing a small on-premises server or a sprawling cloud environment, a deep understanding of Linux networking basics is non-negotiable for sysadmins. This blog demystifies Linux networking fundamentals, equipping you with the knowledge to configure, troubleshoot, and secure network interfaces, services, and connections. From IP addressing to firewall management, we’ll cover essential concepts, practical tools, and best practices to ensure your Linux networks are reliable, secure, and efficient.

Table of Contents

  1. Key Networking Concepts

    • IP Addressing (IPv4 vs. IPv6)
    • CIDR Notation and Subnetting
    • Gateway and Routing
    • DNS and Name Resolution
    • MAC Addresses and ARP
  2. Network Interfaces in Linux

    • Identifying Interfaces
    • Physical vs. Virtual Interfaces
    • Managing Interface State
  3. Configuring Network Interfaces

    • Traditional Methods (ifupdown, sysconfig)
    • Modern Tools (NetworkManager, systemd-networkd)
    • Static vs. DHCP Configuration
  4. Troubleshooting Network Issues

    • Essential Tools (ping, traceroute, mtr)
    • Inspecting Connections (ss, ip route)
    • Packet Capture (tcpdump)
    • DNS Debugging (dig, nslookup)
  5. Common Network Services

    • DHCP Client Configuration
    • DNS Resolution
    • SSH for Remote Access
  6. Best Practices for Linux Networking

    • Security: Firewalls and Access Control
    • Monitoring Network Health
    • Documentation and Change Management
    • Consistency with Automation
  7. Conclusion

  8. References

Key Networking Concepts

Before diving into Linux-specific tools, let’s review foundational networking concepts every sysadmin must know.

IP Addressing (IPv4 vs. IPv6)

An IP address is a unique identifier for a device on a network. Linux supports both IPv4 (32-bit, e.g., 192.168.1.10) and IPv6 (128-bit, e.g., 2001:db8::1). IPv4 is still dominant, but IPv6 is critical for scaling beyond IPv4’s address limitations.

CIDR Notation and Subnetting

Classless Inter-Domain Routing (CIDR) simplifies IP address management by combining an IP and a subnet mask into a single notation: IP/prefix-length. The prefix length indicates how many bits of the IP are network bits (the rest are host bits).

Example:

  • 192.168.1.0/24 means the first 24 bits are network bits (subnet mask 255.255.255.0), supporting 254 hosts (.1 to .254).

Gateway and Routing

A gateway is a router that connects your local network to external networks (e.g., the internet). The routing table on a Linux system determines how packets are forwarded to their destination. Use ip route to view the routing table:

ip route show
# Example output:
# default via 192.168.1.1 dev eth0 proto dhcp src 192.168.1.10 metric 100
# 192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.10 metric 100

Here, default via 192.168.1.1 specifies the default gateway.

DNS and Name Resolution

Domain Name System (DNS) translates human-readable domain names (e.g., google.com) to IP addresses. Linux systems use /etc/resolv.conf (or systemd-resolved) to configure DNS servers:

cat /etc/resolv.conf
# Example output:
# nameserver 8.8.8.8  # Google DNS
# nameserver 8.8.4.4

MAC Addresses and ARP

A MAC address is a hardware-level identifier embedded in network interfaces (e.g., 00:1a:2b:3c:4d:5e). The Address Resolution Protocol (ARP) maps IP addresses to MAC addresses on local networks. Use ip neigh to view the ARP table:

ip neigh show
# Example output:
# 192.168.1.1 dev eth0 lladdr 00:aa:bb:cc:dd:ee REACHABLE

Network Interfaces in Linux

Linux represents network connections as interfaces (e.g., Ethernet, Wi-Fi, virtual bridges).

Identifying Interfaces

Use ip link or ip addr to list all interfaces:

ip link show
# Example output:
# 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
#     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
# 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
#     link/ether 00:1a:2b:3c:4d:5e brd ff:ff:ff:ff:ff:ff
  • lo: Loopback interface (local testing, 127.0.0.1).
  • eth0: Physical Ethernet interface (naming varies: ens33, enp0s3 on modern systems).

Physical vs. Virtual Interfaces

  • Physical: Hardware-based (Ethernet, Wi-Fi: eth0, wlan0).
  • Virtual: Software-defined (e.g., br0 for bridges, vlan0 for VLANs, tun0 for VPN tunnels).

Managing Interface State

Bring interfaces up/down with ip link:

# Bring eth0 up
sudo ip link set dev eth0 up

# Bring eth0 down
sudo ip link set dev eth0 down

Configuring Network Interfaces

Linux offers multiple tools to configure interfaces. We’ll cover traditional and modern approaches.

Traditional Methods

Debian/Ubuntu: /etc/network/interfaces

Older Debian-based systems use ifupdown with /etc/network/interfaces:

# Static IP configuration for eth0
auto eth0
iface eth0 inet static
  address 192.168.1.10/24  # IP and CIDR
  gateway 192.168.1.1      # Default gateway
  dns-nameservers 8.8.8.8 8.8.4.4  # DNS servers

Apply changes with sudo ifdown eth0 && sudo ifup eth0.

RHEL/CentOS: /etc/sysconfig/network-scripts

Older RHEL-based systems use sysconfig scripts (e.g., /etc/sysconfig/network-scripts/ifcfg-eth0):

DEVICE=eth0
BOOTPROTO=static  # or dhcp for dynamic IP
IPADDR=192.168.1.10
PREFIX=24         # Subnet mask (255.255.255.0)
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4
ONBOOT=yes        # Bring up on boot

Apply changes with sudo systemctl restart network.

Modern Tools

NetworkManager (nmcli/nmtui)

NetworkManager is the default on most desktop and server distributions. Use nmcli (CLI) or nmtui (text UI) for configuration.

Example: Static IP with nmcli

# Create a new connection profile for eth0
sudo nmcli con add con-name "static-eth0" ifname eth0 type ethernet ip4 192.168.1.10/24 gw4 192.168.1.1

# Set DNS servers
sudo nmcli con mod "static-eth0" ipv4.dns "8.8.8.8 8.8.4.4"

# Activate the connection
sudo nmcli con up "static-eth0"

systemd-networkd

systemd-based systems (e.g., Fedora, Debian 10+) use systemd-networkd for lightweight, declarative configuration. Define interfaces in /etc/systemd/network/*.network files:

Example: /etc/systemd/network/eth0.network

[Match]
Name=eth0  # Match interface by name

[Network]
Address=192.168.1.10/24
Gateway=192.168.1.1
DNS=8.8.8.8 8.8.4.4

Restart the service to apply:

sudo systemctl restart systemd-networkd

Troubleshooting Network Issues

When networks break, these tools will save you.

Testing Connectivity: ping, traceroute, mtr

  • ping: Check if a host is reachable (ICMP):

    ping -c 4 8.8.8.8  # Send 4 packets to Google DNS
  • traceroute: Map the path to a host (shows hops):

    traceroute google.com
  • mtr: Combines ping and traceroute for real-time hop analysis:

    mtr google.com  # Press q to exit

Inspecting Interfaces and Routes

  • ip addr: Show IP addresses assigned to interfaces:

    ip addr show eth0  # Filter by interface
  • ip route: Debug routing issues (e.g., missing gateway):

    ip route show  # List all routes
    sudo ip route add default via 192.168.1.1 dev eth0  # Add default gateway

Inspecting Connections: ss

ss (socket statistics) replaces netstat (deprecated) to list open ports and connections:

ss -tuln  # t: TCP, u: UDP, l: listening, n: numeric (no DNS)
# Example output:
# Netid State  Recv-Q Send-Q Local Address:Port  Peer Address:Port
# tcp   LISTEN 0      128    0.0.0.0:22         0.0.0.0:*        # SSH listening

Packet Capture: tcpdump

Capture raw network traffic to debug issues (e.g., dropped packets, misconfigured services):

# Capture HTTP traffic on eth0 (port 80)
sudo tcpdump -i eth0 -n port 80

DNS Debugging: dig

dig (domain information groper) troubleshoots DNS resolution:

dig google.com  # Resolve google.com
dig @8.8.8.8 google.com  # Use specific DNS server

Common Network Services

Linux relies on key services for network functionality.

DHCP Client Configuration

To dynamically fetch an IP from a DHCP server:

  • dhclient: Traditional client:

    sudo dhclient eth0  # Request IP for eth0
  • NetworkManager: Automatically handles DHCP by default (set BOOTPROTO=dhcp in profiles).

DNS Resolution

Linux uses /etc/resolv.conf, but modern systems (e.g., systemd-resolved) manage this file dynamically. To override DNS temporarily:

echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf

For persistent changes, use NetworkManager or systemd-networkd (see Configuring Interfaces).

SSH for Remote Access

Secure Shell (SSH) is critical for remote server management.

  • Key-Based Authentication (more secure than passwords):

    # Generate SSH key pair (client-side)
    ssh-keygen -t ed25519  # Use ed25519 for better security
    
    # Copy public key to remote server
    ssh-copy-id user@remote-server-ip
  • Harden SSH (edit /etc/ssh/sshd_config):

    PermitRootLogin no          # Disable root login
    PasswordAuthentication no   # Require keys
    AllowUsers alice bob        # Restrict allowed users

    Restart the SSH service:

    sudo systemctl restart sshd

Best Practices for Linux Networking

Security: Firewalls and Access Control

  • UFW (Uncomplicated Firewall) (Ubuntu/Debian):

    sudo ufw allow 22/tcp  # Allow SSH
    sudo ufw allow 80/tcp  # Allow HTTP
    sudo ufw enable        # Start firewall on boot
  • firewalld (RHEL/CentOS):

    sudo firewall-cmd --add-port=22/tcp --permanent  # Allow SSH
    sudo firewall-cmd --reload  # Apply changes
  • Disable Unused Services: Stop/disable services like telnet, ftp, or cups if unused.

Monitoring Network Health

  • iftop/nload: Real-time bandwidth usage:

    sudo iftop -i eth0  # Monitor eth0 bandwidth
    nload               # Simple CLI bandwidth monitor
  • Prometheus + Node Exporter: For enterprise-grade monitoring (track throughput, latency, errors).

Documentation and Change Management

  • Track IP assignments, VLANs, and gateway configurations in a tool like NetBox.
  • Log network changes (e.g., “Added static IP 192.168.1.10 to server X”).

Consistency with Automation

Use tools like Ansible to enforce network configurations across servers:

# Ansible playbook example: Configure static IP
- name: Set static IP on eth0
  hosts: servers
  tasks:
    - name: Configure interface
      ansible.builtin.template:
        src: eth0.network.j2
        dest: /etc/systemd/network/eth0.network
      notify: restart networkd

  handlers:
    - name: restart networkd
      ansible.builtin.service:
        name: systemd-networkd
        state: restarted

Conclusion

Linux networking is a vast topic, but mastering these basics—interfaces, IP addressing, troubleshooting tools, and best practices—will make you a more effective sysadmin. Whether you’re setting up a home lab or managing enterprise infrastructure, the ability to configure, debug, and secure networks is foundational.

Practice with the tools covered here (e.g., ip, ss, tcpdump), document your setups, and automate relentlessly. As networks evolve, stay curious: new tools like nmstate (declarative network API) and eBPF (advanced tracing) are reshaping Linux networking, but the fundamentals will always apply.

References