Complete Guide to Linux Networking: Commands, FAQ, Configuration, and Troubleshooting

Table of Contents

Essential Network Commands

Linux provides a comprehensive set of command-line tools for network management and diagnostics. Here are the most important ones:

1. ip Command (Modern replacement for ifconfig)

The ip command is the modern approach to network configuration and is part of the iproute2 package.

# Display all network interfaces
ip addr show
ip addr show eth0

# Add an IP address
sudo ip addr add 192.168.1.100/24 dev eth0

# Remove an IP address
sudo ip addr del 192.168.1.100/24 dev eth0

# Show routing table
ip route show

# Add a route
sudo ip route add 192.168.2.0/24 via 192.168.1.1

# Display network statistics
ip -s link

# Show interface statistics
ip -s addr

2. ifconfig Command (Legacy)

While considered legacy, ifconfig is still widely used for quick network interface checks.

# Display all network interfaces
ifconfig

# Display specific interface
ifconfig eth0

# Bring interface up/down
sudo ifconfig eth0 up
sudo ifconfig eth0 down

# Assign IP address
sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0

# Set MAC address
sudo ifconfig eth0 hw ether 00:11:22:33:44:55

3. ping Command

Test connectivity to remote hosts.

# Basic ping
ping 8.8.8.8

# Ping with count limit
ping -c 4 google.com

# Ping with timeout
ping -w 5000 google.com

# Change packet size
ping -s 1500 google.com

# Flood ping (use carefully)
sudo ping -f google.com

4. traceroute and tracepath Commands

Trace the route packets take to reach a destination.

# Using traceroute
traceroute google.com

# Using tracepath (doesn't require root)
tracepath google.com

# Specify maximum hops
traceroute -m 10 google.com

5. netstat Command

Display network statistics and connections.

# List all listening ports
netstat -tuln

# List all connections
netstat -tuan

# Show statistics for each protocol
netstat -s

# Display process information with connections
netstat -tulnp

# Show interface statistics
netstat -i

# Monitor network continuously
netstat -c

6. ss Command (Modern replacement for netstat)

# List all listening sockets
ss -tuln

# Display all TCP connections
ss -t

# Show UDP sockets
ss -u

# Show statistics
ss -s

# Display processes using sockets
ss -tulnp

7. nslookup and dig Commands

DNS query tools for network troubleshooting.

# Basic DNS lookup
nslookup google.com

# Query specific DNS server
nslookup google.com 8.8.8.8

# Reverse DNS lookup
nslookup 8.8.8.8

# Using dig for detailed DNS information
dig google.com

# Query specific record type
dig google.com MX

# Reverse DNS with dig
dig -x 8.8.8.8

8. host Command

Simple DNS lookup utility.

# Basic DNS lookup
host google.com

# Query specific DNS server
host google.com 8.8.8.8

# Verbose output
host -v google.com

9. curl and wget Commands

Download files and test HTTP connectivity.

# Download file with curl
curl -O <https://example.com/file.tar.gz>

# Download with wget
wget <https://example.com/file.tar.gz>

# Test HTTP status
curl -I <https://google.com>

# Test connectivity with timeout
curl -m 5 <https://google.com>

10. arp Command

Address Resolution Protocol – map IP addresses to MAC addresses.

See also: Mastering the Linux Command Line — Your Complete Free Training Guide

# Display ARP table
arp -a

# Display ARP table for specific interface
arp -i eth0

# Add static ARP entry
sudo arp -s 192.168.1.50 00:11:22:33:44:55

# Remove ARP entry
sudo arp -d 192.168.1.50

11. route Command

View and manipulate routing table.

# Display routing table
route -n

# Add route
sudo route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.1

# Add default route
sudo route add default gw 192.168.1.1

# Delete route
sudo route del -net 192.168.2.0 netmask 255.255.255.0

12. ifup and ifdown Commands

Bring network interfaces up and down.

# Bring interface up
sudo ifup eth0

# Bring interface down
sudo ifdown eth0

# Restart interface
sudo ifdown eth0 && sudo ifup eth0

13. hostname Command

View or set system hostname.

# Display current hostname
hostname

# Display FQDN
hostname -f

# Display domain name
hostname -d

# Set hostname (temporary)
sudo hostname newname

# Set hostname permanently (edit /etc/hostname)

14. dhclient Command

DHCP client for obtaining IP addresses.

# Obtain IP via DHCP
sudo dhclient eth0

# Release DHCP lease
sudo dhclient -r eth0

# Renew DHCP lease
sudo dhclient -r && sudo dhclient eth0

15. nmcli Command (NetworkManager)

Command-line interface for NetworkManager.

# Display network connections
nmcli connection show

# Display devices
nmcli device show

# Connect to network
nmcli connection up connection_name

# Disconnect from network
nmcli connection down connection_name

# Create new connection
nmcli connection add type ethernet ifname eth0 con-name my-eth0


Network FAQ in Linux

Frequently Asked Questions About Linux Networking

Q1: What’s the difference between ifconfig and ip command?

A: ifconfig is a legacy command that is being deprecated, while ip is the modern, more powerful replacement. The ip command provides more features and better control over network configuration. Modern distributions encourage using ip instead of ifconfig.

# Legacy way (ifconfig)
ifconfig eth0 192.168.1.100

# Modern way (ip)
ip addr add 192.168.1.100/24 dev eth0

Q2: How can I check if my internet connection is working?

A: Use the ping command to test connectivity:

ping 8.8.8.8          # Ping Google's DNS
ping google.com       # Ping by domain name

Q3: How do I view open ports and listening services?

A: Use ss or netstat commands:

# Modern way
ss -tuln

# Legacy way
netstat -tuln

# With process names
sudo ss -tulnp

Q4: How do I configure a static IP address?

A: Edit the network configuration files (varies by distribution):

For Debian/Ubuntu:

sudo nano /etc/network/interfaces

For RHEL/CentOS/Fedora:

sudo nano /etc/sysconfig/network-scripts/ifcfg-eth0

Using NetworkManager (GUI/CLI):

nmcli connection modify eth0 ipv4.method manual ipv4.addresses 192.168.1.100/24
nmcli connection up eth0

Q5: How do I change my DNS servers?

A: Edit /etc/resolv.conf or network configuration files:

sudo nano /etc/resolv.conf

Add:

nameserver 8.8.8.8
nameserver 8.8.4.4

Or in /etc/network/interfaces (Debian/Ubuntu):

dns-nameservers 8.8.8.8 8.8.4.4

Q6: How do I add a default gateway?

A: Using the ip command:

sudo ip route add default via 192.168.1.1

Or in configuration files (varies by distribution).

Q7: How do I check my MAC address?

A: Use ip or ifconfig:

ip link show          # Modern way
ifconfig              # Legacy way

Q8: How do I enable IP forwarding?

A: Edit /etc/sysctl.conf:

sudo nano /etc/sysctl.conf

Uncomment or add:

net.ipv4.ip_forward=1

Apply changes:

sudo sysctl -p

Q9: What’s the difference between TCP and UDP?

A:

  • TCP (Transmission Control Protocol): Connection-oriented, reliable, slower, used for HTTP, FTP, SSH
  • UDP (User Datagram Protocol): Connectionless, faster but unreliable, used for DNS, streaming, VoIP

Q10: How do I find which process is using a specific port?

A: Use ss or lsof:

sudo ss -tulnp | grep :8080
sudo lsof -i :8080

Q11: How do I set up a VPN on Linux?

A: This depends on the VPN type. Common approaches:

# OpenVPN
sudo openvpn --config /path/to/config.ovpn

# Using NetworkManager
nmcli connection add type vpn ifname vpn0 con-name my-vpn vpn-type openvpn

Q12: How do I check network interface statistics?

A: Use ip command:

ip -s link              # Interface statistics
ip -s addr              # Address statistics

Q13: How do I bond multiple network interfaces?

A: Create bond interface in network configuration or use NetworkManager:

nmcli connection add type bond ifname bond0 con-name bond0
nmcli connection add type ethernet ifname eth0 master bond0
nmcli connection add type ethernet ifname eth1 master bond0

Q14: How do I configure VLAN?

A: Create VLAN interface:

# Create VLAN interface
sudo ip link add link eth0 name eth0.100 type vlan id 100

# Assign IP to VLAN
sudo ip addr add 192.168.100.1/24 dev eth0.100

# Bring up VLAN
sudo ip link set eth0.100 up

Q15: How do I monitor real-time network traffic?

A: Use iftop or nethogs:

sudo iftop           # Real-time bandwidth usage
sudo nethogs         # Per-process bandwidth usage


Network Configuration Files in Linux

Understanding Network Configuration

Network configuration files vary by Linux distribution. This section covers the most common ones.

Debian/Ubuntu – /etc/network/interfaces

# Static IP configuration
auto eth0
iface eth0 inet static
    address 192.168.1.100
    netmask 255.255.255.0
    gateway 192.168.1.1
    dns-nameservers 8.8.8.8 8.8.4.4

# DHCP configuration
auto eth1
iface eth1 inet dhcp

# IPv6 configuration
auto eth0
iface eth0 inet6 static
    address 2001:db8::1
    netmask 64
    gateway 2001:db8::ff

Netplan (Ubuntu 18.04+): /etc/netplan/

network:
  version: 2
  ethernets:
    eth0:
      dhcp4: true
    eth1:
      addresses:
      - 192.168.1.100/24
      gateway4: 192.168.1.1
      nameservers:
        addresses: [8.8.8.8, 8.8.4.4]

RHEL/CentOS/Fedora – /etc/sysconfig/network-scripts/

File: /etc/sysconfig/network-scripts/ifcfg-eth0

# Static IP
TYPE=Ethernet
BOOTPROTO=static
ONBOOT=yes
NAME=eth0
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4

# DHCP
TYPE=Ethernet
BOOTPROTO=dhcp
ONBOOT=yes
NAME=eth0

/etc/resolv.conf – DNS Configuration

# DNS server configuration (often auto-generated)
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 1.1.1.1

Note: On systems using NetworkManager or systemd-resolved, this file may be auto-managed.

/etc/hostname – System Hostname

# Set system hostname
myserver.example.com

/etc/hosts – Local Hostname Resolution

# IP address    Hostname                Aliases
127.0.0.1       localhost               localhost.localdomain
192.168.1.100   myserver.example.com    myserver
192.168.1.101   webserver.example.com   webserver

/etc/sysctl.conf – Network Kernel Parameters

# IP forwarding
net.ipv4.ip_forward = 1

# Enable source packet routing
net.ipv4.conf.all.send_redirects = 1

# TCP parameters
net.ipv4.tcp_syn_retries = 5
net.ipv4.tcp_fin_timeout = 30

# Connection tracking
net.netfilter.nf_conntrack_max = 2000000

Apply changes:

sudo sysctl -p

NetworkManager Configuration

File: /etc/NetworkManager/NetworkManager.conf

[main]
plugins=ifcfg-rh,ibft,keyfile
dns=systemd-resolved
dhcp=dhclient

Connection Profiles: /etc/NetworkManager/system-connections/

Example connection file:

[connection]
id=Wired connection 1
uuid=550e8400-e29b-41d4-a716-446655440000
type=802-3-ethernet

[ipv4]

method=auto

[ipv6]

method=auto

Firewall Configuration – /etc/sysconfig/iptables (older systems)

# Accept established connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# Accept SSH
-A INPUT -p tcp --dport 22 -j ACCEPT

# Accept HTTP/HTTPS
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT

UFW (Uncomplicated Firewall) – Debian/Ubuntu

# Enable UFW
sudo ufw enable

# Allow SSH
sudo ufw allow 22/tcp

# Allow HTTP/HTTPS
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

# Deny access
sudo ufw deny 3306


Troubleshooting Network Issues in Linux

Systematic Network Troubleshooting Guide

When facing network issues, follow this systematic approach:

Step 1: Check Physical Connection

# Check if interface is up
ip link show
ip link show eth0

# Output should show "UP" state
# Example: 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500

Step 2: Verify IP Configuration

# Check IP address assignment
ip addr show
ip addr show eth0

# Check if using DHCP or static
cat /etc/network/interfaces      # Debian/Ubuntu
cat /etc/sysconfig/network-scripts/ifcfg-eth0  # RHEL/CentOS

Common Issue: No IP Address

# Try to obtain IP via DHCP
sudo dhclient eth0

# If DHCP fails, check DHCP service
sudo systemctl status dhcpd       # RHEL/CentOS
sudo systemctl status isc-dhcp-server  # Debian/Ubuntu

Step 3: Test Connectivity

# Test localhost
ping 127.0.0.1

# Test default gateway
ping 192.168.1.1

# Test external connectivity
ping 8.8.8.8
ping google.com

Troubleshooting Ping Failures:

# Check if ICMP is blocked by firewall
sudo iptables -L -n | grep ICMP

# Temporarily allow ICMP
sudo iptables -A INPUT -p icmp -j ACCEPT

# Check firewall status
sudo firewall-cmd --list-all      # firewalld
sudo ufw status                    # UFW

Step 4: Check Routing

# Display routing table
ip route show
route -n

# Test route to specific network
traceroute google.com
tracepath google.com

# Check specific route
ip route get 8.8.8.8

Common Issue: No Default Route

# Add default route
sudo ip route add default via 192.168.1.1

# Or make it persistent (add to config files)

Step 5: Verify DNS Resolution

# Test DNS
nslookup google.com
dig google.com
host google.com

# Check configured DNS servers
cat /etc/resolv.conf

# Test specific DNS server
nslookup google.com 8.8.8.8

Troubleshooting DNS Issues:

# Flush DNS cache (if running systemd-resolved)
sudo systemctl restart systemd-resolved

# Test with specific DNS server
sudo nano /etc/resolv.conf
# Add: nameserver 8.8.8.8

# Restart networking service
sudo systemctl restart networking  # Debian/Ubuntu
sudo systemctl restart network     # RHEL/CentOS

Step 6: Check Ports and Services

# List listening ports
ss -tuln
sudo netstat -tulnp

# Check if specific service is running
sudo systemctl status httpd        # Apache
sudo systemctl status nginx        # Nginx
sudo systemctl status sshd         # SSH

Troubleshooting Service Issues:

# Start service
sudo systemctl start nginx

# Check service logs
sudo systemctl status nginx
journalctl -u nginx -n 50

# Check port binding
sudo lsof -i :80
sudo ss -tlnp | grep :80

Step 7: Check Firewall Rules

# UFW status
sudo ufw status verbose

# iptables rules
sudo iptables -L -n
sudo iptables -L -n -v

# firewalld status
sudo firewall-cmd --list-all

# Check specific port
sudo firewall-cmd --query-port=80/tcp

Troubleshooting Firewall Issues:

# Allow port through firewall
sudo ufw allow 8080/tcp
sudo firewall-cmd --add-port=8080/tcp --permanent
sudo firewall-cmd --reload

# Or allow service
sudo ufw allow http
sudo firewall-cmd --add-service=http --permanent

Step 8: Check Network Interface Configuration

# Detailed interface information
ip addr show eth0
ip link show eth0
ethtool eth0

# Check MTU size (should be 1500 for Ethernet)
ip link show eth0 | grep mtu

# Change MTU if needed
sudo ip link set eth0 mtu 9000

Common Network Issues and Solutions

Issue 1: “No internet connection”

# Step-by-step diagnosis
ping 127.0.0.1           # Test localhost
ping 192.168.1.1         # Test gateway
ping 8.8.8.8             # Test internet
ping google.com          # Test DNS

# If localhost works but gateway doesn't:
sudo ip route show       # Check routes
sudo ip route add default via 192.168.1.1

# If gateway works but internet doesn't:
nslookup google.com      # Check DNS
cat /etc/resolv.conf

Issue 2: “Slow network speed”

# Check network load
iftop                    # View bandwidth usage
nethogs                  # Per-process bandwidth

# Check for packet loss
ping -c 100 google.com   # Look at % packet loss

# Check network interface statistics
ip -s link show eth0

# Monitor network in real-time
watch -n 1 'ip -s link show eth0'

Issue 3: “Cannot resolve domain names”

# Test DNS resolution
nslookup google.com
dig google.com

# Check if systemd-resolved is running
systemctl status systemd-resolved

# Restart DNS
sudo systemctl restart systemd-resolved

# Check /etc/resolv.conf
cat /etc/resolv.conf

# Test with specific DNS server
nslookup google.com 1.1.1.1

Issue 4: “Connection refused”

# Check if service is running
sudo systemctl status nginx
sudo ps aux | grep nginx

# Check if port is listening
sudo ss -tlnp | grep :80
sudo lsof -i :80

# Check firewall
sudo iptables -L -n
sudo ufw status

# Try to connect
telnet localhost 80

Issue 5: “Intermittent connectivity”

# Monitor connection continuously
ping -c 1000 google.com

# Check for duplicate IP addresses
arp -a                 # Look for duplicate MAC addresses

# Check interface errors
ethtool -S eth0
ip -s link show eth0

# Look for ARP conflicts
sudo tcpdump -i eth0 -n arp

Issue 6: “DHCP not working”

# Check DHCP service status
sudo systemctl status isc-dhcp-server  # Debian/Ubuntu
sudo systemctl status dhcpd             # RHEL/CentOS

# Manually request DHCP lease
sudo dhclient -v eth0

# Check DHCP client logs
sudo journalctl -u dhclient -n 50

# Restart networking
sudo systemctl restart networking

Issue 7: “IP configuration not persisting”

# Check if changes are saved in config files
cat /etc/network/interfaces      # Debian/Ubuntu
cat /etc/sysconfig/network-scripts/ifcfg-eth0  # RHEL/CentOS

# Use netplan for Ubuntu 18.04+
sudo nano /etc/netplan/01-netcfg.yaml
sudo netplan apply

# Restart networking service
sudo systemctl restart networking

Advanced Troubleshooting Tools

# tcpdump - Capture and analyze packets
sudo tcpdump -i eth0 -n
sudo tcpdump -i eth0 -n host 192.168.1.100

# wireshark - GUI packet analyzer
sudo wireshark

# mtr - Continuous traceroute
mtr google.com

# iperf - Network bandwidth testing
iperf -s           # Server mode
iperf -c server_ip # Client mode

# nc/ncat - Network troubleshooting
nc -zv google.com 80   # Check if port is open

# netstat/ss detailed monitoring
ss -tan             # TCP sockets
ss -uan             # UDP sockets
ss -pan             # All with process info

Network Troubleshooting Checklist

# Quick diagnostic script
#!/bin/bash
echo "=== Network Troubleshooting Checklist ==="
echo ""
echo "1. Interface Status:"
ip link show

echo ""
echo "2. IP Configuration:"
ip addr show

echo ""
echo "3. Routing Table:"
ip route show

echo ""
echo "4. DNS Servers:"
cat /etc/resolv.conf

echo ""
echo "5. Ping Gateway:"
ping -c 3 192.168.1.1

echo ""
echo "6. Listening Ports:"
ss -tuln

echo ""
echo "7. Firewall Status:"
sudo iptables -L -n

echo ""
echo "8. Network Statistics:"
ip -s link show


Conclusion

Linux networking is a vast topic with many tools and configurations available. Key takeaways:

  1. Use modern tools: Prefer ip over ifconfig, use ss instead of netstat
  2. Know your distribution: Configuration files vary between Debian/Ubuntu, RHEL/CentOS, etc.
  3. Troubleshoot systematically: Follow a step-by-step approach from physical layer to applications
  4. Monitor regularly: Use tools like iftop, nethogs, and systemd to keep tabs on network health
  5. Document changes: Keep records of network configuration modifications for future reference

For more information and man pages:

man ip
man ss
man ifconfig
man netstat
man ping
man nslookup

David Cao
David Cao

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

Articles: 546

Leave a Reply

Your email address will not be published. Required fields are marked *