dotlinux guide

Basic Linux Networking: Configuring and Troubleshooting Tips

Linux powers everything from embedded devices to enterprise servers, and at the heart of its versatility lies robust networking capabilities. Whether you’re a system administrator managing servers, a developer debugging application connectivity, or a hobbyist setting up a home lab, understanding Linux networking fundamentals is critical. This blog demystifies basic Linux networking, covering core concepts, configuration tools, troubleshooting workflows, and best practices. By the end, you’ll be equipped to configure network interfaces, diagnose common issues, and maintain reliable connectivity in Linux environments.

Table of Contents

  1. Fundamental Networking Concepts
    • IP Addressing & Subnetting
    • Default Gateway
    • DNS (Domain Name System)
  2. Essential Networking Tools
    • ip (Replacements for ifconfig)
    • NetworkManager: nmcli & nmtui
    • netplan (Modern Configuration)
  3. Configuring Network Interfaces
    • Dynamic IP (DHCP)
    • Static IP Assignment
  4. DNS and Hostname Configuration
    • /etc/resolv.conf and DNS Servers
    • Hostname Management
    • /etc/hosts for Local Resolution
  5. Troubleshooting Common Networking Issues
    • Verifying Connectivity: ping, traceroute, mtr
    • Inspecting Ports and Connections: ss
    • Packet Capture: tcpdump
    • Firewall Checks: iptables/ufw
  6. Best Practices
  7. Conclusion
  8. References

Fundamental Networking Concepts

Before diving into configuration, let’s clarify key networking terms:

IP Addressing & Subnetting

An IP address (e.g., 192.168.1.10) uniquely identifies a device on a network. IPv4 (32-bit) is most common, but IPv6 (128-bit) is growing.

  • Subnet Mask/CIDR: Defines the network portion of an IP. For example, 192.168.1.10/24 (CIDR notation) means the first 24 bits are the network (192.168.1.0), and the last 8 bits are for hosts. The subnet mask here is 255.255.255.0.

Default Gateway

A gateway is a router that connects your local network to external networks (e.g., the internet). Use ip route show to view the default gateway:

ip route show | grep default  # Output: default via 192.168.1.1 dev eth0

DNS (Domain Name System)

DNS translates human-readable domain names (e.g., google.com) to IP addresses. Without DNS, you’d need to remember 8.8.8.8 instead of google.com.

Essential Networking Tools

Linux offers powerful tools to manage and monitor networks. Here are the most critical:

ip Command (iproute2 Suite)

The ip command replaces legacy tools like ifconfig and route. Key subcommands:

TaskCommand Example
Show interfacesip link show
Show IP addressesip addr show (or ip a)
Bring interface up/downip link set eth0 up / ip link set eth0 down
Add static routeip route add 10.0.0.0/24 via 192.168.1.1 dev eth0

NetworkManager: nmcli & nmtui

NetworkManager is a dynamic network management tool (default on most Linux desktops/servers).

  • nmcli (CLI): Manage connections, interfaces, and settings.
    Example: List active connections:

    nmcli connection show --active  # "Wired connection 1" might be the default
  • nmtui (Text UI): Interactive menu for configuring networks (run nmtui in terminal).

netplan (Modern Configuration)

Netplan (used in Ubuntu 18.04+, Debian 10+) uses YAML config files to define networks. Configs live in /etc/netplan/ (e.g., 01-netcfg.yaml).

Configuring Network Interfaces

Dynamic IP (DHCP)

Most home networks use DHCP to assign IPs automatically.

With nmcli:

nmcli connection modify "Wired connection 1" ipv4.method auto  
nmcli connection up "Wired connection 1"  # Apply changes

With netplan:

Edit /etc/netplan/01-netcfg.yaml:

network:
  version: 2
  renderer: NetworkManager  # Use NetworkManager (or systemd-networkd)
  ethernets:
    eth0:  # Replace with your interface name (check `ip link show`)
      dhcp4: true  # Enable IPv4 DHCP
      dhcp6: true  # Optional: Enable IPv6 DHCP

Apply with:

sudo netplan apply

Static IP Assignment

For servers, static IPs ensure consistent connectivity.

With nmcli:

nmcli connection modify "Wired connection 1" \
  ipv4.method manual \
  ipv4.addresses 192.168.1.100/24 \  # IP and subnet (CIDR)
  ipv4.gateway 192.168.1.1 \         # Default gateway
  ipv4.dns "8.8.8.8, 8.8.4.4"        # DNS servers (Google DNS)
nmcli connection up "Wired connection 1"

With netplan:

Edit /etc/netplan/01-netcfg.yaml:

network:
  version: 2
  renderer: networkd  # Use systemd-networkd (no GUI)
  ethernets:
    enp0s3:  # Interface name (check `ip link show`)
      addresses: [192.168.1.100/24]  # Static IP/CIDR
      gateway4: 192.168.1.1          # Default gateway (IPv4)
      nameservers:
        addresses: [8.8.8.8, 8.8.4.4]  # DNS servers

Apply:

sudo netplan apply

DNS and Hostname Configuration

DNS Servers

DNS servers are defined in /etc/resolv.conf, but modern systems (using NetworkManager or systemd-resolved) may manage this file dynamically.

With nmcli:

Set DNS for a connection:

nmcli connection modify "Wired connection 1" ipv4.dns "8.8.8.8, 1.1.1.1"
nmcli connection up "Wired connection 1"

With netplan:

DNS is configured directly in the netplan YAML (see static IP example above).

Hostname Management

The hostname identifies your system on the network.

  • View/set hostname:

    hostname  # Show current hostname
    sudo hostnamectl set-hostname "server01"  # Set new hostname (persistent)
  • /etc/hostname: Stores the hostname (updated by hostnamectl).

/etc/hosts File

For local name resolution (bypassing DNS), edit /etc/hosts:

sudo nano /etc/hosts

Add entries like:

127.0.0.1   localhost  
192.168.1.50 nas.local  # Map "nas.local" to a local server

Troubleshooting Common Networking Issues

When connectivity fails, follow this workflow:

1. Verify Interface Status

Check if the interface is up and has an IP:

ip link show eth0  # "UP" should appear (e.g., `<BROADCAST,MULTICAST,UP,LOWER_UP>`)
ip addr show eth0  # Ensure an IP is assigned (e.g., `inet 192.168.1.100/24`)

2. Test Connectivity

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

ping -c 4 192.168.1.1  # Ping gateway (4 packets)
ping -c 4 8.8.8.8      # Ping Google DNS (test internet)

traceroute: Trace path to a host (shows hops):

traceroute google.com  # Identify where the connection fails

mtr: Combines ping and traceroute (real-time):

mtr google.com  # Ctrl+C to exit; look for packet loss

3. Inspect Ports and Connections

Use ss (replaces netstat) to check open ports and active connections:

ss -tuln  # Show listening TCP/UDP ports (t: TCP, u: UDP, l: listening, n: numeric)
ss -tulnp # Show process IDs (requires root: `sudo ss -tulnp`)
ss -t state established  # Show established TCP connections

4. Capture Packets with tcpdump

Debug traffic with packet capture (e.g., port 80):

sudo tcpdump -i eth0 port 80  # Capture HTTP traffic on eth0
sudo tcpdump -w capture.pcap  # Save to file (open with Wireshark later)

5. Check Firewall Rules

Linux firewalls (e.g., iptables, ufw) may block traffic.

ufw (Uncomplicated Firewall, user-friendly):

sudo ufw status  # Show active rules (e.g., "Status: active, Allow 22/tcp")
sudo ufw allow 80/tcp  # Allow HTTP (if needed)

iptables (advanced):

sudo iptables -L  # List all rules (look for DROP/REJECT on critical ports)

Common Issues & Fixes

SymptomLikely CauseFix
No IP addressDHCP failure or misconfigured staticsudo dhclient eth0 (force DHCP) or check netplan/nmcli config
Can ping IP but not domainDNS failureCheck /etc/resolv.conf or set DNS via nmcli/netplan
Connection refusedFirewall block or service not runningCheck ss -tuln for listening port; allow via ufw

Best Practices

  1. Use Modern Tools: Prefer ip over ifconfig, nmcli/netplan over manual config files.
  2. Backup Configs: Before editing, backup files (e.g., sudo cp /etc/netplan/01-netcfg.yaml /etc/netplan/01-netcfg.yaml.bak).
  3. Static IP for Servers: Avoid DHCP for critical services (e.g., databases, web servers).
  4. Secure with Firewalls: Default-deny incoming traffic; allow only necessary ports (e.g., 22 for SSH, 80/443 for web).
  5. Document Changes: Log network config modifications (e.g., IP assignments, DNS servers).
  6. Monitor Networks: Use tools like iftop (bandwidth) or nload to track usage.

Conclusion

Mastering basic Linux networking is foundational for system administration and development. By understanding IP addressing, using tools like ip, nmcli, and netplan, and following troubleshooting workflows, you can resolve most connectivity issues efficiently. Remember to prioritize security with firewalls and document changes to maintain reliable networks.

With these skills, you’ll be well-equipped to manage Linux networks in home labs, enterprise environments, or cloud instances.

References