How to Check Internet Connection in Linux Command Line and Shell Script

Learn how to check internet connectivity from the Linux command line using tested ping, curl, wget, DNS, route, netcat, telnet, and Bash script examples with sample output.

Published

Updated

Read time 8 min read

Reviewed byDeepak Prasad

How to Check Internet Connection in Linux Command Line and Shell Script

A Linux server can fail internet access at different layers. The default route may be missing, DNS may be broken, ICMP may be blocked, TCP ports may be filtered, or HTTP access may require a proxy. Because of this, one command is not always enough to prove that the server is fully connected to the internet.

This guide shows tested commands to check internet connectivity from the Linux command line. Use the command that matches what you want to prove:

  • Use ip route get to confirm the system has a route toward an external IP address.
  • Use getent, dig, nslookup, or host to test DNS name resolution.
  • Use ping to test ICMP reachability.
  • Use curl or wget to test real HTTP or HTTPS internet access.
  • Use nc, Bash /dev/tcp, or telnet to test whether a remote TCP port is reachable.

The examples below were tested on a Linux host with ping from iputils 20240905, curl 8.12.1, GNU Wget 1.24.5, OpenBSD netcat 1.228, BIND DNS tools 9.20.11, and iproute2 6.14.0. Your IP addresses, latency, DNS resolver, and route output can be different.

NOTE
For a shell script, prefer an HTTP or HTTPS check with curl because it tests the type of access most applications need. For network troubleshooting, test in this order: route, DNS, ICMP or TCP, then HTTP.

1. Check whether Linux has a route to the internet

Start with the route table when the server looks completely offline. This does not contact 1.1.1.1; it asks the kernel which interface, gateway, and source IP it would use for that destination.

bash
ip route get 1.1.1.1

Tested output:

text
1.1.1.1 via 10.0.2.2 dev enp0s3 src 10.0.2.15 uid 0
    cache

This output means the host has a route to 1.1.1.1 through gateway 10.0.2.2 using interface enp0s3. It does not prove that DNS, firewall rules, or web access is working.

If this command says the network is unreachable, first check the IP address, gateway, and interface state. The nmcli command examples article is useful when you need to inspect or configure NetworkManager connections.


2. Check DNS resolution from the command line

DNS checks answer a different question: can the Linux host convert a domain name into an IP address? DNS may work even when web access is blocked, so treat this as a DNS test, not a complete internet test.

Use getent first if you want to test name resolution through the normal system resolver path configured in /etc/nsswitch.conf:

bash
getent hosts example.com

Tested output:

text
2606:4700:10::ac42:93f3 example.com
2606:4700:10::6814:179a example.com

Use dig when you want DNS-focused output:

bash
dig +short example.com

Tested output:

text
104.20.23.154
172.66.147.243

You can also use nslookup:

bash
nslookup example.com

Tested output:

text
Server:         127.0.0.53
Address:        127.0.0.53#53

Non-authoritative answer:
Name:   example.com
Address: 172.66.147.243
Name:   example.com
Address: 104.20.23.154

Or host:

bash
host example.com

Tested output:

text
example.com has address 104.20.23.154
example.com has address 172.66.147.243
example.com has IPv6 address 2606:4700:10::ac42:93f3
example.com has IPv6 address 2606:4700:10::6814:179a
example.com mail is handled by 0 .
example.com has HTTP service bindings 1 . alpn="h2" ipv4hint=104.20.23.154,172.66.147.243 ipv6hint=2606:4700:10::6814:179a,2606:4700:10::ac42:93f3

If DNS fails but IP-based tests work, troubleshoot resolver configuration, /etc/resolv.conf, NetworkManager DNS settings, or your local DNS server. If you run your own DNS service, see the guide to configure a BIND DNS server.


3. Check internet reachability with ping

ping sends ICMP echo requests. It is quick and useful, but many networks block ICMP, so a failed ping does not always mean HTTPS or application traffic is down.

First ping a public IP address. This tests routing plus ICMP without depending on DNS:

bash
ping -c 1 -W 2 1.1.1.1

Tested output:

text
PING 1.1.1.1 (1.1.1.1) 56(84) bytes of data.
64 bytes from 1.1.1.1: icmp_seq=1 ttl=255 time=8.89 ms

--- 1.1.1.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 8.888/8.888/8.888/0.000 ms

Then ping a domain name. This tests DNS resolution plus ICMP reachability:

bash
ping -c 1 -W 2 example.com

Tested output:

text
PING example.com (172.66.147.243) 56(84) bytes of data.
64 bytes from 172.66.147.243: icmp_seq=1 ttl=255 time=8.64 ms

--- example.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 8.644/8.644/8.644/0.000 ms

In the statistics section, 1 received and 0% packet loss show that the ping succeeded. For more ping options, see the Linux ping command examples.


4. Check real HTTPS access with curl

For most servers, curl is the most practical internet check because applications usually need HTTP or HTTPS access, not only ICMP. The command below requests https://example.com, discards the body, prints the HTTP status code, and stops after 5 seconds.

bash
curl -fsS --max-time 5 -o /dev/null -w 'http_code=%{http_code} remote_ip=%{remote_ip}\n' https://example.com

Tested output:

text
http_code=200 remote_ip=2606:4700:10::6814:179a

The important result is http_code=200, which means the HTTPS request succeeded. The remote_ip value may be IPv4 or IPv6 depending on your resolver and network.

Useful options in this command:

  • -f makes curl fail on HTTP error responses such as 404 or 500.
  • -sS keeps normal output quiet but still shows errors.
  • --max-time 5 prevents a script from hanging too long.
  • -o /dev/null discards the page body.
  • -w prints only the fields you want in the output.

See the curl command examples article for more options. If the command is missing on Ubuntu, use install cURL on Ubuntu; on other distributions see the curl command not found guide.

IMPORTANT
If your Linux host must use a proxy, configure http_proxy, https_proxy, or curl proxy options before using curl as the final connectivity test. A direct curl test may fail even when proxy-based internet access is available.

5. Check internet access with wget

wget --spider checks whether a URL is reachable without downloading the page content. This is useful on systems where wget is already installed.

bash
wget -q --spider --timeout=5 https://example.com
echo $?

Tested output:

text
0

Exit status 0 means the URL check succeeded. A non-zero exit status means the check failed, but you still need to inspect the error to know whether it was DNS, TCP, TLS, HTTP, or proxy related.

For more wget examples, see the wget command in Linux article. If the command is missing, see wget command not found.


6. Check a remote TCP port with nc, Bash dev tcp, or telnet

A TCP port check is useful when ping is blocked or when you only need to know whether an outbound connection to a specific IP and port works. For HTTPS connectivity, test port 443.

Use nc when available:

bash
nc -vz -w 3 1.1.1.1 443
echo $?

Tested output:

text
Connection to 1.1.1.1 443 port [tcp/https] succeeded!
0

Use Bash /dev/tcp when you want a simple check without netcat. This is a Bash feature, not a POSIX shell feature:

bash
timeout 5 bash -c 'cat < /dev/null > /dev/tcp/1.1.1.1/443'
echo $?

Tested output:

text
0

Use telnet only when it is already installed or when you are working on an older system:

bash
timeout 5 bash -c 'printf "quit\n" | telnet 1.1.1.1 443'
echo $?

Tested output:

text
Trying 1.1.1.1...
Connected to 1.1.1.1.
Escape character is '^]'.
Connection closed by foreign host.
0

A successful TCP connection proves that the remote IP and port are reachable. It does not prove that the HTTPS certificate, HTTP response, proxy settings, or application login works. For more port troubleshooting, see how to check whether a port is open in Linux.


7. Use a shell script to check internet connection

For scripts, keep the check short, deterministic, and easy to interpret. The following script uses curl because it validates HTTPS access and returns a reliable exit code.

bash
#!/usr/bin/env bash

if curl -fsS --max-time 5 -o /dev/null https://example.com; then
    echo "online: https check succeeded"
else
    echo "offline: https check failed"
fi

Tested output:

text
online: https check succeeded

You can place this logic inside a Bash function if you need to reuse it in a larger script:

bash
check_internet_access() {
    curl -fsS --max-time 5 -o /dev/null https://example.com
}

if check_internet_access; then
    echo "online: https check succeeded"
else
    echo "offline: https check failed"
fi

For script control flow basics, see Bash if else examples and Bash function examples.


8. Which command should you use?

Use this quick mapping when choosing a command:

Requirement Best command
Check whether a route exists toward the internet ip route get 1.1.1.1
Check DNS resolution getent hosts example.com or dig +short example.com
Check basic ICMP reachability ping -c 1 -W 2 1.1.1.1
Check real web access curl -fsS --max-time 5 -o /dev/null https://example.com
Check a specific TCP port nc -vz -w 3 1.1.1.1 443
Check connectivity in a shell script curl with a timeout and exit-code check

nmap and traceroute can also help with network diagnosis, but they are not required for a basic internet connectivity check. They were not installed on the test host used for this article, so they are not used as primary tested examples here. If you need deeper packet or path troubleshooting, tools such as tcpdump can help; see the tcpdump command examples.


Frequently Asked Questions

1. What is the best Linux command to check internet connection in a shell script?

curl is usually the best choice for a shell script because it tests real HTTP or HTTPS access and supports short timeouts, silent mode, failure handling, and useful exit codes.

2. Why does ping work but curl or wget fails?

Ping uses ICMP, while curl and wget use HTTP or HTTPS over TCP. A firewall, proxy, DNS issue, TLS inspection device, or blocked outbound port 80 or 443 can allow ping while breaking web access.

3. Why does DNS lookup work but the server is still not connected to the internet?

DNS lookup only proves that the system can resolve a name. The resolved service may still be unreachable because of routing, firewall, proxy, TCP, TLS, or remote server issues.

4. Can I check internet connectivity without ping?

Yes. Use curl or wget for HTTP or HTTPS checks, nc or Bash dev tcp for a TCP port check, and getent or dig for DNS resolution checks.

5. Is checking 8.8.8.8 or 1.1.1.1 enough to prove internet access?

It proves that the system can reach that public IP on the tested protocol, but it does not prove that DNS, HTTPS, proxy configuration, or every external service works.

Summary

To check internet connection in Linux, do not rely on a single command for every situation. Start with ip route get when routing is in doubt, use DNS tools when names do not resolve, use ping for quick ICMP checks, use nc or Bash /dev/tcp for TCP port checks, and use curl or wget when you need to prove real HTTP or HTTPS access.

For automation, curl -fsS --max-time 5 -o /dev/null https://example.com is usually the clearest shell-script check because it tests the same web connectivity most Linux servers and applications actually need.

Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive …