How to check if internet connectivity is available in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to check for internet connectivity in Linux using fundamental command-line tools. You will begin by using the ping command to test reachability to a public server and understand the basic output of this essential network utility.

Following that, you will explore how to test DNS resolution using the nslookup command, ensuring that your system can translate domain names into IP addresses. Finally, you will utilize the curl command to check connectivity by attempting to retrieve content from a web server. By completing these steps, you will gain practical skills in diagnosing common network issues in a Linux environment.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/RemoteAccessandNetworkingGroup(["Remote Access and Networking"]) linux(("Linux")) -.-> linux/PackagesandSoftwaresGroup(["Packages and Softwares"]) linux/RemoteAccessandNetworkingGroup -.-> linux/ping("Network Testing") linux/RemoteAccessandNetworkingGroup -.-> linux/ip("IP Managing") linux/PackagesandSoftwaresGroup -.-> linux/curl("URL Data Transferring") subgraph Lab Skills linux/ping -.-> lab-558790{{"How to check if internet connectivity is available in Linux"}} linux/ip -.-> lab-558790{{"How to check if internet connectivity is available in Linux"}} linux/curl -.-> lab-558790{{"How to check if internet connectivity is available in Linux"}} end

Ping a public server with ping

In this step, you'll learn how to use the ping command to check network connectivity. The ping command sends packets to a target host and measures the time it takes for the packets to return. This helps you determine if a server is reachable and how quickly you can communicate with it.

Open your terminal if it's not already open. Remember, you can find the Xfce Terminal icon on the left side of your desktop.

Now, let's ping a well-known public server, like Google's DNS server at 8.8.8.8. Type the following command and press Enter:

ping 8.8.8.8

You will see output similar to this:

PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=... time=... ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=... time=... ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=... time=... ms
...

The output shows that your computer is sending packets to 8.8.8.8 and receiving responses.

  • icmp_seq: The sequence number of the packet.
  • ttl: Time To Live, indicating how many hops the packet can take before being discarded.
  • time: The time in milliseconds it took for the packet to travel to the server and back (round-trip time).

To stop the ping command, press Ctrl + C.

You can also ping a server by its domain name, like google.com:

ping google.com

The output will be similar, but ping will first resolve the domain name to an IP address.

PING google.com (...) 56(84) bytes of data.
64 bytes from ... (...): icmp_seq=1 ttl=... time=... ms
64 bytes from ... (...): icmp_seq=2 ttl=... time=... ms
...

Using ping is a fundamental way to troubleshoot network issues. If ping fails, it indicates a problem with your network connection or the target server.

Practice pinging a few different websites or IP addresses to get comfortable with the command.

Click Continue to proceed to the next step.

Test DNS resolution with nslookup

In this step, you'll learn about DNS (Domain Name System) and how to use the nslookup command to test DNS resolution. DNS is like the internet's phone book; it translates human-readable domain names (like google.com) into machine-readable IP addresses (like 8.8.8.8).

The nslookup command is a tool for querying DNS servers to get information about domain names.

Open your terminal if it's not already open.

Let's use nslookup to find the IP address for google.com. Type the following command and press Enter:

nslookup google.com

You will see output similar to this:

Server:		...
Address:	...#53

Non-authoritative answer:
Name:	google.com
Address: ...
Name:	google.com
Address: ...
...

The output shows:

  • Server: The DNS server that your computer used for the lookup.
  • Address: The IP address of the DNS server.
  • Non-authoritative answer: The information returned by the DNS server.
  • Name: The domain name you queried.
  • Address: One or more IP addresses associated with the domain name. Websites often have multiple IP addresses for load balancing and redundancy.

You can also use nslookup to find the domain name associated with an IP address (a reverse DNS lookup). Let's try the IP address 8.8.8.8:

nslookup 8.8.8.8

The output will look something like this:

Server:		...
Address:	...#53

Non-authoritative answer:
...
Name:	dns.google.

This shows that the IP address 8.8.8.8 is associated with the domain name dns.google..

nslookup is a valuable tool for diagnosing network issues related to domain name resolution. If you can ping an IP address but not a domain name, the problem might be with DNS.

Experiment with nslookup by looking up different domain names.

Click Continue to move on.

Check connectivity with curl

In this step, you'll use the curl command to check connectivity to a web server and retrieve content. curl is a versatile command-line tool for transferring data with URLs. It supports various protocols, including HTTP, HTTPS, FTP, and more.

Open your terminal if it's not already open.

Let's use curl to fetch the content of a simple webpage, like example.com. Type the following command and press Enter:

curl example.com

You will see the HTML content of the example.com webpage printed directly in your terminal.

<!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style type="text/css">
    body {
        background-color: #f0f0f2;
        margin: 0;
        padding: 0;
        font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;

    }
    div {
        width: 600px;
        margin: 5em auto;
        padding: 2em;
        background-color: #fdfdff;
        border-radius: 0.5em;
        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
    }
    a:link, a:visited {
        color: #38488f;
        text-decoration: none;
    }
    @media (max-width: 700px) {
        div {
            margin: 0 auto;
            width: auto;
        }
    }
    </style>
</head>

<body>
<div>
    <h1>Example Domain</h1>
    <p>This domain is for use in illustrative examples in documents. You may use this
    domain in examples without prior coordination or asking for permission.</p>
    <p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>

This shows that your computer was able to connect to example.com and retrieve its content.

curl is a powerful tool for testing web connectivity, debugging web services, and downloading files.

You can also use curl with the -I option to fetch only the HTTP headers, which is useful for checking the status of a webpage without downloading the entire content:

curl -I example.com

The output will show the response headers:

HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Etag: "..."
Expires: ...
Last-Modified: ...
Server: ECS (sg2/2A3E)
Vary: Accept-Encoding
X-Cache: HIT
Content-Length: 1270
Date: ...
Connection: close

A HTTP/1.1 200 OK status indicates a successful connection.

curl is an essential tool for anyone working with web technologies.

Click Continue to complete this lab.

Summary

In this lab, you learned how to check internet connectivity in Linux using several fundamental commands. You started by using the ping command to test reachability to a public server by both IP address and domain name, observing the round-trip time and packet sequence. This demonstrated how to verify basic network connectivity and identify potential issues.