Linux Networking

Configure, manage, and troubleshoot networks on Linux with confidence using these essential tools and techniques.

Network Configuration Basics

Modern Linux distributions use either NetworkManager (desktop systems) or systemd-networkd / Netplan (servers) to manage network connections. Understanding both approaches is essential for any Linux administrator.

Viewing Network Interfaces

# Show all network interfaces and their IP addresses
ip addr show

# Short version
ip a

# Show only active interfaces
ip link show up

# Show routing table
ip route show

Configuring a Static IP with Netplan (Ubuntu Server)

# /etc/netplan/01-static.yaml
network:
  version: 2
  ethernets:
    eth0:
      addresses:
        - 192.168.1.100/24
      gateway4: 192.168.1.1
      nameservers:
        addresses:
          - 1.1.1.1
          - 8.8.8.8

# Apply the configuration
sudo netplan apply

NetworkManager CLI (nmcli)

# List all connections
nmcli connection show

# Show active Wi-Fi networks
nmcli device wifi list

# Connect to a Wi-Fi network
nmcli device wifi connect "NetworkName" password "password123"

# Set a static IP
nmcli connection modify "Wired connection 1" \
  ipv4.addresses 192.168.1.100/24 \
  ipv4.gateway 192.168.1.1 \
  ipv4.dns "1.1.1.1,8.8.8.8" \
  ipv4.method manual

nmcli connection up "Wired connection 1"

DNS Configuration

DNS (Domain Name System) translates human-readable domain names into IP addresses. On modern Linux systems, DNS is typically managed through systemd-resolved or configured directly in /etc/resolv.conf.

# Check current DNS settings
resolvectl status

# Test DNS resolution
dig linuxland.nl
nslookup linuxland.nl

# Edit hosts file for local overrides
sudo nano /etc/hosts
# 192.168.1.50 myserver.local
Tip: For improved privacy and speed, consider using DNS over HTTPS (DoH) or DNS over TLS (DoT). Both Cloudflare (1.1.1.1) and Google (8.8.8.8) support encrypted DNS.

Network Troubleshooting Tools

ToolPurposeExample
pingTest basic connectivityping -c 4 google.com
tracerouteTrace the path packets taketraceroute google.com
digDNS lookup and diagnosticsdig example.com A
ssShow open sockets and portsss -tulnp
curlHTTP requests and testingcurl -I https://example.com
wgetDownload fileswget https://example.com/file.zip
nmapNetwork and port scanningnmap -sV 192.168.1.1
tcpdumpCapture and analyse packetssudo tcpdump -i eth0 port 80
mtrCombined ping and traceroutemtr google.com

Common Troubleshooting Workflow

# Step 1: Check if interface is up
ip link show

# Step 2: Check if you have an IP address
ip addr show

# Step 3: Test local network (gateway)
ping -c 3 192.168.1.1

# Step 4: Test external connectivity
ping -c 3 1.1.1.1

# Step 5: Test DNS resolution
ping -c 3 google.com

# Step 6: Check open ports and listening services
sudo ss -tulnp

This step-by-step approach isolates the problem layer by layer: interface, IP, local network, internet, then DNS.

Managing Network Services

Checking Open Ports

# Show all listening TCP and UDP ports
sudo ss -tulnp

# Find what process is using port 80
sudo ss -tlnp | grep :80

# Scan your own server for open ports
sudo nmap -sT localhost

SSH Tunnelling and Port Forwarding

SSH tunnels let you securely forward traffic through an encrypted connection, which is useful for accessing remote services or bypassing firewalls:

# Local port forwarding: access remote service locally
# Forwards local port 8080 to remote port 80
ssh -L 8080:localhost:80 user@remote-server

# Remote port forwarding: expose local service remotely
ssh -R 9090:localhost:3000 user@remote-server

# SOCKS proxy through SSH
ssh -D 1080 user@remote-server

VPN on Linux

Virtual Private Networks encrypt all traffic between your machine and the VPN server. Linux supports several VPN protocols natively.

WireGuard

A modern, fast, and lightweight VPN protocol built into the Linux kernel since version 5.6. WireGuard uses state-of-the-art cryptography and is significantly simpler to configure than older alternatives. It is now the recommended choice for most Linux VPN setups.

OpenVPN

The most widely supported VPN protocol with broad compatibility across platforms and commercial VPN providers. OpenVPN is battle-tested and runs in userspace, making it highly portable. Most commercial VPN services provide OpenVPN configuration files.

WireGuard Quick Setup

# Install WireGuard
sudo apt install wireguard -y

# Generate key pair
wg genkey | tee privatekey | wg pubkey > publickey

# Create configuration
sudo nano /etc/wireguard/wg0.conf

[Interface]
PrivateKey = YOUR_PRIVATE_KEY
Address = 10.0.0.1/24
ListenPort = 51820

[Peer]
PublicKey = PEER_PUBLIC_KEY
AllowedIPs = 10.0.0.2/32
Endpoint = peer.example.com:51820

# Start the VPN interface
sudo wg-quick up wg0

# Enable on boot
sudo systemctl enable wg-quick@wg0

Network Monitoring

# Monitor bandwidth in real time
sudo apt install iftop -y
sudo iftop -i eth0

# Monitor network connections
sudo apt install nethogs -y
sudo nethogs eth0

# Network statistics per interface
ip -s link show eth0

Learn about managing software on Linux.

Package Management →