The Ultimate Guide to Checking Internet Connection on Raspberry Pi

Learn how to check if your Raspberry Pi is connected to the internet, troubleshoot connection issues, and make sure your device is online. …


Updated August 19, 2023

Need help with your Raspberry Pi?
Contact Me!

Do you love silly Raspberry Pi Projects?
Check out my this YouTube Channel!


Learn how to check if your Raspberry Pi is connected to the internet, troubleshoot connection issues, and make sure your device is online.

Raspberry Pi is an amazing single-board computer that can be used for various projects and applications. One of the most common use cases is connecting it to the internet and using it as a server or IoT device. In order to do this, you need to make sure your Raspberry Pi is connected to the internet properly. This guide will help you check if your Raspberry Pi is connected to the internet and how to troubleshoot connection issues.

There are several ways to check if your Raspberry Pi is connected to the internet:

  1. Checking the IP address of your device: You can use the hostname -I command to get the IP address assigned to your device. If it returns an IP address, your device is connected to the network.

    $ hostname -I
    192.168.0.10
    

    Keep in mind that this command only shows the IP address assigned by your local network (LAN). To check if your device is connected to the internet, you need to ping a public IP address or domain name. For example:

    $ ping -c 4 google.com
    PING google.com (172.217.168.174) 56(84) bytes of data.
    64 bytes from lhr36s08-in-f14.1e100.net (172.217.168.174): icmp_seq=1 ttl=57 time=14.4 ms
    64 bytes from lhr36s08-in-f14.1e100.net (172.217.168.174): icmp_seq=2 ttl=57 time=14.4 ms
    64 bytes from lhr36s08-in-f14.1e100.net (172.217.168.174): icmp_seq=3 ttl=57 time=14.4 ms
    64 bytes from lhr36s08-in-f14.1e100.net (172.217.168.174): icmp_seq=4 ttl=57 time=14.4 ms
    
    --- google.com ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 3003ms
    rtt min/avg/max/mdev = 14.428/14.435/14.467/0.069 ms
    

    If the ping command returns a response, it means your Raspberry Pi is connected to the internet.

  2. Checking network status with nmcli: You can use the NetworkManager CLI tool (nmcli) to check the network status of your device. First, make sure you have NetworkManager installed:

    $ sudo apt install network-manager
    

    Then, run the following command to check the connection status:

    $ nmcli device show wlan0 | grep IP4.ADDRESS
    IP4.ADDRESS[1]:                             192.168.0.10/24
    

    If the command returns an IP address, your Raspberry Pi is connected to the internet.

  3. Checking network status with ifconfig: You can also use the ifconfig command to check if your device has an IP address and is connected to a network. Run the following command:

    $ ifconfig
    wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.0.10  netmask 255.255.255.0  broadcast 192.168.0.255
    

    If the ifconfig command shows an IP address, your Raspberry Pi is connected to the internet.

  4. Checking network status with iwconfig: If you’re using a wireless connection, you can use the iwconfig command to check if your device is connected to a WiFi access point:

    $ iwconfig wlan0
    wlan0     IEEE 802.11  ESSID:"your_network"  Nickname:"<WIFI@REALTEK>"
             Mode:Managed  Frequency=2.437 GHz  Access Point: A0:B9:C6:D5:E4:F3  
             Bit Rate=1 Mb/s   Tx-Power=20 dBm   
             Retry short limit:7   RTS thr:off   Fragment thr:off
             Power Management:on
             Link Quality=68/70  Signal level=-42 dBm  
             Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
             Tx excessive retries:0  Invalid misc:173   Missed beacon:0
    

    If iwconfig shows a connected network, your device is online.

  5. Checking internet connection with a Python script: You can also use the following Python script to check if your Raspberry Pi has access to the internet:

    import socket
    
    def is_connected():
        try:
            socket.create_connection(("www.google.com", 80))
            return True
        except OSError:
            pass
        return False
    
    if is_connected():
        print("Connected to the internet")
    else:
        print("Not connected to the internet")
    

    Save this script as check_internet.py and run it with Python 3:

    $ python3 check_internet.py
    Connected to the internet
    

    If the script returns “Connected to the internet”, your device is online.

If you’re having trouble connecting your Raspberry Pi to the internet, make sure that:

  • Your network is working properly and other devices can access the internet.
  • Your WiFi credentials are correct.
  • You have a valid IP address assigned by your router or DHCP server.
  • Your firewall settings allow incoming and outgoing connections.

If you still can’t connect, try restarting your Raspberry Pi and your network devices. If the problem persists, consider consulting a networking expert for help.