Networking Commands

Networking is a core aspect of Linux system administration, enabling systems to communicate, transfer data, and connect to the internet or other networks. Whether you're configuring a server, troubleshooting network issues, or managing containers, a solid understanding of networking commands is essential. This guide covers the most important networking commands in Linux, providing the tools you need to monitor, configure, and troubleshoot network connections.

Basic Networking Commands

ifconfig - Interface Configuration

The ifconfig command displays or configures a network interface. While it has largely been replaced by ip, it's still useful for basic network management.

ifconfig
  • Common Uses:
    • Display network interfaces and their IP addresses.
    • Bring an interface up or down:
      ifconfig eth0 up
      ifconfig eth0 down

Want to learn more about configuring network interfaces on Linux?

ip - Show/Manipulate Routing, Devices, and Addresses

The ip command is the modern replacement for ifconfig, offering more functionality and flexibility.

ip a
  • Common Options:
    • ip a: Show all IP addresses.
    • ip link: Show network interfaces.
    • ip route: Display or modify the routing table.
    • ip addr add 192.168.1.10/24 dev eth0: Assign an IP address to an interface.
    • ip link set eth0 up: Bring an interface up.

Brush up on IP addresses.

ping - Test Network Connectivity

The ping command tests connectivity between your system and a remote host by sending ICMP echo requests and listening for replies.

ping example.com
  • Options:
    • -c X: Send X number of packets (e.g., ping -c 4 example.com).
    • -i X: Set the interval between packets (e.g., ping -i 0.5 example.com).

traceroute - Trace the Route to a Host

The traceroute command shows the path that packets take to reach a remote host, helping diagnose network routing issues.

traceroute example.com
  • Options:
    • -n: Display IP addresses instead of hostnames.
    • -m X: Set the maximum number of hops (e.g., traceroute -m 20 example.com).

netstat - Network Statistics

The netstat command provides detailed statistics about network connections, routing tables, and interface statistics.

netstat
  • Options:
    • -tuln: Display listening ports and their associated services.
    • -r: Show the routing table.

ss - Socket Statistics

The ss command is a modern replacement for netstat, offering more detailed information about sockets.

ss -tuln
  • Common Options:
    • -tuln: Display listening sockets (TCP and UDP).
    • -p: Show processes using sockets.

Read about more network diagnostic tools.

Network Configuration Commands

hostname - Show or Set the System's Hostname

The hostname command displays or sets the system's hostname.

hostname
  • Options:
    • hostnamectl set-hostname new-hostname: Set a new hostname.
    • hostname -I: Display all IP addresses associated with the hostname.

nmcli - NetworkManager Command Line Interface

nmcli is a command-line tool for managing NetworkManager, used to configure network interfaces and connections.

nmcli device status
  • Common Uses:
    • Show the status of all network interfaces:
      nmcli device status
    • Connect to a Wi-Fi network:
      nmcli device wifi connect "SSID" password "PASSWORD"

iwconfig - Configure Wireless Interfaces

The iwconfig command is used to configure wireless network interfaces and view wireless information.

iwconfig
  • Common Uses:
    • View wireless interface details such as SSID and signal strength.
    • Set wireless network parameters like SSID and mode.

route - Show/Manipulate the IP Routing Table

The route command displays or modifies the system's routing table.

route -n
  • Options:
    • route add default gw 192.168.1.1: Add a default gateway.
    • route del default gw 192.168.1.1: Remove a default gateway.

Troubleshooting and Diagnostics Commands

dig - DNS Lookup

The dig command queries DNS servers and retrieves information about DNS records. It's a powerful tool for troubleshooting DNS issues.

dig example.com
  • Common Options:
    • +short: Show a shortened output (e.g., only the IP address).
    • @dns-server: Query a specific DNS server.

nslookup - Query Internet Name Servers

The nslookup command queries DNS servers to find IP addresses associated with a domain name.

nslookup example.com
  • Usage:
    • nslookup: Enter interactive mode to perform multiple queries.

curl - Transfer Data from or to a Server

The curl command transfers data to or from a server, supporting a wide range of protocols including HTTP, FTP, and more.

curl http://example.com
  • Options:
    • -I: Fetch the HTTP headers only.
    • -O: Save the output to a file.

wget - Download Files from the Web

wget is a command-line utility to download files from the web, particularly useful for automated downloads.

wget http://example.com/file.zip
  • Options:
    • -c: Resume a partially downloaded file.
    • -q: Run in quiet mode (no output).

nc (Netcat) - Network Utility

Netcat (nc) is a versatile networking tool used for reading from and writing to network connections using TCP or UDP.

nc -zv example.com 80
  • Common Uses:
    • Port scanning: nc -zv example.com 80-90
    • Create a simple chat server: nc -l 1234 and nc hostname 1234 on another machine.

tcpdump - Network Packet Analyzer

The tcpdump command captures and analyzes network traffic on an interface. It's an essential tool for network diagnostics and security monitoring.

tcpdump -i eth0
  • Options:
    • -n: Display numeric addresses instead of resolving hostnames.
    • -c X: Capture X number of packets.
    • -w file.pcap: Save captured packets to a file for later analysis with tools like Wireshark.

ethtool - Display or Change Ethernet Device Settings

The ethtool command allows you to view and change settings for network interfaces.

ethtool eth0
  • Common Uses:
    • View the speed and duplex settings of an interface.
    • Modify settings such as auto-negotiation and wake-on-LAN.

Go deeper by learning nmap.