Menu
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.”
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:
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 anythingTue 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:
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.
sudo hwclock # same as hwclock --show or -r2026-12-01 14:30:12.482910+01:00
The two copy directions are the ones to remember:
| Command | Short | Direction |
|---|---|---|
hwclock --systohc | -w | system clock to hardware clock |
hwclock --hctosys | -s | hardware clock to system 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:
sudo hwclock --systohc --utchwclock 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:
timedatectl 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:
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/AmsterdamIf 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:
ls -l /etc/localtimelrwxrwxrwx 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.
cat /etc/timezone # Debian/UbuntuEurope/Amsterdam
Ways to change the timezone:
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 nametzselect 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:
TZ=Asia/Tokyo date
TZ=UTC dateNTP: 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.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:
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 ::1servernames one server,poola DNS name that returns many.iburstsends 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):
sudo apt install ntp # Debian/Ubuntu
sudo systemctl enable --now ntpsecThen check it with ntpq, the NTP query tool. -p lists the peers:
ntpq -p 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. stis the stratum,pollthe seconds between queries,offsetanddelayare in milliseconds.reachis an octal record of the last 8 tries.377means all 8 answered.
ntpdate: a one-shot fix#
ntpdate sets the clock once and exits. It’s deprecated but still on the exam:
sudo ntpdate pool.ntp.org 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:
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 LANmakestep 1.0 3lets 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.rtcsynchas the kernel copy the system time to the hardware clock every 11 minutes.
sudo dnf install chrony # RHEL/Fedora (usually already there)
sudo systemctl enable --now chronyd
sudo apt install chrony # Debian/Ubuntu: replaces systemd-timesyncdchronyc is the command-line client:
chronyc sourcesMS 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:
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 slewingExam 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/adjtimesays UTC or LOCAL.dateshows and sets the system clock:date -s "YYYY-MM-DD hh:mm"ordate MMDDhhmm[[CC]YY][.ss].timedatectldoes it all on systemd:set-time,set-timezone,list-timezones,set-ntp,set-local-rtc./etc/localtimeis a symlink to a file in/usr/share/zoneinfo/./etc/timezoneholds the zone name on Debian/Ubuntu.TZoverrides 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). servernames one host,poola DNS name such as0.pool.ntp.orgthat returns many.iburstspeeds up the first sync.ntpq -plists ntpd’s peers,*= synced source.chronyc sourcesandchronyc trackingdo the same for chrony.ntpdateis a deprecated one-shot sync that can’t run alongside ntpd.
Answer these 3 questions to commit this byte to your learning path.
Reset. This byte isn't committed any more. Answer the questions again to commit it.
$ git commit -m "learn(lpic-108): Maintain system time"
[main 9cd3557] learn(lpic-108): Maintain system time
1 byte changed, 3 insertions(+)
See it on your path Already committed . Try the questions again any time, it won't undo anything. To uncommit it and start over: