Network is Unreachable Error Fix
In this tutorial, you'll learn about Network is Unreachable Error Fix. We cover key concepts, practical examples, and best practices.
The Network is unreachable error means the operating system cannot find any network route to the destination IP address. This is caused by disconnected cables, missing default gateway, incorrect IP configuration, or routing table issues.
The Wrong Way
import requests
try:
response = requests.get("https://192.168.99.99")
print(response.status_code)
except requests.exceptions.ConnectionError as e:
print(f"Error: {e}")
Output:
Error: HTTPConnectionPool(host='192.168.99.99', port=443):
Max retries exceeded (Caused by NewConnectionError: [Errno 101] Network is unreachable)
The Right Way
Check network interfaces and routing before connecting:
import socket
import subprocess
def check_network_connectivity(host, port):
# Check if we have a default route
try:
result = subprocess.run(
["ip", "route", "show", "default"],
capture_output=True, text=True
)
if not result.stdout:
print("No default gateway configured")
return False
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(5)
sock.connect((host, port))
sock.close()
return True
except socket.error as e:
print(f"Cannot reach {host}:{port} - {e}")
return False
if check_network_connectivity("8.8.8.8", 53):
print("Network is reachable")
Step-by-Step Fix
1. Check network interfaces
# Show all network interfaces
ip addr show
# Check if interface is up
ip link show
# Check connection status
nmcli device status
2. Verify routing table
# Show routing table
ip route show
# Check default gateway
route -n | grep ^0.0.0.0
3. Test basic connectivity
# Ping the gateway
ping -c 3 $(ip route | grep default | awk '{print $3}')
# Ping a public IP (not DNS - avoid DNS dependency)
ping -c 3 8.8.8.8
4. Restart the network interface
# Bring interface down and up
sudo ip link set eth0 down
sudo ip link set eth0 up
# Or use systemd
sudo systemctl restart networking
5. Check for firewall blocking all traffic
# Check iptables rules
sudo iptables -L -n -v
# Flush rules for testing
sudo iptables -F
6. Configure a default gateway
sudo ip route add default via 192.168.1.1 dev eth0
Prevention Tips
- Check physical network connections (cables, Wi-Fi) first.
- Verify the default gateway is correctly configured.
- Monitor interface status with
ip monitor. - Set up network failover with bonding or teaming.
- Use
ping -c 1 8.8.8.8as a quick connectivity test.
Common Mistakes with unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad
These mistakes appear frequently in real-world NETWORK code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro