# Maintain system time

> Read and set the clock, keep the hardware clock in UTC, pick a timezone and sync with NTP using ntpd or chrony.

Source: https://aminbytes.dev/bytes/essential-system-services/system-time/


A wrong clock breaks more than you'd think. TLS certificates look expired or "not yet valid", Kerberos logins fail, `make` rebuilds the wrong files, cron jobs fire at odd hours, and log lines from two servers can't be put in order. Keeping time right is one of the quiet jobs every Linux admin owns.

Linux actually has **two** clocks, plus a timezone on top. Once you see how they fit together, the commands make sense.

## Two clocks: system time and hardware time

| Clock | Where it lives | Who uses it |
| --- | --- | --- |
| **System clock** (software clock) | In the running kernel's memory | Everything: programs, logs, file timestamps |
| **Hardware clock** (RTC, real-time clock, CMOS clock) | A battery-powered chip on the motherboard | Only read at boot, so the kernel knows the time before the network is up |

At boot, the kernel (or systemd) reads the hardware clock to set the system clock. After that, the system clock runs on its own, and NTP keeps correcting it. The hardware clock drifts slowly, so you copy the good system time back to it now and then.

The kernel always counts time in **UTC** internally. The timezone is only applied when a time is shown to you. The hardware clock should be in UTC too, unless the machine dual-boots with Windows, which expects local time.

## Reading and setting the time with date

`date` shows the system clock, in your timezone:

```bash
date
date -u                      # the same moment in UTC
date "+%Y-%m-%d %H:%M:%S"    # your own format
date -d "next friday"        # do date maths without changing anything
```

```console
Tue Dec  1 14:30:12 CET 2026
Tue Dec  1 13:30:12 UTC 2026
2026-12-01 14:30:12
Fri Dec  4 00:00:00 CET 2026
```

As root, `date` can also **set** the system clock. There are two styles:

```bash
sudo date -s "2026-12-01 14:30:00"    # GNU style, easy to read
sudo date 120114302026.00             # classic style: MMDDhhmm[[CC]YY][.ss]
```

The classic format reads as month 12, day 01, 14:30, year 2026, 00 seconds. Setting the time by hand only changes the system clock, and an NTP daemon may move it right back.

> [!NOTE]
> You can't change the clock inside a Docker container: the container shares the host's kernel clock and lacks the permission. Use a virtual machine to practise setting the time. Reading it works anywhere.

## The hardware clock with hwclock

`hwclock` (from util-linux) reads and writes the RTC. It needs root.

```bash
sudo hwclock                 # same as hwclock --show or -r
```

```console
2026-12-01 14:30:12.482910+01:00
```

The two copy directions are the ones to remember:

| Command | Short | Direction |
| --- | --- | --- |
| `hwclock --systohc` | `-w` | **sys**tem clock **to** **h**ardware **c**lock |
| `hwclock --hctosys` | `-s` | **h**ardware **c**lock **to** **sys**tem clock |
| `hwclock --utc` / `--localtime` | `-u` / `-l` | Say which scale the RTC keeps (combine with the above) |

So, to store the correct time in the hardware clock in UTC:

```bash
sudo hwclock --systohc --utc
```

`hwclock` remembers the choice in `/etc/adjtime`. Its third line is `UTC` or `LOCAL`. When NTP keeps the system clock synced, the kernel also copies it to the RTC every 11 minutes, so you rarely need to do this by hand.

## timedatectl: the systemd way

On systemd machines, `timedatectl` handles the clock, the RTC mode, the timezone and NTP from one place:

```bash
timedatectl
```

```console
               Local time: Tue 2026-12-01 14:30:12 CET
           Universal time: Tue 2026-12-01 13:30:12 UTC
                 RTC time: Tue 2026-12-01 13:30:12
                Time zone: Europe/Amsterdam (CET, +0100)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
```

`RTC in local TZ: no` means the hardware clock is in UTC, which is what you want. The other subcommands:

```bash
sudo timedatectl set-time "2026-12-01 14:30:00"   # set the clock (fails while NTP is on)
sudo timedatectl set-ntp false                    # turn network time sync off
sudo timedatectl set-ntp true                     # and back on
sudo timedatectl set-local-rtc 0                  # keep the RTC in UTC
timedatectl list-timezones | grep Europe
sudo timedatectl set-timezone Europe/Amsterdam
```

If NTP is on, `set-time` refuses with `Failed to set time: Automatic time synchronization is enabled`. Turn it off first, or better, let NTP fix the clock.

## Timezones

Timezone rules (offsets, daylight saving dates) come from the tz database in **`/usr/share/zoneinfo/`**, one binary file per zone, like `/usr/share/zoneinfo/Europe/Amsterdam`.

The system timezone is **`/etc/localtime`**. On current distributions it's a symlink to one of those files:

```bash
ls -l /etc/localtime
```

```console
lrwxrwxrwx 1 root root 36 Nov  3 09:12 /etc/localtime -> /usr/share/zoneinfo/Europe/Amsterdam
```

Debian and Ubuntu also keep the zone's name as plain text in **`/etc/timezone`**. RHEL and Fedora don't have that file.

```bash
cat /etc/timezone          # Debian/Ubuntu
```

```console
Europe/Amsterdam
```

Ways to change the timezone:

```bash
sudo timedatectl set-timezone Europe/Amsterdam                       # any systemd distro
sudo ln -sf /usr/share/zoneinfo/Europe/Amsterdam /etc/localtime      # by hand
sudo dpkg-reconfigure tzdata                                         # Debian/Ubuntu menu
tzselect                                                             # asks questions, prints the zone name
```

`tzselect` doesn't change anything: it helps you find the right name and suggests a line for your shell profile. A single user or command can override the system zone with the `TZ` variable:

```bash
TZ=Asia/Tokyo date
TZ=UTC date
```

## NTP: keeping the clock right

The **Network Time Protocol** (NTP) asks time servers on the network what time it is, measures the network delay, and gently speeds up or slows down the system clock until it's right. It runs over UDP port 123.

NTP servers form layers called **strata**. Stratum 0 is a reference clock (atomic clock, GPS). A server wired to one is stratum 1, a server syncing from that is stratum 2, and so on. Stratum 16 means "not synchronized".

### pool.ntp.org

You don't need to pick servers yourself. **pool.ntp.org** is a big volunteer project: its DNS names hand out a different set of servers on every lookup.

| Name | Gives you |
| --- | --- |
| `pool.ntp.org`, `0.pool.ntp.org` to `3.pool.ntp.org` | Servers from anywhere, the numbered names return different sets |
| `europe.pool.ntp.org`, `nl.pool.ntp.org` | Servers on a continent or in a country |
| `2.debian.pool.ntp.org`, `2.rhel.pool.ntp.org` | Vendor zones used in distribution defaults |

Use the `pool` keyword in the config files below so the daemon picks several servers from one name. (Side note: Vendors who ship hardware with an NTP server built in are asked to request their own vendor zone. A router brand once hard-coded a single university server and flooded it with traffic from hundreds of thousands of devices.)

### Which daemon does your distro use?

| Distro | Default | Config file |
| --- | --- | --- |
| RHEL 9, Fedora | chrony (`chronyd.service`) | `/etc/chrony.conf` |
| Debian 12, Ubuntu 24.04 | systemd-timesyncd (a small client) | `/etc/systemd/timesyncd.conf` |
| Debian/Ubuntu with chrony installed | chrony (`chrony.service`) | `/etc/chrony/chrony.conf` |
| Older systems, or the `ntp`/`ntpsec` package | ntpd | `/etc/ntp.conf` (`/etc/ntpsec/ntp.conf` with NTPsec) |

RHEL 8 and later don't ship ntpd at all. Run only **one** time daemon at a time: they all want port 123.

## ntpd and ntpq

**ntpd** is the classic reference implementation. It reads **`/etc/ntp.conf`**:

```text {title="/etc/ntp.conf"}
driftfile /var/lib/ntp/ntp.drift

# take four servers from the pool
pool 0.pool.ntp.org iburst
pool 1.pool.ntp.org iburst

# or name single servers
# server ntp1.example.com iburst

# answer time queries, but refuse changes and status queries from others
restrict default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1
```

- `server` names one server, `pool` a DNS name that returns many.
- `iburst` sends a quick burst of packets at startup, so the first sync takes seconds instead of minutes.
- The **driftfile** stores how fast this machine's clock tends to run, so ntpd corrects it right away after a reboot.

Install and start it (Debian 12 installs NTPsec, a maintained fork of ntpd, for the `ntp` package):

```bash
sudo apt install ntp            # Debian/Ubuntu
sudo systemctl enable --now ntpsec
```

Then check it with **ntpq**, the NTP query tool. `-p` lists the peers:

```bash
ntpq -p
```

```console
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 0.pool.ntp.org  .POOL.          16 p    -   64    0    0.000   +0.000   0.000
*ntp1.example.nl 193.79.237.14    2 u   35   64  377    4.512   -0.231   0.118
+ntp2.example.de .PPS.            1 u   40   64  377   12.003   +0.402   0.207
-ntp3.example.fr 145.238.203.14   2 u   22   64  377   18.774   +2.915   0.540
```

- The first character: `*` is the server ntpd follows now, `+` a good candidate, `-` an outlier it ignores.
- `st` is the stratum, `poll` the seconds between queries, `offset` and `delay` are in milliseconds.
- `reach` is an octal record of the last 8 tries. `377` means all 8 answered.

### ntpdate: a one-shot fix

**ntpdate** sets the clock once and exits. It's deprecated but still on the exam:

```bash
sudo ntpdate pool.ntp.org
```

```console
 1 Dec 14:30:12 ntpdate[2345]: adjust time server 162.159.200.1 offset +0.003214 sec
```

It fails with "the NTP socket is in use" while ntpd is running. Today you'd use `ntpd -gq` (set once and quit) or `chronyd -q` instead. Why did people run `ntpdate` before starting ntpd? Because ntpd exits if the clock is more than 1000 seconds off. The `-g` option lifts that limit for the first correction.

## chrony and chronyc

**chrony** is the modern replacement. It syncs faster, copes well with laptops and VMs that sleep or lose the network, and is the default on RHEL. The daemon is `chronyd`. The config:

```text {title="/etc/chrony.conf"}
pool 2.rhel.pool.ntp.org iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
# allow 192.168.1.0/24      # uncomment to serve time to your LAN
```

- `makestep 1.0 3` lets chrony **step** (jump) the clock if it's more than 1 second off, but only during the first 3 updates. After that it only slews.
- `rtcsync` has the kernel copy the system time to the hardware clock every 11 minutes.

```bash
sudo dnf install chrony                  # RHEL/Fedora (usually already there)
sudo systemctl enable --now chronyd
sudo apt install chrony                  # Debian/Ubuntu: replaces systemd-timesyncd
```

**chronyc** is the command-line client:

```bash
chronyc sources
```

```console
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* time.cloudflare.com           3   6   377    35   +102us[ +145us] +/-   12ms
^+ ntp1.example.nl               2   6   377    34   -210us[ -167us] +/-   18ms
^- ntp2.example.de               2   6   377    33  +1802us[+1845us] +/-   40ms
```

`^` means a server, and the second column uses the same `*`, `+`, `-` marks as ntpq. Other handy commands:

```bash
chronyc tracking          # how far off the system clock is, current stratum
chronyc sourcestats       # drift and error per source
sudo chronyc makestep     # step the clock right now instead of slewing
```

## Exam checklist

- The system clock lives in the kernel, the hardware clock (RTC) on the motherboard. Keep the RTC in **UTC**.
- `hwclock --systohc` (`-w`) writes system time to the RTC, `--hctosys` (`-s`) does the reverse. `/etc/adjtime` says UTC or LOCAL.
- `date` shows and sets the system clock: `date -s "YYYY-MM-DD hh:mm"` or `date MMDDhhmm[[CC]YY][.ss]`.
- `timedatectl` does it all on systemd: `set-time`, `set-timezone`, `list-timezones`, `set-ntp`, `set-local-rtc`.
- `/etc/localtime` is a symlink to a file in `/usr/share/zoneinfo/`. `/etc/timezone` holds the zone name on Debian/Ubuntu. `TZ` overrides it per user or command.
- NTP uses UDP port 123. ntpd reads `/etc/ntp.conf`, chrony reads `/etc/chrony.conf` (RHEL) or `/etc/chrony/chrony.conf` (Debian).
- `server` names one host, `pool` a DNS name such as `0.pool.ntp.org` that returns many. `iburst` speeds up the first sync.
- `ntpq -p` lists ntpd's peers, `*` = synced source. `chronyc sources` and `chronyc tracking` do the same for chrony.
- `ntpdate` is a deprecated one-shot sync that can't run alongside ntpd.


## Byte check

1. You want the hardware clock to match the system clock, which was just corrected by NTP. Which command do you run?
   - **`hwclock --systohc`** (correct)
   - `hwclock --hctosys`
   - `date --systohc`
   `--systohc` copies the system time **to** the hardware clock. `--hctosys` goes the other way, and `date` has no such option.

2. In the output of `ntpq -p`, what does a `*` in front of a server name mean?
   - The server is unreachable
   - **It's the server ntpd is currently synchronized to** (correct)
   - It's a pool name that hasn't been resolved yet
   `*` marks the system peer, the source ntpd follows right now. `+` marks good candidates, `-` and `x` mark rejected ones.

3. On a RHEL 9 system, which file holds the NTP servers used by the default time daemon?
   - `/etc/ntp.conf`
   - `/etc/timezone`
   - **`/etc/chrony.conf`** (correct)
   RHEL 9 ships only chrony, configured in `/etc/chrony.conf`. On Debian and Ubuntu the same file lives at `/etc/chrony/chrony.conf`.


