# Set system time automatically on Linux

## Introduction  


It is possibly to set and synchronize the time in Linux automatically through the systemd service. It's the successor of NTP daemon. In my network the mt-engine01.simmy.ch provides system time. Hence the device can change, I created an DNS alias ntp.simmy.ch. Using this alias allows changes of the time source without problems.

## Ubuntu 22.04 LTS  


```bash
nano /etc/systemd/timesyncd.conf
```

```ini
This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it under the
#  terms of the GNU Lesser General Public License as published by the Free
#  Software Foundation; either version 2.1 of the License, or (at your option)
#  any later version.
#
# Entries in this file show the compile time defaults. Local configuration
# should be created by either modifying this file, or by creating "drop-ins" in
# the timesyncd.conf.d/ subdirectory. The latter is generally recommended.
# Defaults can be restored by simply deleting this file and all drop-ins.
#
# See timesyncd.conf(5) for details.

[Time]
NTP=ntp.simmy.ch
FallbackNTP=0.pool.ntp.org, 1.pool.ntp.org, 2.pool.ntp.org
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048
```

```bash
systemctl restart systemd-timesyncd
```

## Debian

```bash
apt install systemd-timesyncd
nano /etc/systemd/timesyncd.conf
```

```ini
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it under the
#  terms of the GNU Lesser General Public License as published by the Free
#  Software Foundation; either version 2.1 of the License, or (at your option)
#  any later version.
#
# Entries in this file show the compile time defaults. Local configuration
# should be created by either modifying this file, or by creating "drop-ins" in
# the timesyncd.conf.d/ subdirectory. The latter is generally recommended.
# Defaults can be restored by simply deleting this file and all drop-ins.
#
# See timesyncd.conf(5) for details.

[Time]
NTP=ntp.simmy.ch
FallbackNTP=0.pool.ntp.org, 1.pool.ntp.org, 2.pool.ntp.org
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048

```

```bash
systemctl restart systemd-timesyncd
```

## Set the correct timezone

Figure out the timezone:

```bash
timedatectl list-timezones | grep Europe
```

Set the timezone

```bash
timedatectl set-timezone Europe/Zurich
```

## Useful commands / checks

```bash
root@iVentoy ~# timedatectl status
               Local time: Sat 2024-01-06 16:21:29 CET
           Universal time: Sat 2024-01-06 15:21:29 UTC
                 RTC time: Sat 2024-01-06 15:21:29
                Time zone: Europe/Zurich (CET, +0100)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
root@iVentoy ~#
```

```bash
root@iVentoy ~# systemctl status systemd-timesyncd
* systemd-timesyncd.service - Network Time Synchronization
     Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; preset: enabled)
     Active: active (running) since Sat 2024-01-06 15:51:09 CET; 31min ago
       Docs: man:systemd-timesyncd.service(8)
   Main PID: 3022 (systemd-timesyn)
     Status: "Contacted time server 46.140.15.108:123 (0.debian.pool.ntp.org)."
      Tasks: 2 (limit: 4650)
     Memory: 1.3M
        CPU: 39ms
     CGroup: /system.slice/systemd-timesyncd.service
             `-3022 /lib/systemd/systemd-timesyncd

Jan 06 15:51:09 iVentoy systemd[1]: Starting systemd-timesyncd.service - Network Time Synchronization...
Jan 06 15:51:09 iVentoy systemd[1]: Started systemd-timesyncd.service - Network Time Synchronization.
Jan 06 15:51:09 iVentoy systemd-timesyncd[3022]: Contacted time server 46.140.15.108:123 (0.debian.pool.ntp.org).
Jan 06 15:51:09 iVentoy systemd-timesyncd[3022]: Initial clock synchronization to Sat 2024-01-06 14:51:09.538088 UTC.
root@iVentoy ~#
```

```bash
date
```

## Useful links

<span class="wikiexternallink">[https://www.digitalocean.com/community/tutorials/how-to-set-up-time-synchronization-on-debian-10](https://www.digitalocean.com/community/tutorials/how-to-set-up-time-synchronization-on-debian-10)</span>