Network Basics (ping, wget, curl)
Category: Linux Command Basics
Type: Linux Commands
Generated on: 2025-07-10 03:07:23
For: System Administration, Development & Technical Interviews
Network Basics Cheatsheet (ping, wget, curl) - Linux Commands
Section titled “Network Basics Cheatsheet (ping, wget, curl) - Linux Commands”This cheatsheet provides a comprehensive guide to ping, wget, and curl, essential commands for network troubleshooting and data transfer in Linux.
1. Command Overview
ping: Tests network connectivity by sending ICMP “echo request” packets to a target host and waiting for “echo reply” packets. Used to verify if a host is reachable.wget: Downloads files from the web using HTTP, HTTPS, and FTP protocols. Useful for retrieving files, mirroring websites, and scheduling downloads.curl: A versatile command-line tool for transferring data with URLs. Supports a wide range of protocols (HTTP, HTTPS, FTP, SMTP, etc.) and is commonly used for interacting with APIs, testing web services, and downloading files.
2. Basic Syntax
-
ping:Terminal window ping [options] <hostname or IP address> -
wget:Terminal window wget [options] <URL> -
curl:Terminal window curl [options] <URL>
3. Practical Examples
-
ping:-
Basic connectivity test:
Terminal window ping google.comPING google.com (142.250.184.142) 56(84) bytes of data.64 bytes from fra16s31-in-f14.1e100.net (142.250.184.142): icmp_seq=1 ttl=117 time=7.31 ms64 bytes from fra16s31-in-f14.1e100.net (142.250.184.142): icmp_seq=2 ttl=117 time=7.25 ms^C--- google.com ping statistics ---2 packets transmitted, 2 received, 0% packet loss, time 1001msrtt min/avg/max/mdev = 7.253/7.282/7.312/0.029 ms -
Ping a specific IP address:
Terminal window ping 8.8.8.8
-
-
wget:-
Download a file:
Terminal window wget https://example.com/file.txtThis will download
file.txtto the current directory. -
Download a file with a different name:
Terminal window wget -O new_file.txt https://example.com/file.txt
-
-
curl:-
Download a file and display its contents in the terminal:
Terminal window curl https://example.com/index.htmlThis will print the HTML source code of
index.htmlto the standard output. -
Download a file and save it to a file:
Terminal window curl -o index.html https://example.com/index.htmlThis will download
index.htmland save it asindex.htmlin the current directory. -
Make a simple GET request to an API:
Terminal window curl https://api.example.com/usersThis will retrieve user data from the API.
-
4. Common Options
-
ping:-c <count>: Stop after sending<count>ECHO_REQUEST packets. Example:ping -c 5 google.com(sends 5 pings).-i <interval>: Wait<interval>seconds between sending each packet. Example:ping -i 2 google.com(sends pings every 2 seconds).-s <size>: Specify the packet size. Example:ping -s 1024 google.com(sends 1024-byte packets).-t <ttl>: Set the Time To Live (TTL) value. Example:ping -t 64 google.com(sets TTL to 64).-w <deadline>: Specify a timeout, in seconds, beforepingexits regardless of how many packets have been sent or received.
-
wget:-O <filename>: Specify the output filename. Example:wget -O myfile.zip https://example.com/file.zip-q: Quiet mode (suppress output). Example:wget -q https://example.com/file.txt-c: Continue an interrupted download. Example:wget -c https://example.com/largefile.iso-b: Run in the background. Example:wget -b https://example.com/file.txt-r: Recursive download (download entire website). WARNING: Use with caution. Can consume significant bandwidth and storage. Example:wget -r https://example.com-l <level>: Set the recursion depth. Example:wget -r -l 2 https://example.com(download up to 2 levels deep).--limit-rate=<rate>: Limits the download rate. Example:wget --limit-rate=200k https://example.com/largefile.iso(limits to 200 KB/s)--no-check-certificate: Don’t verify the server’s certificate. Use with caution. Example:wget --no-check-certificate https://self-signed.example.com
-
curl:-o <filename>: Specify the output filename. Example:curl -o myfile.html https://example.com/index.html-O: Use the remote file name for saving the downloaded file. Example:curl -O https://example.com/myfile.zip-I: Get only the headers of the response. Example:curl -I https://example.com(useful for checking server status and headers).-v: Verbose mode (show more details about the transfer). Example:curl -v https://example.com-H <header>: Add a custom header to the request. Example:curl -H "Content-Type: application/json" https://api.example.com/data-X <method>: Specify the HTTP method (GET, POST, PUT, DELETE, etc.). Example:curl -X POST -d '{"key":"value"}' https://api.example.com/data-d <data>: Send data in a POST request. Example:curl -d "param1=value1¶m2=value2" https://api.example.com/submit--data-binary <data>: Send binary data in a POST request. Useful for sending files. Example:curl --data-binary @myfile.txt https://api.example.com/upload-u <user:password>: Specify the username and password for authentication. Example:curl -u user:password https://example.com/protected-k: Allow insecure server connections (don’t verify SSL certificates). WARNING: Use with caution. Can expose you to security risks. Example:curl -k https://self-signed.example.com--limit-rate <rate>: Limits the transfer rate. Example:curl --limit-rate 100k https://example.com/largefile.iso(limits to 100 KB/s)
5. Advanced Usage
-
ping:-
Scripting:
#!/bin/bashif ping -c 1 google.com > /dev/null 2>&1; thenecho "Google is reachable"elseecho "Google is not reachable"fi -
Measuring Latency:
Terminal window ping -c 10 google.com | awk '/time=/{print $7}' | cut -d '=' -f 2 | awk '{sum += $1; n++} END {if (n > 0) print sum / n}'This script calculates the average latency from 10 pings to google.com.
-
-
wget:-
Downloading multiple files from a list:
Create a file named
urls.txtwith a list of URLs, one URL per line.Terminal window wget -i urls.txt -
Mirroring a website (advanced):
Terminal window wget -mkEpnp https://example.com-m: Mirroring (recursive download with timestamping).-k: Convert links to be relative, for local viewing.-E: Append.htmlto HTML files.-p: Get all images, CSS, etc. needed to display HTML pages properly.-n: Use timestamping (don’t re-download files that haven’t changed).
WARNING: This can download a significant amount of data. Use with caution.
-
-
curl:-
Uploading a file using POST:
Terminal window curl -F "file=@myfile.txt" https://api.example.com/upload -
Sending JSON data to an API:
Terminal window curl -H "Content-Type: application/json" -X POST -d '{"key":"value"}' https://api.example.com/api_endpoint -
Following redirects:
Terminal window curl -L https://example.com/shorturl-Lfollows redirects. Useful for handling shortened URLs. -
Using cookies:
-
Saving cookies:
Terminal window curl -c cookies.txt https://example.com/login -
Sending cookies:
Terminal window curl -b cookies.txt https://example.com/protected
-
-
Using proxies:
Terminal window curl -x <proxy_address:port> https://example.comExample:
curl -x http://proxy.example.com:8080 https://example.com -
Using authentication with API keys:
Terminal window curl -H "Authorization: Bearer YOUR_API_KEY" https://api.example.com/resource
-
6. Tips & Tricks
-
ping:- Use
Ctrl+Cto stopping. - Pinging the gateway IP address is a quick way to check local network connectivity.
- Use
-
wget:- Combine
-band-cfor background downloads that can be resumed. - Use
nohup wget <URL> &for persistent background downloads that survive terminal closure. - Consider
--waitretryto retry failed downloads after a specified interval.
- Combine
-
curl:- Use
jqto pretty-print JSON responses:curl https://api.example.com | jq .(requiresjqto be installed). - Use
-wto get transfer information like time, speed, and size.Terminal window curl -s -w 'Time: %{time_total}s\nSpeed: %{speed_download} bytes/s\nSize: %{size_download} bytes\n' -o /dev/null https://example.com - For complex POST requests, create a JSON file and use
--data @filename.json.
- Use
7. Troubleshooting
-
ping:- “Destination Host Unreachable”: The target host is not reachable from your network. Check network configuration, DNS resolution, and firewall rules.
- “Request timed out”: The target host is reachable, but not responding to ICMP requests within the timeout period. This could be due to a firewall blocking ICMP, network congestion, or the host being overloaded.
- If pinging a hostname fails, but pinging the IP address works, there’s likely a DNS resolution issue. Check your
/etc/resolv.conffile.
-
wget:- “Connection refused”: The target server is refusing the connection. This could be due to the server being down, a firewall blocking the connection, or an incorrect port number.
- “404 Not Found”: The requested resource does not exist on the server. Double-check the URL.
- “Certificate verification failed”: The SSL certificate of the server is not trusted. Use
--no-check-certificate(with caution) or ensure that the CA certificate is installed on your system. - “Failed to write to disk”: Check disk space and permissions.
-
curl:- “curl: (7) Failed to connect to host”: The target host is unreachable. Check network connectivity, DNS resolution, and firewall rules. Similar to
ping’s “Destination Host Unreachable”. - “curl: (60) SSL certificate problem”: The SSL certificate of the server is not trusted. Use
-k(with caution) or ensure that the CA certificate is installed on your system. - “400 Bad Request”: The server cannot understand the request due to invalid syntax or parameters. Check the request format, headers, and data.
- “401 Unauthorized”: Authentication is required. Provide the correct credentials using
-uor appropriate API key headers. - “403 Forbidden”: The server understands the request, but refuses to fulfill it. This usually indicates that the client does not have the necessary permissions.
- If getting intermittent errors, check the server’s logs for more details.
- “curl: (7) Failed to connect to host”: The target host is unreachable. Check network connectivity, DNS resolution, and firewall rules. Similar to
8. Related Commands
traceroute/tracepath: Trace the route packets take to a destination.nslookup/dig: Query DNS servers to resolve domain names.netstat/ss: Display network connections, routing tables, and interface statistics.tcpdump/wireshark: Capture and analyze network traffic.scp/rsync: Securely copy files between systems.nc(netcat): A versatile tool for reading from and writing to network connections.host: DNS lookup utility.