One-time set date/time on Ubuntu without NTP, using proxychains
set-time-once.sh
#!/usr/bin/env bash
set -euo pipefail
# ============================================================
# One-time Ubuntu date/time setter without NTP
# Uses HTTP Date header through proxychains/proxychains4
# ============================================================
# Optional timezone.
# Change this if needed.
TIMEZONE="Asia/Tehran"
# HTTP URLs only. Avoid HTTPS because wrong system time can break TLS validation.
URLS=(
"http://www.google.com/generate_204"
"http://example.com"
"http://neverssl.com"
"http://cloudflare.com"
)
# ------------------------------------------------------------
# Check root
# ------------------------------------------------------------
if [[ "$EUID" -ne 0 ]]; then
echo "Please run as root:"
echo "sudo $0"
exit 1
fi
# ------------------------------------------------------------
# Find proxychains command
# ------------------------------------------------------------
if command -v proxychains4 >/dev/null 2>&1; then
PROXYCHAINS="proxychains4"
elif command -v proxychains >/dev/null 2>&1; then
PROXYCHAINS="proxychains"
else
echo "Error: proxychains or proxychains4 not found."
echo "Install it first, for example:"
echo "sudo apt install proxychains4"
exit 1
fi
# ------------------------------------------------------------
# Find curl
# ------------------------------------------------------------
if ! command -v curl >/dev/null 2>&1; then
echo "Error: curl is not installed."
echo "Install it first:"
echo "sudo apt install curl"
exit 1
fi
echo "Using: $PROXYCHAINS"
echo "Disabling NTP..."
timedatectl set-ntp false || true
echo "Setting timezone to: $TIMEZONE"
timedatectl set-timezone "$TIMEZONE"
# ------------------------------------------------------------
# Fetch Date header
# ------------------------------------------------------------
HTTP_DATE=""
for URL in "${URLS[@]}"; do
echo "Trying: $URL"
HTTP_DATE="$(
"$PROXYCHAINS" -q curl -sI --max-time 20 "$URL" \
| awk -F': ' 'tolower($1)=="date"{print $2}' \
| tr -d '\r' \
| tail -n 1
)"
if [[ -n "$HTTP_DATE" ]]; then
echo "Received internet time: $HTTP_DATE"
break
fi
done
if [[ -z "$HTTP_DATE" ]]; then
echo "Error: could not get Date header from any server."
echo "Check your proxychains config and internet access."
exit 1
fi
# ------------------------------------------------------------
# Set system clock
# ------------------------------------------------------------
echo "Setting system clock..."
date -s "$HTTP_DATE"
# ------------------------------------------------------------
# Save system time to hardware clock
# Ubuntu normally expects RTC/hardware clock in UTC.
# ------------------------------------------------------------
echo "Writing system time to hardware clock as UTC..."
hwclock --systohc --utc
echo
echo "Done ✅"
echo
echo "Current system time:"
date
echo
echo "timedatectl status:"
timedatectl
echo
echo "Hardware clock:"
hwclock --show
chmod +x set-time-once.sh sudo ./set-time-once.sh
For a different timezone
Edit this line:
TIMEZONE="Europe/Berlin"
For example:
TIMEZONE="Europe/Berlin"
or:
TIMEZONE="UTC"