Over the years, I have used many Raspberry Pis as local/home servers. Throughout the years, I faced a couple of issues that I was able to solve so that I could get a stable and trustworthy server. The last issue that I faced, which was tricky to fix was the networking problem.
The problem
In most cases, I was using my Raspberry Pis headless either as web servers (handing a couple of web requests per minute) or as web clients (crawling a couple of webpages per minute or hour), meaning that multiple network packets were transmitted or received every few minutes. The problems started when I started using one of my Raspberry Pi 3 to host some local services that I would use only once a week… and every time I found the Raspberry to be disconnected from the local network and I was unable to communicate with it.
My network configuration
I think at this point it is essential to lay out my network configuration (at least the part related to my Raspberry Pi). My Raspberry Pi is connected through ethernet on an unmanaged ethernet hub, which is then connected to a router, which in turn is connected to my internet provider’s modem/router through ethernet.

Searching for a solution
There are many discussions around the internet [on Reddit, on Raspberry Forum, on Stack Exchange, on openHUB] talking about possible solutions and what could be going wrong. Many people suggest turning off IPv6, setting a fixed IP, changing ethernet cables etc. I tested most of them, mainly focusing on software-related solutions — I wanted to fix the problem on my current network setup rather than changing it so that it works with my Raspberry — but none of these solutions worked. Here are some info on various possible solutions suggested by the community for similar problems posted by others:
- Disabling the eee didn’t solved my issue, but could help. Here is the command:
sudo ethtool --set-eee eth0 eee off
- No power issues since I am using an official Raspberry Pi 4 power supply with enough ampere to power the system
- No SD cards issues as I was booting my Raspberry Pi of an SSD.
- Setting a static IP did no solve the issue, but it was also not an acceptable solution.
There was one particular solution, suggested by many people on multiple network-related issues, but I wanted to avoid it as it was more like a walk-around… that was to set up a script that would monitor the network for problems and reset it if needed. Since I was left with no choice… this was my only option.
Preparing the network monitor script
I wanted an automated script that would be able to handle any configuration without editing it. Thus I wrote one with the following features:
- Automatic detect the interface and the gateway
- Ping the gateway to check if it can be reached
- Reset the interface if there is a problem
- Wait for the system to connect to an interface after boot
- Keep log in a file
Based on that, here is the final script:
#!/bin/bash
# Log output
exec &>> "./${0##*/}.log"
# Maximum wait time in seconds
NET_MAX_WAIT=120
NET_WAIT_TIME=5
echo "Checking for network readiness..."
# Loop to check for network availability
while [ $NET_MAX_WAIT -gt 0 ]; do
GATEWAY=$(ip route | awk '/default/ {print $3}' | head -n 1)
INTERFACE=$(ip route | awk '/default/ {print $5}' | head -n 1)
if [ -n "$GATEWAY" ] && [ -n "$INTERFACE" ]; then
echo "Network is ready. Gateway: $GATEWAY, Interface: $INTERFACE"
break
fi
echo "Network not ready, retrying in $NET_WAIT_TIME seconds..."
sleep $NET_WAIT_TIME
NET_MAX_WAIT=$((NET_MAX_WAIT - NET_WAIT_TIME))
done
# If network is still not ready, exit the script
if [ -z "$GATEWAY" ] || [ -z "$INTERFACE" ]; then
echo "Could not determine gateway or network interface after timeout. Exiting."
exit 1
fi
echo "Monitoring network connectivity to gateway: $GATEWAY via interface: $INTERFACE"
while true; do
if ! ping -c 1 -W 2 "$GATEWAY" > /dev/null; then
echo "Network unreachable. Restarting interface $INTERFACE..."
sudo ip link set "$INTERFACE" down
sleep 2
sudo ip link set "$INTERFACE" up
fi
sleep 10
done
I saved the script under my home folder at /home/pi/
and named it keep_network_alive.sh
. To automatically start the script on system start-up, I added the following line on Cron by running crontab -e
:
@reboot /bin/bash /home/pi/keep_network_alive.sh &
Conclusion
By setting up the system to run this script on boot, the disconnects stopped. I would not call it a fix, but a walk around… better than nothing.