Table of Contents
- Prerequisites
- Basic Network Information Commands
- Connectivity Testing Commands
- DNS Configuration and Troubleshooting
- Network Interface Configuration
- Routing Table Commands
- Firewall Configuration Basics
- Conclusion
- 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).
sudoprivileges 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:
-
List all interfaces and their IP addresses:
ip addr showOutput includes interface names (e.g.,
eth0for wired,wlan0for Wi-Fi), MAC addresses, and IPv4/IPv6 addresses. -
Enable/disable an interface (replace
eth0with your interface):sudo ip link set eth0 up # Enable sudo ip link set eth0 down # Disable -
Assign a temporary IP address (resets after reboot):
sudo ip addr add 192.168.1.100/24 dev eth0(Here,
/24is the subnet mask, equivalent to255.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 likehttp).
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:
-
Basic DNS lookup (A record):
dig google.comLook for the
ANSWER SECTIONto see the IP address. -
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:
-
List all connections (saved network profiles):
nmcli con show -
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 manualThen 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:
-
List all routes (including the default gateway):
ip route showOutput includes the default gateway (e.g.,
default via 192.168.1.1 dev eth0). -
Add a static route (send traffic for
10.0.0.0/24via192.168.1.2):sudo ip route add 10.0.0.0/24 via 192.168.1.2 dev eth0 -
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:
-
Enable UFW (starts on boot):
sudo ufw enable -
Allow SSH (port 22) (critical for remote servers):
sudo ufw allow 22/tcp -
Allow HTTP/HTTPS (ports 80/443):
sudo ufw allow 80/tcp sudo ufw allow 443/tcp -
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.