13 linux commands/shell scripts to check if you are connected to internet

13 Linux commands/shell scripts to check if you are connected to Internet

In this article, we will see 13 Linux commands/shell scripts to check if you are connected to internet. Many times when you are unsure about the availability of internet connection in your linux system, you might want to run a quick test to check and confirm. You can do this by using few tools and utilities that are easily available in almost all the linux platforms. Not only commands but you can also use shell script to run that check. Here we will see 13 such utilities that can be easily employed to perform internet connectivity check in your system by either running it as a command or using it in a shell script.

Sponsored

 

13 linux commands/shell scripts to check if you are connected to internet

13 Linux commands/shell scripts to check if you are connected to Internet

Also Read: List All Running Services in Linux: [Using 4 Tools]

While there are many linux commands/shell scripts that you can use to check if you are connected to internet but here we will look into 13 best ways to perform this check with ease. You can choose to use any of the below given methods depending on your tool availability and requirements.

 

1. ping

Ping is a free and open source utility used for sending ICMP echo requests to target hosts and then wait for its reply to confirm the host availability over the IP network.

a) Command

To check and verify internet connectivity, you can send one packet(-c 1) to target host google.com using below command. If it receives response from host then it display Connected or else it will display as Not Connected. It is however important to note here that many times you might notice that even though target host is up and running, you won’t get any ping response from them. This is simply because some of the secure servers keep their ICMP response disabled due to security reasons.

ping -c 1 google.com &> /dev/null && echo "Connected" || echo "Not Connected"

Output

Connected

b) Shell Script

You can also use a shell script instead of above command to perform the same check using if else statement as shown below.

if ping -c 1 google.com &> /dev/null; then
 echo "Internet is connected."
else
 echo "No internet connection."
fi

Output

cyberithub@ubuntu:~$ ./ping.sh
Internet is connected.

 

2. curl

Curl is a well known tool for retrieving data from a specified URL or server. By using curl to access a web page or a file over the internet, you can determine if your system is capable of establishing a connection to servers on Internet. Hence it can be effectively used for checking internet connectivity. To know more about curl command, you can check 20 Useful curl command in Linux with Examples | How to Use curl in Linux.

a) Command

In below example, we are silently(-s) fetching the headers(--head) of google.com. If it provides 200 OK response then your system is able to connect internet and hence it will display as Connected on output. But in case if it is not able to download headers then it will display as Not Connected.

curl -s --head http://www.google.com | grep "200 OK" &> /dev/null && echo "Connected" || echo "Not Connected"

Output

Connected

b) Shell Script

We can also use above command in the form of shell script using if else condition as shown below.

if curl -s --head http://www.google.com | grep "200 OK" > /dev/null; then
  echo "Internet is connected."
else
  echo "No internet connection."
fi

Output

cyberithub@ubuntu:~$ ./curl.sh
Internet is connected.

 

3. wget

You can also check internet connectivity in your system by trying to download a page or a file from internet by using a frequently used tool for this purpose called wget. To know more about wget command, you can check what is wget and how to use wget command in Linux (20 Popular wget examples).

See also  How to Install Miniconda on Ubuntu 24.04

Command

You can use below wget command to check and verify internet connectivity in your system. Here we are using -q option to suppress the output and --spider option to check the availability of google.com page instead of downloading it. If it can successfully reach out to the page then it will show output as Connected otherwise as Not Connected.

wget -q --spider http://google.com && echo "Connected" || echo "Not Connected"

Output

Connected

Shell Script

You can also use a shell script instead of above command to perform the same check using if else statement as shown below.

if wget -q --spider http://google.com; then
  echo "Internet is connected."
else
  echo "No internet connection."
fi

Output

cyberithub@ubuntu:~$ ./wget.sh
Internet is connected.

 

4. netcat(nc)

If you have netcat or nc command available in your system then you can use this utility to establish TCP or UDP connections to a host port on internet. You can also scan for listening daemon on specific remote port without sending any data. This feature can be effectively used to check internet connectivity in Linux.

a) Command

We are using below nc command to scan for listening daemons on port 443(-z) without sending any data and specifying timeout to 1 sec using -w option. If it is successful then it will show as Connected or else Not Connected.

nc -zw1 google.com 443 && echo "Connected" || echo "Not Connected"

Output

Connected

b) Shell Script

You can also use a shell script instead of above command to perform the same check using if else statement as shown below.

if nc -zw1 google.com 443; then
  echo "Internet is connected."
else
  echo "No internet connection."
fi

Output

cyberithub@ubuntu:~$ ./nc.sh
Internet is connected.

 

5. fping

fping works very similar to ping utility but as compared to ping it has the ability to send ICMP echo requests to multiple hosts at the same time. We can also utilize this tool to send ICMP echo request to a destination server to test the internet connectivity, just like ping utility.

a) Command

You can simply use fping command to send ICMP echo request to destination google.com and wait for its response. If it receives response then it will show as Connected or else it will display as Not Connected. For fping installation on Ubuntu 22.04, you can check How to Install fping on Ubuntu 22.04 [Easy Steps].

cyberithub@ubuntu:~$ fping google.com 1>/dev/null 2>&1 && echo "Connected" || echo "Not Connected"

Output

Connected

b) Shell Script

You can also use a shell script instead of above command to perform the same check using if else statement as shown below.

if fping google.com 1>/dev/null 2>&1; then
  echo "Internet is connected."
else
  echo "No internet connection."
fi

Output

cyberithub@ubuntu:~$ ./fping.sh
Internet is connected.

 

6. ip

ip command is known to be the successor of utilities like ifconfig and route. It not only allows us to view and manage network interfaces but can also be used to provide routing table details. These features can be effectively used to check the internet connection in a system. To know more about ip command, check 25 Popular Linux IP Command Examples (How to Check Linux IP Address)

a) Command

Below ip command is one such example that you can use to check internet connectivity in your system. In this command, we are checking the routing path taken to reach destination server 8.8.8.8. If it is able to reach destination server then it will display Connected on output or else it will show Not Connected.

ip route get 8.8.8.8 &> /dev/null && echo "Connected" || echo "Not Connected"

Output

Connected

b) Shell Script

You can also use the equivalent shell script to perform same checks using if else statement as shown below.

if ip route get 8.8.8.8 &> /dev/null; then
 echo "Internet is connected."
else
 echo "No internet connection."
fi

Output

cyberithub@ubuntu:~$ ./route.sh
Internet is connected.

 

7. nmcli

nmcli, also known network manager command line interface is a versatile tool used for wide ranges of tasks including checking the status of network interfaces and connections. Here we are going to use it for checking internet connection in your system. To know more about nmcli commands, check 30 nmcli command examples in Linux (RHEL/CentOS) – Cheat Sheet.

Sponsored

a) Command

In below example, nmcli command checks if the network device enp0s3 is in a connected state. We are quietly searching for string "100 (connected)" from nmcli output. If it finds then the output will show as Connected, otherwise it will show as Not Connected.

nmcli -t -f GENERAL.STATE device show enp0s3 | grep -q "100 (connected)" && echo "Connected" || echo "Not Connected"

Output

Connected

b) Shell Script

You can also use a shell script instead of above command to perform the same check using if else statement as shown below.

if nmcli -t -f GENERAL.STATE device show enp0s3 | grep -q "100 (connected)"; then
  echo "Internet is connected."
else
  echo "No internet connection."
fi

Output

cyberithub@ubuntu:~$ ./nmcli.sh
Internet is connected.

 

8. dig

dig, also known as domain information groper is famously used for querying DNS servers. This utility can also be used to check the internet connection in your system by verifying whether your system can resolve domain names.

a) Command

Below example queries the specified OpenDNS server for the IP address associated with myip.opendns.com. Since this domain is designed to respond with the IP address of the requester, it effectively tells you your public IP address. If it provides the public ip address then it will show as Connected, otherwise it will display as Not Connected.

dig +short myip.opendns.com @resolver1.opendns.com > /dev/null && echo "Connected" || echo "Not Connected"

Output

Connected

b) Shell Script

You can also use a shell script instead of above command to perform the same check using if else statement as shown below.

if dig +short myip.opendns.com @resolver1.opendns.com > /dev/null; then
  echo "Internet is connected."
else
  echo "No internet connection."
fi

Output

cyberithub@ubuntu:~$ ./dig.sh
Internet is connected.

 

9. host

host is another very convenient tool which can be used to check internet connectivity in your system by performing DNS lookups and see if your system can resolve domain names. This is one of the basic check anyone can do to verify internet connectivity.

a) Command

In below example, we are performing DNS lookups for domain name google.com using host command. If it is successful then it will display Connected, otherwise it will display as Not Connected,

host google.com > /dev/null && echo "Connected" || echo "Not Connected"

Output

Connected

b) Shell Script

You can also use a shell script instead of above command to perform the same check using if else statement as shown below.

if host google.com > /dev/null; then
 echo "Internet is connected."
else
 echo "No internet connection."
fi

Output

cyberithub@ubuntu:~$ ./host.sh
Internet is connected.

 

10. tracepath

tracepath is a very simple yet very handy tool in tracing the path to a specified destination host. This features cannot just be used for checking the internet connectivity but can also be used to identify any potential issues in network path which could affect your connectivity and performance.

a) Command

To demonstrate here, we are tracing the path of google.com by showing all the hops IP address along the path using tracepath -n google.com command. If it is successful in tracing path then it shows as Connected or else it will show as Not Connected.

tracepath -n google.com > /dev/null && echo "Connected" || echo "Not Connected"

Output

Connected

b) Shell Script

If you are looking to use use a shell script instead of running a command then you can use below script.

if tracepath -n google.com > /dev/null; then
  echo "Internet is connected."
else
  echo "No internet connection."
fi

Output

cyberithub@ubuntu:~$ ./tracepath.sh
Internet is connected.

 

11. nmap

Nmap is another powerful utility not just for checking internet connectivity but can also be leveraged to perform detailed network analysis. It has the ability to scan for open ports on a specified host or a range of hosts. By scanning well-known ports on external servers, you can determine if your system can reach these servers over the internet.

a) Command

Here we are checking internet connectivity by running ping scan on google server 8.8.8.8 using below nmap command. If it gets response then it will show as Connected, otherwise it will show as Not Connected.

nmap -sn 8.8.8.8 &> /dev/null && echo "Connected" || echo "Not Connected"

Output

Connected

b) Shell Script

If you are looking to use a shell script instead of command then you can use below shell script to perform the same check using if else statement as shown below.

if nmap -sn 8.8.8.8 &> /dev/null; then
 echo "Internet is connected."
else
 echo "No internet connection."
fi

Output

cyberithub@ubuntu:~$ ./nmap.sh
Internet is connected.

 

12. ifconfig

ifconfig is a very famous traditional linux utility used for quite sometime to display information about network interfaces on your system. Although it has now been replaced with ip command in latest linux versions but in case you are still using older linux systems like CentOS 7, you can still use this tool for performing various checks including internet connectivity check on your system.

a) Command

To check internet connectivity, you have to use below ifconfig command. As you notice, below command is looking for inet keyword from ifconfig output. This keyword will only show on output when you have internet connection established. So if it is available then it means system has connectivity to Internet, in that case it will show as Connected or else it will show as Not Connected.

ifconfig enp0s3 | grep "inet" &> /dev/null && echo "Connected" || echo "Not Connected"

Output

Connected

b) Shell Script

You can also use above command in the form of shell script as shown below.

ifconfig enp0s3 | grep "inet " &> /dev/null; then
 echo "Internet is connected."
else
 echo "No internet connection."
fi

Output

cyberithub@ubuntu:~$ ./ifconfig.sh 
Internet is connected.

 

13. ss

ss, also known as socket statistics is a free and open source utility to check information about network connections and sockets. By listing active connections, it can confirm if your system has established connections to external servers, indicating an active internet connection. To know more about ss command, check 25 Useful Linux SS Command Examples to Monitor Network Connections.

a) Command

In below example, we are checking all established TCP connections on our system that are using HTTP or HTTPS. If it detects the connection established then it will show as Connected or else it will show as Not Connected. If you want you can only check for HTTPS connection to verify your internet connectivity. It is possible that in some cases below output may be deceiving so I would always recommend you to double check your connectivity using other linux utilities.

ss -t -r state established '( dport = :http or dport = :https )' &> /dev/null && echo "Connected" || echo "Not Connected"

Output

Connected

b) Shell Script

If you are looking to use a shell script instead of command then you can use below shell script to perform the same check using if else statement as shown below.

if ss -t -r state established '( dport = :http or dport = :https )' &> /dev/null; then
 echo "Internet is connected."
else
 echo "No internet connection."
fi

Output

cyberithub@ubuntu:~$ ./ss.sh
Internet is connected.


Discover more from Ubuntu-Server.com

Subscribe to get the latest posts sent to your email.

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply