{
  "id": "time-sync",
  "title": "System Time & NTP",
  "category": "sysadmin",
  "tags": ["ntp", "time", "timedatectl", "timesyncd", "chrony", "drift"],
  "updated": "2025-07-14",
  "summary": "Keeping system clocks accurate and diagnosing time drift.",
  "sections": [
    {
      "heading": "Why System Time Matters",
      "body": "<p>Clocks that drift cause more problems than you expect: SSL certificate validation failures, log timestamps that do not correlate across machines, cron jobs that fire at the wrong time, authentication tokens that expire prematurely, and package signature checks that fail.</p><p>On a server, time should be correct to within a second. Most NTP implementations keep it within milliseconds.</p>"
    },
    {
      "heading": "Checking Current Time Status",
      "body": "",
      "code": "timedatectl\n# Shows: local time, UTC time, timezone, NTP sync status, RTC time\n\ntimedatectl show\n# Machine-readable version of the same"
    },
    {
      "heading": "systemd-timesyncd",
      "body": "<p>Most Debian/Ubuntu systems ship with <code>systemd-timesyncd</code> as the default NTP client. It is a lightweight SNTP implementation—adequate for most servers.</p>",
      "code": "# Enable and start\nsystemctl enable --now systemd-timesyncd\n\n# Check sync status\ntimedatectl timesync-status\n\n# Force a resync\nsystemctl restart systemd-timesyncd\n\n# Config file (NTP servers, fallback)\ncat /etc/systemd/timesyncd.conf"
    },
    {
      "heading": "NTP Server Configuration",
      "body": "<p>The default NTP servers are usually fine. If you need to change them—for example, to use an internal NTP server:</p>",
      "code": "# /etc/systemd/timesyncd.conf\n[Time]\nNTP=ntp.internal.example.com\nFallbackNTP=0.debian.pool.ntp.org 1.debian.pool.ntp.org"
    },
    {
      "heading": "chrony (alternative)",
      "body": "<p>chrony is a more capable NTP implementation. It handles intermittent network connections and large initial offsets better than timesyncd. On systems where accuracy matters:</p>",
      "code": "apt install chrony\nsystemctl enable --now chrony\n\nchronyc tracking     # current sync status\nchronyc sources -v   # configured time sources and their offsets"
    },
    {
      "heading": "Diagnosing Time Problems",
      "body": "",
      "code": "# Is NTP enabled?\ntimedatectl | grep NTP\n\n# Is timesyncd active?\nsystemctl status systemd-timesyncd\n\n# Did a sync happen recently?\njournalctl -u systemd-timesyncd --since \"1 hour ago\"\n\n# What is the current offset?\ntimedatectl timesync-status | grep Offset"
    },
    {
      "heading": "Setting Timezone",
      "body": "",
      "code": "timedatectl list-timezones | grep Europe\ntimedatectl set-timezone Europe/London"
    }
  ]
}
