Check internet connections linux

9 commands to check if connected to internet with shell script examples

Table of Contents

In this article I will share multiple commands and examples to check if connected to internet for your Linux machine. At the end of the article I will also share some example to test internet connection from the Linux to external network using shell script.

But before we do that there are certain things you must take care of, such as

  • You have IP Address assigned to your network interface (static or dhcp)
  • Your gateway is reachable from the Linux system
  • If you are on virtual machine then make sure your VM is configured to be able to connect external network
  • Your DNS is properly configured

Now there are multiple commands which are available in Linux which can be used to check if connected to internet, we will try to discuss some of them here in brief.

1. Ping Test

The very first tool I use to check if connected to internet is via ping utility . But do you know how to check ping or perform ping check to check if connected to internet? Ping is part of iputils rpm so make sure iputils is installed on your setup

To perform ping check, try to ping any page on internet such as google.com

Here we use -c 1 with ping tool to send only one packet . Now check the statistics section, we see 1 packet was transmitted and received and there was 0% packet loss so it means we are connected to internet.

If you plan to check if connected to internet via shell script then you can use below, anything in the output other than «0» means not connected to internet.

2. Check port availability using cat, echo..

There are various tools which can be used to check port availability which I will share in this article. But many of them are not installed by default so you may have to install them manually. Alternatively you can use below command to check if connected to internet without installing any additional rpm

If the output is 0 then it means you are connected to internet, but if the output is something like below

Then your linux node is not connected to internet.

Alternatively you can also use

3. DNS lookup using nslookup, host etc..

You can perform a DNS lookup any web page address to check if connected to internet. With a successful DNS lookup you should get a response something like below.

For a failed DNS lookup you should get something like

There are many more commands to perform DNS lookup such as host, dig etc

4. Curl

curl is a tool to transfer data from or to a server, using one of the many supported protocols such as HTTP, FTP etc. We can also use this tool to query a webpage and test internet connection in Linux.

Here if you receive anything other than 200 OK then it means the server failed to connect to the provided page. So unless you provide an invalid webpage, your node is connected to internet

5. Telnet

Telnet is another tool to check port connectivity so you can check port availability of any webpage from the Linux node to check if connected to internet. We can try connecting port 53 of Google DNS to check internet connection.

Читайте также:  Как удалить avg с компьютера полностью windows 10 если его не видит компьютер

As you see the session was » connected «. If you get an output like below

then it means there is a problem with internet connectivity.

6. Nmap

nmap is normally a port scanner to check the list of open ports on a system. We will use this to connect to external network to scan the port. If it is able to connect to the external network for port scanning then we can check if connected to internet.

Here I am scanning google.com on port 443. As you see highlighted section, nmap was able to establish connection with google.com

7. netcat or nc

In some variant of Linux you will find netcat while in others nc , you can use either of these tools for port scanning. nc or ncat is part of nmap-ncat rpm. Here we use nc command to check connection to google.com on port 443

8. wget

GNU Wget is a free utility for non-interactive download of files from the Web. But we can also use this to check if connected to internet.

Here with —spider Wget will behave as a Web spider, which means that it will not download the pages, just check that they are there
With echo $? we check the exit status, anything other than 0 in the output means your system is not connected to internet

9. Traceroute

The traceroute command shows every step taken on the route from your machine to a specified host. Assuming you are having problems connecting to www.google.com so you can use traceroute to check all the hubs taken to reach the destination server. This will also test internet connection.

Here we were able to trace the route to google.com upto a certain point.

What do those * * * mean?

Each one indicates a five-second timeout at that hop. Sometimes that could indicate that the machine simply doesn’t understand how to cope with that traceroute packet due to a bug, but a consistent set of * indicates that there’s a problem somewhere with the router to which 216.239.43.239 hands off packets.

On a Linux system with no internet connection, you would face problem with DNS resolution itself

How To check if connected to internet using shell script?

You can easily use any or all the above commands/methods to check if connected to internet using bash/shell script.

For example by using wget you can test internet connection in the below shell script:

Here this script will print the status of the internet, similarly for other scripts you can check the exit status and perform tasks accordingly.

Lastly I hope the steps from the article to check if connected to internet (test internet connection) on Linux was helpful. So, let me know your suggestions and feedback using the comment section.

Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Источник

How to Check Internet Connectivity in Linux Terminal

Method 1: PING Command

The best and foremost way is to check the internet availability through the PING command.

The PING command is used for sending and receiving packages from and to some external/remote server.

As an example, we will try to send three internet packages to the Google server and check the internet connectivity if we will be able to receive the internet packets from the Google server.

In the output, you can also witness that the three packages are transmitted and received. Again, this is because pinging to the Google server went successful.

Method 2: CURL Command

Another way to check the internet connection is through the CURL command.

The CURL command is used to download, upload, or transfer the data over the network from or to the server. It supports various protocols, and we can use it to check the internet connectivity by connecting to linuxhint.com and fetch the headers. If the headers are fetched successfully, that means the internet is working; otherwise, there is a problem.

Читайте также:  Серийники для windows 2003 r2

If you get the status 200 OK and the website’s headers, the internet connection is working perfectly fine.

These are the methods to check internet connectivity.

Diagnose the Problem

If the internet is not connected using any of the above methods, you can use the IP command to check whether there is a problem in the gateway settings or network interface.

To get the IP address of your system assigned by the network adapter, use the command provided below:

To get the default gateway IP address, use the “ip r” command:

And try to ping to the default gateway using the PING command:

If pinging goes unsuccessful, it means there is a problem with the gateway settings, and you need to reconfigure them. Otherwise, the gateway settings are fine, but the internet is not working from the internet service provider end.

Conclusion

This post contains a profound guide on how to check the internet connectivity in Linux terminal and how to diagnose the problem. PING command is precisely used for pinging purposes, as its name is saying, while CURL command is used to transferring the data over the network. Using the “ip” command, we can diagnose the problem. Either there is a problem in the default gateway, DNS server resolution, Network adaptor, or ISP (internet service provider).

Источник

Linux: Check Network Connection Command

H ow do I check network connections under Linux using command line options?

You need to use any one of the following tool or command under Linux to check network connections including their state, source/destination, and addresses and bandwidth usage etc:

  1. ss command: It dump socket (network connection) statistics such as all TCP / UDP connections, established connection per protocol (e.g., display all established ssh connections), display all the tcp sockets in various state such as ESTABLISHED or FIN-WAIT-1 and so on.
  2. netstat command: It can display network connections, routing tables, interfaces and much more.
  3. tcptrack and iftop commands: Displays information about TCP connections it sees on a network interface and display bandwidth usage on an interface by host respectively.

Display Currently Established, Closed, Orphaned and Waiting TCP sockets, enter:

# ss -s
Sample outputs:

Or you can use the netstat command as follows:
# netstat -s
Sample outputs:

Display All Open Network Ports

Use the ss command as follows:
# ss -l
Sample outputs:

OR Use the netstat command as follows:
# netstat -tulpn
Sample outputs:

Display All TCP Sockets

Type the ss command as follows:
# ss -t -a
Or use the netstat command as follows:
# netstat -nat

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Display All UDP Sockets

Type the ss command as follows:
# ss -u -a
Or use the netstat command as follows:
# netstat -nau

lsof Command

You can use the lsof command follows to list more information about open ports:
# lsof -i :portNumber
# lsof -i tcp:portNumber
# lsof -i udp:portNumber
# lsof -i :80 | grep LISTEN

View Established Connections Only

Use the netstat command as follows:
# netstat -natu | grep ‘ESTABLISHED’

Say Hello To tcptrack

The tcptrack command displays the status of TCP connections that it sees on a given network interface. tcptrack monitors their state and displays information such as state, source/destination addresses and bandwidth usage in a sorted, updated list very much like the top command.
# tcptrack -i eth0
Sample outputs:

Fig.01: tcptrack in Action

iftop command

The iftop command listens to network traffic on a given network interface such as eth0, and displays a table of current bandwidth usage by pairs of hosts:
# iftop -i eth1
It can display or analyses packet flowing in and out of the 192.168.1.0/24 network:
# iftop -F 192.168.1.0/24
Sample outputs:

Читайте также:  Как изменить тип сети windows server 2012

Fig.02: iftop command in action

Please go through the following articles for more information:

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

6 Linux Utility to Test Network Connectivity

Geekflare is supported by our audience. We may earn affiliate commissions from buying links on this site.

Wondering how to check connectivity between two network endpoints?

One of the common tasks for sysadmin is to check the connectivity to troubleshoot networking issues. It could be anything like application can’t connect to backend service, unable to fetch data from external URL, verify if the flow is opened, etc.

Whatever it might be, the following utility/commands would help you. They are tested on CentOS, and I don’t see any reason not to work on another Linux distro.

telnet

One of the widely used commands to test essential connectivity between servers, server to another network device’s IP. The syntax for the command is easy.

Let’s say you want to test if you can connect to port 8080 on 10.0.0.1 IP address; then the command would be.

If there is no issue in connecting, then you should see the connected message.

Note: if you get a command not found while executing telnet then you need to install telnet as I explained here.

In most of the scenarios, telnet should help. However, if you need some other option then here are some telnet alternatives.

ncat or nc

Ncat (a.k.a. nc) is a powerful network utility with many features like bind and accept a connection, execute commands remotely, write and read data, etc. It works on IPv4 and IPv6, both.

To do a simple test to check if the port is opened or not, you will execute the following.

Let’s take an example of testing 443 port on geekflare.com.

As mentioned, you can also use nc to bind the connection to listen on a particular port. This can be handy when you don’t have actual services running but want to ensure connectivity exists.

To start listening on a port:

It will bind the port to a given number.

If ncat is not installed, then you can get it done with yum install nc on CentOS/RHEL servers.

wget is a useful command to download/test HTTP, HTTPS, and FTP. If you are working as a web engineer or often dealing with web-related issues then wget is your friend. Testing using wget is straightforward.

Here is an example of testing tools.geekflare.com

If it shows connected means there is no connectivity issue.

Check out this to see some of the frequently used wget command examples.

A curl is a multipurpose tool.

Do you know you can telnet to a port using curl?

Well, now you know.

The following is a working example.

And, when there is no listening port or firewall issue, then you will see trying…

You can also use curl to download the data. It supports multiple protocols – HTTP, HTTPS, FTP, IMAP, LDAP, POP3, SCP, SFTP, GOPHER, etc.

A popular tool with hundreds of features. Often this is considered as a security tool, nmap lets you test a single IP/port or in the range.

To test a single port

An example of testing port 443 on siterelic.com

Look at the state column. If you see open means connection is ok. And, if the state is filtered that means connectivity doesn’t exist.

One of the widely used commands is to check if a remote host is responding to ICMP ECHO_REQUEST or not. Keep in mind, this may not give you accurate results when ICMP is blocked at the remote network’s firewall. Assuming that’s not the case, you can ping to IPv4 or IPv4 network endpoint as below.

For example, success result of geekflare.com

For example, failure result of internal IP.

If the endpoint supports IPv6, you can use the ping6 command as below.

If you need to do remote ping over the Internet, you can use the online ping tool.

Conclusion

telnet is phasing out in the latest Linux version. Thanks to the above telnet alternative.

If you are new to Linux and looking to learn then check out this Udemy course.

Источник

Оцените статью