How to test Internet connection on Linux

In this tutorial you will learn how to test internet connection on Linux operating system. When we talk about the internet connection usually this for everybody means different thing. Meaning, you might be connected to the Internet but unable to browse any web sites.

In this tutorial you will learn:

  • How to test internet connection
  • How to test DNS resolution
  • How to test Local Area network
  • How to check your DNS resolution

test Internet connection on Linux

Test Internet connection on Linux

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Installed or upgraded Ubuntu 20.04 Focal Fossa
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to test Internet connection on Linux step by step instructions

Let’s take a top-down approach to test and troubleshoot your Internet connection. Each of the steps below might provide you with hints on what the the problem with your internet connection might be and how to fix it.

  1. The first and the most obvious way on how to test your internet connection is to open up any Internet browser at your disposal and browse any live website. Fro example, navigate your browser to https://linuxconfig.org.

    In case you do not have graphical user interface available, use one of the many command line tools to connect to any website. For example try to use curl command from your terminal:

    $ curl -I https://linuxconfig.org
    HTTP/1.1 200 OK
    
    Test Internet connection on Linux with curl command

    Test Internet connection on Linux with curl command

    In case that you can see the website on your browser or received the 200 OK when using the curl command you might be happy to know that your Internet connection test was successful and you are connected to the Internet. If you still have issues to connect to any other desired Internet services then the issue might be related to firewall on your operating system, router and internet service provider.

  2. In case the first step did not resolve your issue, then it is time to check your internet connection on a lower level. To do so execute the following ping command which will send network packets to an external server using its IP address. In this case let’s try to ping Google’s DNS server :
    $ ping -c 2 8.8.8.8
    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=54 time=10.4 ms
    64 bytes from 8.8.8.8: icmp_seq=2 ttl=54 time=10.2 ms
    
    --- 8.8.8.8 ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 1006ms
    rtt min/avg/max/mdev = 10.157/10.291/10.425/0.134 ms
    

    The above command should result in 0% packet loss.



  3. In case you are unable to ping the above IP address as shown in the previous step then you are either disconnected from the internet or your gateway settings of your network interface are incorrect.

    Therefore, first retrieve your gateway IP address and try to see if you can reach it by using the ping command. For example, first use the ip command to to obtain your default gateway IP address:

    $ ip r
    default via 192.168.1.1 dev enp0s3 proto dhcp metric 100
    

    Next, try to ping this IP address:

    $ ping -c 1 192.168.1.1
    PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
    64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=2.77 ms
    
    --- 192.168.1.1 ping statistics ---
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 2.765/2.765/2.765/0.000 ms
    
    Check and test default gateway on Linux

    Check and test default gateway on Linux

    Based on your results there are few possible interpretations. In case you can reach your gateway and unable to ping the server as shown in the above step, then you are most likely disconnected from the Internet. In case, you are not able to reach your default gateway your either have incorrect default gateway settings or the gateway blocks your ping requests.

  4. Next, step is to check your DNS server network configuration settings:
    $ systemd-resolve --status | grep Current
          Current Scopes: DNS
      Current DNS Server: 192.168.1.1
    

    Our system is set to use DNS server host with an IP address 192.168.1.1. Make sure that you can reach your DNS server. Again, the ping is a handy tool also here:

    $ ping -c 2 192.168.1.1
    PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
    64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.535 ms
    64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.570 ms
    
    --- 192.168.1.1 ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 1016ms
    rtt min/avg/max/mdev = 0.535/0.552/0.570/0.017 ms
    

    Once again, the output of the above command should result in 0% packet loss.

    In case you cannot reach your DNS it could mean that it either, does not respond to ping’s ICPM packages, it is behind the firewall or the server is down.

    In which case update your /etc/resolv.conf with an alternative DNS server.

  5. Test you DNS server by attempting to resolve DNS name eg. linuxconfig.org with dig command:
    $ dig @192.168.1.1 linuxconfig.org
    
    ; <<>> DiG 9.16.1-Ubuntu <<>> @192.168.1.1 linuxconfig.org
    ; (1 server found)
    ;; global options: +cmd
    ;; Got answer:
    ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 10032
    ;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1
    
    ;; OPT PSEUDOSECTION:
    ; EDNS: version: 0, flags:; udp: 4096
    ;; QUESTION SECTION:
    ;linuxconfig.org.               IN      A
    
    ;; ANSWER SECTION:
    linuxconfig.org.        187     IN      A       104.26.3.13
    linuxconfig.org.        187     IN      A       104.26.2.13
    
    ;; Query time: 4 msec
    ;; SERVER: 192.168.1.1#53(192.168.1.1)
    ;; WHEN: Thu May 07 11:01:41 AEST 2020
    ;; MSG SIZE  rcvd: 76
    


  6. Confirm system-wide settings by trying to resolve DNS host name. Example:

    $ resolvectl query linuxconfig.org
    linuxconfig.org: 104.26.3.13                   -- link: enp0s3
                     104.26.2.13                   -- link: enp0s3
    
    -- Information acquired via protocol DNS in 2.7ms.
    -- Data is authenticated: no
    


Comments and Discussions
Linux Forum