#!/bin/bash # Network troubleshooting script for LXC containers # Exit on any error set -e echo "Starting network troubleshooting..." # Check if we can ping IP addresses echo "Testing basic connectivity..." if ping -c 2 8.8.8.8 >/dev/null 2>&1; then echo "✅ Basic network connectivity works (can ping 8.8.8.8)" else echo "❌ Cannot ping 8.8.8.8 - basic network connectivity issue" echo "Checking network interfaces..." ip a fi # Check and fix DNS echo "Checking DNS configuration..." if ! grep -q "nameserver" /etc/resolv.conf || ! host archive.ubuntu.com >/dev/null 2>&1; then echo "❌ DNS resolution not working, fixing..." # Add Google DNS servers echo "nameserver 8.8.8.8 nameserver 8.8.4.4" > /etc/resolv.conf echo "✅ Added Google DNS servers to /etc/resolv.conf" # Make DNS changes persistent echo "Making DNS changes persistent..." mkdir -p /etc/systemd/resolved.conf.d/ cat > /etc/systemd/resolved.conf.d/dns_servers.conf << EOF [Resolve] DNS=8.8.8.8 8.8.4.4 FallbackDNS=1.1.1.1 EOF # Try to restart systemd-resolved if it exists if systemctl status systemd-resolved >/dev/null 2>&1; then systemctl restart systemd-resolved echo "✅ Restarted systemd-resolved" fi # Try to restart systemd-networkd if it exists if systemctl status systemd-networkd >/dev/null 2>&1; then systemctl restart systemd-networkd echo "✅ Restarted systemd-networkd" fi else echo "✅ DNS configuration looks good" fi # Add hosts entries as a fallback echo "Adding fallback entries to /etc/hosts..." if ! grep -q "archive.ubuntu.com" /etc/hosts; then cat >> /etc/hosts << EOF 185.125.190.36 archive.ubuntu.com 185.125.190.36 security.ubuntu.com EOF echo "✅ Added Ubuntu repositories to /etc/hosts" fi # Test DNS resolution echo "Testing DNS resolution..." if host archive.ubuntu.com >/dev/null 2>&1; then echo "✅ DNS resolution working for archive.ubuntu.com" else echo "❌ DNS resolution still not working for archive.ubuntu.com" echo "Trying ping with hosts file..." if ping -c 2 archive.ubuntu.com >/dev/null 2>&1; then echo "✅ Can ping archive.ubuntu.com using hosts file entry" else echo "❌ Still cannot reach archive.ubuntu.com" echo "This may be a more serious network issue with your LXC container" fi fi echo "Network troubleshooting completed" echo "Try running 'apt update' now to see if it works"