System - Synkroniser tid nu (alternativ til NTP)
Source: Download script
Last Updated: 11. September 2025 by Agnete Moos (agms@sonderborg.dk)
Click to see the source code
#! /usr/bin/env sh
# One-off synchronize time with htpdate.
# Used on instances where NTP ports or NTP synchronization in general are blocked.
# If possible, we recommend opening up in the firewall for NTP instead.
# This script may cause jobmanager to time out due to changes in the time settings
set -ex
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install --assume-yes htpdate
# Ensure that the htpdate service is disabled in case the script times out before htpdate is removed
systemctl disable --now htpdate
echo "time before updating"
date
htpdate -s www.pool.ntp.org dk.pool.ntp.org www.wikipedia.org
echo "time after updating"
date
# htpdate only updates the system clock so update the hardware clock from the system clock
hwclock --systohc
# Enabling this service as previous versions of the script disabled it
# because it was under suspicion for interfering and syncing the time back to be incorrect
# (if NTP is blocked by the firewall)
systemctl enable --now systemd-timesyncd
# Clean up and remove htpdate, as otherwise it leaves a service running which continually syncs time
# via htpdate
apt-get remove --assume-yes htpdate
Beskrivelse
Normalt i Ubuntu og Windows synkroniseres tiden løbende over internettet, via protokollen NTP. NTP kører normalt på port 123 over UDP.
Nærmere information om NTP: https://en.wikipedia.org/wiki/Network_Time_Protocol
Nogle kommuner har dog strikse firewalls der bl.a. blokerer NTP, og derved kan tiden på computeren efterhånden komme ud af synkronisering med den faktiske tid.
Hvis det er muligt, vil vi i stedet for dette script, klart anbefale at åbne op for og bruge NTP.
Dette script synkroniserer tiden en enkelt gang via protokollen HTTP Time Protocol (HTP), som modsat NTP vil fungere, så længe maskinen har almindelig netadgang (port 80/443). Programmet der bruges dertil hedder htpdate.
Teknisk set kan htpdate også sættes op til løbende at synkronisere, men det understøtter dette script IKKE på nuværende tidspunkt.
Dette script er blevet testet og virker på Ubuntu 22.04.