Is it up? ping and curl
Check a machine answers with ping -c, fetch a page with curl, save it with -o, and read status codes and redirects with curl -I, on a pretend network.
The machines in these lessons are pretend. Your laptop, a web server called web, a database server called db, the router and the website example.com all live inside this page, and nothing you type leaves your browser. The commands and what they print are the real ones.
ping: does it answer?
ping sends a tiny message to another machine and times the reply. It keeps going until you press Ctrl+C, so -c 3 (count) is handy:
ping -c 3 webEach reply line has a time: a few milliseconds is normal on a home network. The summary says how many came back. "Name or service not known" means the name itself is unknown; no replies at all means the machine is down, or something blocks ping.
curl: what does the website say?
A machine can answer ping while its website is broken, so to check a website, ask for a page. curl fetches a URL and prints what comes back:
curl http://web/curl -I shows only the headers, starting with the status:
| Status | Means |
|---|---|
| 200 OK | it worked |
| 301, 302 | moved: the Location header says where |
| 404 Not Found | no page at that address |
| 500, 502 | the server, or the app behind it, failed |
curl -I http://web/nope-o FILE saves the page instead of printing it, and -L follows redirects to the final page.
Your turn
Check that web answers with ping -c 3. Save the shop’s home page, http://web/, as shop.html with curl. Then find out where http://www.example.com/ redirects to, from its headers.
Your task
Check that web answers with ping -c 3. Save the shop’s home page, http://web/, as shop.html with curl. Then find out where http://www.example.com/ redirects to, from its headers.
- web answers ping (not done yet)
- shop.html is the shop’s home page (not done yet)
- The redirect’s status and Location header (not done yet)
The checklist ticks itself off as you work in the terminal: any way that gets the result counts.
Hints come one at a time, then one way to do it. Try each before the next.
What this lesson uses, in one place.
ping- Check that another machine answers More on ping
ping -c- Stop after COUNT replies More on ping
curl- Fetch a URL, or test a web server More on curl
curl -I- Headers only (a HEAD request) More on curl
curl -o- Save to a file More on curl
Every everyday command on one page: the Linux commands cheat sheet.