Troubleshooting Network Connectivity Issues on a Raspberry Pi

A step-by-step guide to troubleshoot and monitor internet connectivity issues on your Raspberry Pi. …


Updated August 13, 2023

Need help with your Raspberry Pi?
Contact Me!

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


A step-by-step guide to troubleshoot and monitor internet connectivity issues on your Raspberry Pi. Raspberry Pi is an affordable, credit-card sized computer that can run a variety of applications and serve different purposes. However, one common issue that users face is losing network connectivity or having unstable connections. In this article, we will explore how to check internet connection on Raspberry Pi using the command line interface.

Step 1: Connect Your Raspberry Pi to a Network

To begin with, ensure your Raspberry Pi is connected to a network either via Ethernet or Wi-Fi. You can connect an Ethernet cable to the Pi’s ethernet port or set up a Wi-Fi connection by following these steps:

  1. Open the terminal and type sudo nano /etc/wpa_supplicant/wpa_supplicant.conf to edit the Wi-Fi configuration file.
  2. Add your network details in the following format at the end of the file:
network={
    ssid="your_network_name"
    psk="your_password"
}
  1. Save and exit the file by pressing Ctrl + X, then Y and Enter.
  2. Restart the Wi-Fi service with sudo systemctl restart wpa_supplicant.
  3. Check your IP address using the command: ifconfig wlan0. Your Pi should now have an assigned IP address from your router.

Step 2: Ping a Website to Test Connectivity

The simplest way to check if your Raspberry Pi has internet connectivity is by pinging a website. Open the terminal and type the following command:

ping -c 4 google.com

This will send 4 ICMP echo requests to google.com and display the results. If you receive replies from Google, your Pi has internet connectivity. The output should look something like this:

PING google.com (172.217.3.206) 56(84) bytes of data.
64 bytes from lga25s39-in-f14.1e100.net (172.217.3.206): icmp_seq=1 ttl=56 time=18.7 ms
64 bytes from lga25s39-in-f14.1e100.net (172.217.3.206): icmp_seq=2 ttl=56 time=18.7 ms
64 bytes from lga25s39-in-f14.1e100.net (172.217.3.206): icmp_seq=3 ttl=56 time=18.6 ms
64 bytes from lga25s39-in-f14.1e100.net (172.217.3.206): icmp_seq=4 ttl=56 time=18.7 ms

--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 18.690/18.725/18.761/0.070 ms

If you don’t receive any replies or get a ping: unknown host google.com error, it means your Pi is not connected to the internet.

Step 3: Check DNS Resolution

DNS (Domain Name System) resolves domain names to IP addresses. To check if DNS resolution is working correctly on your Raspberry Pi, use the dig command:

dig +short google.com

This should return the IP address of Google’s servers. If it doesn’t, make sure your Pi is using the correct DNS server by editing /etc/resolv.conf:

  1. Open the file with sudo nano /etc/resolv.conf.
  2. Add your DNS server details:
nameserver 8.8.8.8.8
nameserver 8.8.4.4.4

These are Google’s public DNS servers, but you can use any other reliable DNS servers. Save and exit the file as before. 3. Test DNS resolution again using dig +short google.com.

Step 4: Check Firewall Settings

If you’re still experiencing connectivity issues, it could be due to a firewall blocking incoming or outgoing connections. To check if this is the case, disable the firewall temporarily with:

sudo ufw disable

Test your internet connection again using ping and dig. If it works now, you can re-enable the firewall and configure it to allow specific services or ports:

sudo ufw enable
sudo ufw allow <service_name>

Replace <service_name> with the service you want to allow (e.g., ssh, http, https). You can also specify a port number instead:

sudo ufw allow <port_number>/<protocol>

For example, to allow incoming SSH connections on port 22:

sudo ufw allow 22/tcp

Conclusion

Now you know how to check internet connectivity on your Raspberry Pi using the command line interface. If you’re still having issues, consider restarting your router or modem, checking your network cables, or contacting your ISP for further assistance. Always remember to enable the firewall after troubleshooting and configuring it to allow only necessary services.