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 getto confirm the system has a route toward an external IP address. - Use
getent,dig,nslookup, orhostto test DNS name resolution. - Use
pingto test ICMP reachability. - Use
curlorwgetto test real HTTP or HTTPS internet access. - Use
nc, Bash/dev/tcp, ortelnetto 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.
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.
ip route get 1.1.1.1Tested output:
1.1.1.1 via 10.0.2.2 dev enp0s3 src 10.0.2.15 uid 0
cacheThis 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:
getent hosts example.comTested output:
2606:4700:10::ac42:93f3 example.com
2606:4700:10::6814:179a example.comUse dig when you want DNS-focused output:
dig +short example.comTested output:
104.20.23.154
172.66.147.243You can also use nslookup:
nslookup example.comTested output:
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.154Or host:
host example.comTested output:
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:93f3If 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:
ping -c 1 -W 2 1.1.1.1Tested output:
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 msThen ping a domain name. This tests DNS resolution plus ICMP reachability:
ping -c 1 -W 2 example.comTested output:
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 msIn 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.
curl -fsS --max-time 5 -o /dev/null -w 'http_code=%{http_code} remote_ip=%{remote_ip}\n' https://example.comTested output:
http_code=200 remote_ip=2606:4700:10::6814:179aThe 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:
-fmakes curl fail on HTTP error responses such as 404 or 500.-sSkeeps normal output quiet but still shows errors.--max-time 5prevents a script from hanging too long.-o /dev/nulldiscards the page body.-wprints 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.
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.
wget -q --spider --timeout=5 https://example.com
echo $?Tested output:
0Exit 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:
nc -vz -w 3 1.1.1.1 443
echo $?Tested output:
Connection to 1.1.1.1 443 port [tcp/https] succeeded!
0Use Bash /dev/tcp when you want a simple check without netcat. This is a Bash feature, not a POSIX shell feature:
timeout 5 bash -c 'cat < /dev/null > /dev/tcp/1.1.1.1/443'
echo $?Tested output:
0Use telnet only when it is already installed or when you are working on an older system:
timeout 5 bash -c 'printf "quit\n" | telnet 1.1.1.1 443'
echo $?Tested output:
Trying 1.1.1.1...
Connected to 1.1.1.1.
Escape character is '^]'.
Connection closed by foreign host.
0A 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.
#!/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"
fiTested output:
online: https check succeededYou can place this logic inside a Bash function if you need to reuse it in a larger script:
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"
fiFor 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?
2. Why does ping work but curl or wget fails?
3. Why does DNS lookup work but the server is still not connected to the internet?
4. Can I check internet connectivity without ping?
5. Is checking 8.8.8.8 or 1.1.1.1 enough to prove internet access?
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.

