Accurate time synchronization is critical. In modern software, the user ensures if logs are recorded in the right order or database updates are applied correctly, out-of-sync time can cause errors, data corruption, and other difficult issues to debug.
In this article, you would see that Debian 10 is time synchronization built-in, it is activated using the standard ntpd time server by default and provided by the ntp package. To let your studies work better, choose your own Ubuntu VPS Server with instant activation.
To let this tutorial work better, please consider the below Prerequisites:
A non-root user with sudo privileges
To set up, follow our Initial Setup with Debian 10
Table of Contents
Setting Up Time Synchronization On Debian 10
Step 1: How To Navigate Basic Time Commands
You can use date to find out the time on your server. all users are able to run the below command and print out the date and time.
date
Wed 31 Jul 2019 06:03:19 PM UTC
Your server will default to the UTC time zone as you see in the output. What is UTC? It is Coordinated Universal Time, the time at zero degrees longitude. Consistently using Universal Time reduces confusion when your infrastructure spans multiple time zones. But also you can use the timedatectl command if you have different requirements and need to change the time zone.
First, list the available time zones:
timedatectl list-timezones
To print your screen, you need a list of time zone. By pressing SPACE to page down and b to page up. Do not forget to make note of it when you find the correct time zone and then type q to exit the list.
It is time to set the time zone with timedatectl set-timezone. Make sure to replace the (America/New_York) with the time zone you find on the list. Remember to use sudo with timedatectl to make this change:
sudo timedatectl set-timezone America/New_York
To verify your changes, you can run date again:
date
Output
Wed 31 Jul 2019 02:08:43 PM EDT
The time zone abbreviation should reflect the newly chosen value. From now on you know how to check the clock and set time zones. So we are going to make sure our time is being synchronized properly.
Step 2: How To Check The Status Of ntpd
You can trust Debian 10 bout presenting you the standard ntpd server to keep your system time synchronized with a pool of external time servers as it runs ntpd by default.
To check it is running, use the below command:
sudo systemctl status ntp
Output
● ntp.service - Network Time Service Loaded: loaded (/lib/systemd/system/ntp.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2019-07-31 13:57:08 EDT; 17min ago Docs: man:ntpd(8) Main PID: 429 (ntpd) Tasks: 2 (limit: 1168) Memory: 2.1M CGroup: /system.slice/ntp.service └─429 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 106:112 . . .
When you see the active (running) status in the output, it means that ntpd started up properly. To get more information about the status of ntpd you can use the ntpq command:
ntpq -p
remote refid st t when poll reach delay offset jitter ============================================================================== 0.debian.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000 1.debian.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000 2.debian.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000 3.debian.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000 +208.67.72.50 152.2.133.55 2 u 12 64 377 39.381 1.696 0.674 +198.46.223.227 204.9.54.119 2 u 6 64 377 22.671 3.536 1.818 -zinc.frizzen.ne 108.61.56.35 3 u 43 64 377 12.012 1.268 2.553 -pyramid.latt.ne 204.123.2.72 2 u 11 64 377 69.922 2.858 0.604 +nu.binary.net 128.252.19.1 2 u 10 64 377 35.362 3.148 0.587 #107.155.79.108 129.7.1.66 2 u 65 64 377 42.380 1.638 1.014 +t1.time.bf1.yah 98.139.133.62 2 u 6 64 377 11.233 3.305 1.118 *sombrero.spider 129.6.15.30 2 u 47 64 377 1.304 2.941 0.889 +hydrogen.consta 209.51.161.238 2 u 45 64 377 1.830 2.280 1.026 -4.53.160.75 142.66.101.13 2 u 42 64 377 29.077 2.997 0.789 #horp-bsd01.horp 146.186.222.14 2 u 39 64 377 16.165 4.189 0.717 -ntpool1.603.new 204.9.54.119 2 u 46 64 377 27.914 3.717 0.939
ntpq is a query tool for ntpd. The -p flag asks for information about the NTP servers (or peers) ntpd is connected to. Your output will be slightly different but should list the default Debian pool servers plus a few others. Bear in mind that it can take a few minutes for ntpd to establish connections.
Step 3: How To Switch To systemd-timesyncd
In case you need to replace ntpd, you can use systemd’s built-in timesyncd component. timesyncd is a lighter-weight alternative to ntpd that is more integrated with systemd. Note, however, that it doesn’t support running as a time server, and it is slightly less sophisticated in the techniques it uses to keep your system time in sync. If you are running complex real-time distributed systems, you may want to stick with ntpd.
First, uninstall ntpd to use timesyncd:
sudo apt purge ntp
Then, start up the timesyncd service:
sudo systemctl start systemd-timesyncd
Finally, check the status of the service to make sure it’s running:
sudo systemctl status systemd-timesyncd
● systemd-timesyncd.service - Network Time Synchronization Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled) Drop-In: /usr/lib/systemd/system/systemd-timesyncd.service.d └─disable-with-time-daemon.conf Active: active (running) since Wed 2019-07-31 14:21:37 EDT; 6s ago Docs: man:systemd-timesyncd.service(8) Main PID: 1681 (systemd-timesyn) Status: "Synchronized to time server for the first time 96.245.170.99:123 (0.debian.pool.ntp.org)." Tasks: 2 (limit: 1168) Memory: 1.3M CGroup: /system.slice/systemd-timesyncd.service └─1681 /lib/systemd/systemd-timesyncd
You can use timedatectl to print out systemd’s current understanding of the time:
timedatectl
Local time: Wed 2019-07-31 14:22:15 EDT Universal time: Wed 2019-07-31 18:22:15 UTC RTC time: n/a Time zone: America/New_York (EDT, -0400) System clock synchronized: yes NTP service: active RTC in local TZ: no
This prints out the local time, universal time (which may be the same as local time, if you didn’t switch from the UTC time zone), and some networking time status information. System clock synchronized: yes means that the time has been successfully synced, and NTP service: active means that timesyncd is enabled and running.
conclusion
In this article, you learned how to view the system time, change time zones, work with ntpd, and switch to systemd’s timesyncd service. If you have more sophisticated timekeeping needs than what we’ve covered here, you might refer to the official NTP documentation, and also take a look at the NTP Pool Project, a global group of volunteers providing much of the world’s NTP infrastructure. If you are interested in this subject, read more related articles on Set date and time on Ubuntu 18 AND Set Timezone on CentOS 7 Linux.
hello, thanks. I want to set UTC timezone in Linux?
You need to use the below command to do this. , scroll to the bottom of the Continents list and select Etc or None of the above; in the second list, select UTC.
sudo dpkg-reconfigure tzdata
Is it possible to know is NTP is sync or not?
Yes sure, to ensure it, follow the below path please:
Use the ntpstat command to view the status of the NTP service on the instance. [ec2-user ~]$ ntpstat. …
Also you can use the ntpq -p command to see a list of peers known to the NTP server and a summary of their state.
Should I spend several times to Sync NTP?
Not at all, It is depends on an NTP server accepting as a synchronization source. And it takes about 5 minutes.
What if UFW is running?
In this situation, you can simply allow NTP incoming queries from the specific network to Open NTP on Firewall
ufw allow from 192.168.1.0/24 to any port 123 proto udp
Good tutorial. I need to restart the NTP server and verify if it is running or not, thank you.
To reach your target, run the below commands respectively:
systemctl restart ntp
systemctl status ntp