funwithlinux guide

An Introduction to Linux Network Configuration Commands

In the world of Linux, networking is the backbone of connectivity—whether you’re managing a server, troubleshooting a desktop, or deploying IoT devices. Unlike graphical tools, Linux offers a rich set of command-line utilities for network configuration, monitoring, and troubleshooting. These tools are not only powerful but also essential for automation, remote management, and deep system insights. This blog aims to demystify Linux network configuration commands, breaking down their purpose, syntax, and practical examples. By the end, you’ll have a solid foundation to manage IP addresses, test connectivity, configure DNS, set up routing, and even tweak firewalls—all from the terminal.

Table of Contents

  1. Prerequisites
  2. Basic Network Information Commands
  3. Connectivity Testing Commands
  4. DNS Configuration and Troubleshooting
  5. Network Interface Configuration
  6. Routing Table Commands
  7. Firewall Configuration Basics
  8. Conclusion
  9. References

Prerequisites

To follow along, you’ll need:

  • A Linux-based operating system (e.g., Ubuntu, CentOS, Fedora).
  • Basic familiarity with the command line (e.g., navigating directories, running commands).
  • sudo privileges to modify network settings (required for most configuration commands).

Basic Network Information Commands

Before configuring networks, you first need to inspect existing settings. These commands help you view interfaces, IP addresses, open ports, and more.

ip: The Swiss Army Knife of Network Commands

The ip command (part of the iproute2 package) is the modern replacement for legacy tools like ifconfig, route, and netstat. It handles almost all network-related tasks, from viewing interfaces to modifying routing tables.

Key Subcommands:

  • ip addr: View/modify IP addresses of interfaces.
  • ip link: Manage network interfaces (e.g., enable/disable).
  • ip route: View/modify routing tables.
  • ip neigh: View the ARP (Address Resolution Protocol) table (maps IPs to MAC addresses).

Examples:

  1. List all interfaces and their IP addresses:

    ip addr show  

    Output includes interface names (e.g., eth0 for wired, wlan0 for Wi-Fi), MAC addresses, and IPv4/IPv6 addresses.

  2. Enable/disable an interface (replace eth0 with your interface):

    sudo ip link set eth0 up   # Enable  
    sudo ip link set eth0 down # Disable  
  3. Assign a temporary IP address (resets after reboot):

    sudo ip addr add 192.168.1.100/24 dev eth0  

    (Here, /24 is the subnet mask, equivalent to 255.255.255.0.)

ifconfig: The Legacy Utility (Optional)

ifconfig is deprecated in most modern Linux distributions but still works on older systems. Use it to view/set IP addresses, but prefer ip for new setups.

Example:

ifconfig eth0  # View settings for eth0  
sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0  # Assign IP  

hostname: View/Set System Hostname

The hostname command shows the system’s hostname. To set it permanently (systemd-based systems), use hostnamectl.

Examples:

hostname  # View current hostname  
sudo hostnamectl set-hostname "my-server"  # Set new hostname (persistent)  

ss: Socket Statistics

ss replaces netstat for viewing open ports and socket connections. It’s faster and more feature-rich.

Example: List all listening TCP/UDP ports

ss -tuln  
  • -t: TCP ports
  • -u: UDP ports
  • -l: Listening ports
  • -n: Show port numbers (instead of names like http).

Connectivity Testing Commands

Once you know your network settings, test if you can communicate with other devices or servers.

ping: Test Network Reachability

ping sends ICMP echo requests to a target IP/hostname to check if it’s reachable.

Examples:

ping google.com          # Continuous pings (Ctrl+C to stop)  
ping -c 4 8.8.8.8        # Send 4 pings (Google DNS)  
ping -i 2 -c 5 example.com  # Ping every 2 seconds, 5 times total  

traceroute: Trace Packet Path

traceroute shows the path packets take from your system to a target, including intermediate routers.

Example:

traceroute google.com  

Output lists each hop (router) with IP addresses and response times.

mtr: Combine ping and traceroute

mtr (My Traceroute) combines ping and traceroute into a single tool, providing real-time latency and packet loss data.

Example:

mtr google.com  

curl/wget: Test HTTP Connectivity

Use curl or wget to test if a web server responds.

Examples:

curl -I https://google.com  # Show HTTP headers (e.g., status code 200 = OK)  
wget https://example.com/file.txt  # Download a file (tests connectivity)  

DNS Configuration and Troubleshooting

DNS (Domain Name System) translates human-readable hostnames (e.g., google.com) to IP addresses. These commands help debug DNS issues.

/etc/resolv.conf: DNS Resolver Configuration

The /etc/resolv.conf file lists DNS servers your system uses to resolve hostnames.

Example:

cat /etc/resolv.conf  

Output might look like:

nameserver 8.8.8.8  # Google DNS  
nameserver 8.8.4.4  

Note: On systems with NetworkManager or systemd-resolved, this file may be auto-generated. To edit manually, disable auto-generation first (e.g., sudo systemctl stop systemd-resolved).

dig: Domain Information Groper

dig is a powerful tool to query DNS records (A, CNAME, MX, etc.).

Examples:

  1. Basic DNS lookup (A record):

    dig google.com  

    Look for the ANSWER SECTION to see the IP address.

  2. Query a specific DNS server (e.g., Cloudflare DNS 1.1.1.1):

    dig @1.1.1.1 google.com  

nslookup: DNS Lookup Utility

nslookup is simpler than dig for basic DNS queries.

Example:

nslookup google.com  

Network Interface Configuration

To permanently configure IP addresses, DNS, or Wi-Fi, use these tools instead of temporary ip commands.

nmcli: NetworkManager Command-Line Interface

nmcli controls NetworkManager, the default network manager on most Linux desktops and many servers. It handles dynamic (DHCP) and static IP setups.

Examples:

  1. List all connections (saved network profiles):

    nmcli con show  
  2. Edit a connection (e.g., set a static IP for eth0):

    sudo nmcli con mod "Wired connection 1" \  
      ipv4.addresses 192.168.1.100/24 \  
      ipv4.gateway 192.168.1.1 \  
      ipv4.dns "8.8.8.8,8.8.4.4" \  
      ipv4.method manual  

    Then restart the connection:

    sudo nmcli con up "Wired connection 1"  

netplan: YAML-Based Configuration (Ubuntu/Debian)

Netplan is used in Ubuntu 18.04+ and Debian 10+ for declarative network configuration via YAML files.

Step 1: Edit the Netplan config file

Config files are in /etc/netplan/ (e.g., 01-network-manager-all.yaml).

Example for a static IP:

network:  
  version: 2  
  renderer: networkd  # Use systemd-networkd (not NetworkManager)  
  ethernets:  
    eth0:  
      addresses: [192.168.1.100/24]  
      gateway4: 192.168.1.1  
      nameservers:  
        addresses: [8.8.8.8, 8.8.4.4]  

Step 2: Apply the config

sudo netplan apply  

Routing Table Commands

Routing tables determine how packets are forwarded between networks. Use ip route to view or modify them.

ip route: View/Modify Routing Tables

Examples:

  1. List all routes (including the default gateway):

    ip route show  

    Output includes the default gateway (e.g., default via 192.168.1.1 dev eth0).

  2. Add a static route (send traffic for 10.0.0.0/24 via 192.168.1.2):

    sudo ip route add 10.0.0.0/24 via 192.168.1.2 dev eth0  
  3. Delete a route:

    sudo ip route del 10.0.0.0/24  

Firewall Configuration Basics

Firewalls control incoming/outgoing traffic. Use ufw (Uncomplicated Firewall) for simplicity, or iptables for advanced rules.

ufw: Uncomplicated Firewall

ufw is a user-friendly frontend for iptables.

Examples:

  1. Enable UFW (starts on boot):

    sudo ufw enable  
  2. Allow SSH (port 22) (critical for remote servers):

    sudo ufw allow 22/tcp  
  3. Allow HTTP/HTTPS (ports 80/443):

    sudo ufw allow 80/tcp  
    sudo ufw allow 443/tcp  
  4. Check status:

    sudo ufw status verbose  

Conclusion

Linux network configuration commands are powerful yet approachable once you understand their purpose. From inspecting interfaces with ip addr to securing servers with ufw, these tools form the foundation of network management.

Start small: practice viewing settings with ip addr and ss, test connectivity with ping, and gradually move to configuration with nmcli or netplan. With time, you’ll master troubleshooting and automation of network tasks.

References