Linux Command Line · Lesson 34 of 43
Networking basics: ping, curl and ip
Check connectivity and fetch data from the command line with ping, curl, wget, ip and dig, and learn to troubleshoot a network step by step.
UNDERSTAND IT. THEN TRY IT.
What you can do after this lesson
You will check whether a machine can reach the network, test a website or an API without a browser, and find out where a connection breaks. These tools are what administrators and developers reach for first when "it doesn't load".
Most of these commands need a network, so we show them as text with example output (the address 203.0.113.10 comes from a range reserved for documentation and is not the real address of any site). Your results will differ, and the practice machine used in this course stays offline. The one exception, curl reading a local file, is run for real.
ping: is it alive?
ping sends a small message to a host and waits for the answer, which tells you the host is reachable and how long the round trip takes. On Linux it runs until you press Ctrl+C, so limit it with -c:
ping -c 3 example.com
PING example.com (203.0.113.10) 56(84) bytes of data.
64 bytes from 203.0.113.10: icmp_seq=1 ttl=56 time=11.8 ms
64 bytes from 203.0.113.10: icmp_seq=2 ttl=56 time=11.2 ms
64 bytes from 203.0.113.10: icmp_seq=3 ttl=56 time=11.5 ms
--- example.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003msThe time is the latency in milliseconds, and packet loss shows messages that never came back. Some servers deliberately ignore ping, so silence does not always mean the host is down.
Your addresses: ip
ip addr (short form ip a) lists your network interfaces and their IP addresses. ip route shows where traffic leaves for other networks, including the default gateway, usually your router:
ip addr
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP
inet 192.168.1.23/24 brd 192.168.1.255 scope global eth0
ip route
default via 192.168.1.1 dev eth0
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.23Here the machine has the address 192.168.1.23, the /24 describes the size of the local network, and the gateway is 192.168.1.1. The special interface lo is the loopback: the address 127.0.0.1, or localhost, always means "this machine". hostname -I prints your addresses on one line. The old ifconfig and route commands do the same on older systems.
curl: talk to a web server
curl transfers data using URLs. Without options, it prints the response body to the screen:
curl https://example.comThe options you will use most:
| Option | Effect |
|---|---|
-s |
Silent: no progress bar |
-I |
Show only the response headers |
-L |
Follow redirects |
-o file |
Save the body to a file |
-O |
Save using the name from the URL |
-v |
Verbose: show the request and response in full |
-X POST -d 'a=1' |
Send a POST request with data (-d alone already makes it a POST) |
-H 'Header: value' |
Add a request header |
curl -sI https://example.com | head -n 1
HTTP/2 200The first line holds the status code: 200 is success, 301 and 302 are redirects, 404 is not found, and 500 and above are server errors. For APIs, combine options: curl -s -H "Accept: application/json" https://api.example.com/items. Pipe JSON into jq to read it comfortably.
curl is not limited to the web. It can read local files with a file:// URL, which works without any network and is a fine way to see the basic idea:
curl -s file:///home/learner/project/docs/notes.txt | head -n 2Linux is a family of operating systems.
The shell reads your commands.wget is a simpler downloader: wget https://example.com/file.zip saves the file with its own name and can resume interrupted downloads with -c.
DNS: names to addresses
Computers talk to addresses, and people use names, so the DNS system translates between them. dig and nslookup show what a name resolves to:
dig +short example.com
203.0.113.10The file /etc/hosts can override DNS for particular names, and /etc/resolv.conf or resolvectl status shows which DNS servers the machine uses. A name that fails to resolve while the same address pings fine is a DNS problem, and that distinction is the most useful trick in network troubleshooting.
Troubleshooting in order
When "the internet is down", work from the inside outwards, stopping at the first step that fails:
- Do I have an address? Check
ip addrfor aninetline on the active interface. - Can I reach my router?
ping -c 3 192.168.1.1(your gateway fromip route). - Can I reach the internet by address?
ping -c 3 1.1.1.1. - Do names resolve?
dig +short example.com. If step 3 worked and this fails, fix DNS. - Does the service answer?
curl -sI https://example.com. If the host answers ping but this fails, the problem is the service or a firewall.
For deeper checks, traceroute example.com shows each hop on the way, and ss -tulpn lists the ports on which your machine is listening.
Security notes
Treat the output of curl like any untrusted input. Do not pipe a download directly into a shell. Do not scan or probe networks that you do not own or have permission to test. Use https:// for anything involving passwords or keys.
Common mistakes
Letting ping run forever. Use -c 3.
Assuming no reply means the host is down. Firewalls often drop ping.
Forgetting -L and getting an empty page. A redirect response carries no real page.
Blaming "the internet" without testing the steps. Work from your address outwards.
Exercise
On your own machine, find your IP address and default gateway with ip addr and ip route. Ping the gateway, then a public address such as 1.1.1.1, then a name. Fetch only the headers of a website with curl -sI, and then save its page into a file with curl -s -o page.html. Explain what each result tells you about your connection.
KNOWLEDGE CHECK
A quick check. You’ve got this.
Try each question. Every answer comes with an explanation.
Ready for the next small win?
Mark the lesson complete when you are ready.