Linux Network Commands Guide

Network Commands

Commands for checking, configuring, and diagnosing network connections.

ping - Check Network Connectivity

Checks connectivity with a specified host.

Option Description
-c N Send only N pings and exit
-i N Send pings at N-second intervals
-s N Set packet size to N bytes
-t N Set TTL (Time To Live) value to N
-w N Set timeout to N seconds

Examples:

ping google.com - Continuously ping Google's servers
ping -c 5 192.168.1.1 - Send 5 pings to the specified IP address
ping -i 2 -c 10 example.com - Send 10 pings at 2-second intervals
ping -c 5 google.com | grep "time=" - Display only response times
ping -c 10 google.com | grep "time=" | awk '{print $7}' | cut -d "=" -f 2 - Extract only the response time values
ping -c 5 google.com | tail -1 | awk '{print $4}' | cut -d '/' -f 2 - Extract the average response time
for ip in 192.168.1.{1..10}; do ping -c 1 -W 1 $ip | grep "from" && echo "$ip is up"; done - Ping multiple IP addresses
ping -c 5 -q google.com | grep "packet loss" | awk '{print $6}' - Extract packet loss percentage
ping -c 1 -W 1 google.com > /dev/null 2>&1 && echo "Internet is up" || echo "Internet is down" - Check internet connectivity
ping -c 5 -s 1500 google.com - Check MTU with large packet size
ping -c 5 google.com | tee ping_results.txt - Save results to a file while displaying them

ifconfig - Configure Network Interfaces

Displays or configures network interface information.

Note: The ip command is recommended in recent Linux distributions.

Syntax/Option Description
ifconfig Display information for all active interfaces
ifconfig -a Display information for all interfaces (including inactive ones)
ifconfig [interface] Display information for a specific interface
ifconfig [interface] up/down Enable/disable an interface
ifconfig [interface] [ip] netmask [mask] Set IP address and netmask

Examples:

ifconfig - Display information for all active network interfaces
ifconfig eth0 - Display information for the eth0 interface
sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0 - Set IP address and netmask for eth0
ifconfig | grep -A 1 "inet " - Display IPv4 addresses for all interfaces
ifconfig | grep -o -E 'inet ([0-9]{1,3}\.){3}[0-9]{1,3}' | cut -d' ' -f2 - Extract only IP addresses
ifconfig | grep "ether" | awk '{print $2}' - Extract MAC addresses for all interfaces
ifconfig eth0 | grep "RX packets" | awk '{print "Received: " $3 " packets, " $5 " bytes"}' - Format received packet and byte counts
ifconfig eth0 | grep "TX packets" | awk '{print "Transmitted: " $3 " packets, " $5 " bytes"}' - Format transmitted packet and byte counts
for i in $(ifconfig | grep -o '^[a-zA-Z0-9]*:' | sed 's/://'); do echo "===$i==="; ifconfig $i | grep "inet "; done - Format IP addresses for each interface
ifconfig | grep "mtu" | awk '{print $1, "MTU:", $NF}' - Display MTU values for each interface
ifconfig eth0 | grep "errors" | awk '{print "RX errors: " $3 ", TX errors: " $7}' - Display receive and transmit error counts

ip - Configure IP Networking

Configures and manages IP addresses, routing, network devices, etc.

Subcommand Description
ip addr Display/configure IP addresses
ip link Display/configure network interfaces
ip route Display/configure routing table
ip neigh Display/configure neighbor table (ARP table)
ip tunnel Display/configure tunnels

Examples:

ip addr show - Display IP address information for all interfaces
ip link set eth0 up - Enable the eth0 interface
ip route show - Display routing table
sudo ip addr add 192.168.1.100/24 dev eth0 - Add an IP address to eth0

netstat - Display Network Connections, Routing Tables, Interface Statistics

Displays network connections, routing tables, interface statistics, etc.

Note: The ss command is recommended in recent Linux distributions.

Option Description
-a Display all connections (including listening sockets)
-t Display TCP connections only
-u Display UDP connections only
-n Display numerical addresses and ports
-p Display process ID and program name
-r Display routing table
-i Display network interface statistics

Examples:

netstat -tuln - Display listening TCP and UDP ports numerically
netstat -anp - Display all connections with associated processes
netstat -r - Display routing table

ss - Display Socket Statistics

Displays socket statistics. Used as a replacement for netstat.

Option Description
-a Display all sockets (including listening sockets)
-t Display TCP sockets only
-u Display UDP sockets only
-n Display numerical addresses and ports
-p Display process using the socket
-l Display listening sockets only
-i Display internal TCP information

Examples:

ss -tuln - Display listening TCP and UDP ports numerically
ss -anp - Display all sockets with associated processes
ss -i - Display internal TCP information

wget - Download Files from Network

Downloads files using HTTP, HTTPS, FTP, and other protocols.

Option Description
-O [file] Save downloaded file with specified name
-c Resume downloading a partially downloaded file
-b Download in background
-r Download recursively
-P [dir] Save files to specified directory
--limit-rate=[rate] Limit download speed (e.g., 200k)

Examples:

wget https://example.com/file.zip - Download a file
wget -O output.zip https://example.com/file.zip - Save file with specified name
wget -c https://example.com/large-file.iso - Resume interrupted download

curl - Data Transfer Tool

Transfers data using various protocols.

Option Description
-o [file] Save output to specified file
-O Save with same name as remote file
-L Follow redirects
-I, --head Fetch HTTP headers only
-X [method] Specify HTTP request method (GET, POST, PUT, DELETE, etc.)
-d, --data [data] Send POST data
-H [header] Specify HTTP header

Examples:

curl https://example.com - Display webpage content
curl -o page.html https://example.com - Save webpage to specified file
curl -I https://example.com - Fetch HTTP headers only
curl -X POST -d "name=value" https://example.com/api - Send POST request
curl -s https://example.com | grep "title" | sed 's/<[^>]*>//g' - Extract title from webpage and remove HTML tags
curl -s https://api.github.com/users/username | jq '.name, .location' - Fetch JSON data from GitHub API and extract specific fields
curl -u username:password https://example.com/protected - Access using Basic authentication
curl -H "Authorization: Bearer token123" https://api.example.com - Access API using Bearer token
curl -F "file=@document.pdf" https://example.com/upload - Upload file (multipart/form-data)
curl -s -w "\nHTTP Status: %{http_code}\nTotal time: %{time_total}s\n" https://example.com -o /dev/null - Display performance statistics
curl --limit-rate 200K https://example.com/large-file.iso -o large-file.iso - Limit download speed
for i in {1..5}; do curl -s -o /dev/null -w "%{http_code}\n" https://example.com; done - Send multiple requests and check HTTP status codes
curl -s -L https://bit.ly/shorturl - Follow redirects to get final destination of a short URL

traceroute - Trace Packet Route

Displays the route packets take to reach a destination.

Option Description
-n Display IP addresses numerically (no name resolution)
-w [seconds] Set timeout for responses
-m [max_ttl] Set maximum TTL (hop count)
-p [port] Specify port number to use

Examples:

traceroute google.com - Trace route to Google servers
traceroute -n 8.8.8.8 - Trace route without name resolution
traceroute -m 15 example.com - Trace route with maximum 15 hops
traceroute -T -p 443 example.com - TCP traceroute on port 443
traceroute -I example.com - Use ICMP protocol for traceroute
traceroute -4 example.com - Use IPv4 only for traceroute
traceroute -w 1 example.com - Set response wait time to 1 second (faster)
traceroute example.com | grep -v "\*\*\*" - Exclude timed-out hops
traceroute example.com | awk '{print $2}' - Extract only hop IP addresses or hostnames
traceroute -q 1 example.com | grep -v "\*" | awk '{print $2}' | xargs -n 1 whois | grep "Country" - Display country information for IPs in route
for ip in 8.8.8.8 1.1.1.1; do echo "=== $ip ==="; traceroute -n -w 1 $ip | tail -n +2 | head -n 5; done - Compare first 5 hops to multiple destinations
traceroute example.com 2>&1 | tee traceroute_$(date +%Y%m%d_%H%M%S).log - Save traceroute results to timestamped log file

nslookup/dig - Perform DNS Queries

Queries DNS information for domain names or IP addresses.

Command/Option Description
nslookup [domain] Get IP address for domain
nslookup -type=[record_type] [domain] Get specific DNS record type (MX, NS, SOA, etc.)
dig [domain] Get detailed DNS information for domain
dig [domain] [record_type] Get specific DNS record type
dig +short [domain] Display concise results only

Examples:

nslookup google.com - Get Google's IP address
nslookup -type=MX example.com - Get mail server information for domain
dig google.com - Get detailed DNS information for Google
dig example.com MX - Get mail server information for domain
dig +short example.com - Display IP addresses only