Linux sync local time

About time synchronisation

Network Time Protocol (NTP) is a networking protocol for synchronising time over a network. Basically, a client requests the current time from a server, and uses it to set its own clock.

Behind this simple description, there is a lot of complexity. There are three tiers of NTP servers; tier one NTP servers are connected to atomic clocks, while tier two and tier three three servers spread the load of actually handling requests across the Internet.

The client software is also a lot more complex than you might expect. It must factor in communication delays and adjust the time in a way that does not upset all the other processes that run on the server. Luckily, all that complexity is hidden from you!

By default, Ubuntu uses timedatectl / timesyncd to synchronise time, and they are available by default. See our guide If you would like to know how to configure timedatectl and timesyncd .

Users can also optionally use chrony to serve NTP.

How time synchronisation works

Since Ubuntu 16.04, timedatectl / timesyncd (which are part of systemd ) replace most of ntpdate / ntp .

About timesyncd

timesyncd replaces not only ntpdate , but also the client portion of chrony (formerly ntpd ). So, on top of the one-shot action that ntpdate provided on boot and network activation, timesyncd now regularly checks and keeps your local time in sync. It also stores time updates locally, so that after reboots the time monotonically advances (if applicable).

About timedatectl

If chrony is installed, timedatectl steps back to let chrony handle timekeeping. This ensures that no two time-syncing services can conflict with each other.

ntpdate is now considered deprecated in favor of timedatectl (or chrony ) and is no longer installed by default. timesyncd will generally keep your time in sync, and chrony will help with more complex cases. But if you had one of a few known special ntpdate use cases, consider the following:

  • If you require a one-shot sync, use: chronyd -q
  • If you require a one-shot time check (without setting the time), use: chronyd -Q

While use of ntpd is no longer recommended, this also still applies to ntpd being installed to retain any previous behaviour/config that you had through an upgrade. However, it also implies that on an upgrade from a former release, ntp / ntpdate might still be installed and therefore renders the new systemd -based services disabled.

Читайте также:  Enable wake on lan on linux

Further reading

  • ntp.org: home of the Network Time Protocol project
  • pool.ntp.org: project of virtual cluster of timeservers
  • Freedesktop.org info on timedatectl
  • Freedesktop.org info on systemd-timesyncd service
  • Chrony FAQ
  • Feeding chrony from GPSD
  • Also see the Ubuntu Time wiki page for more information.

Источник

Commands to Sync Time with NTP Server in Linux

For many people, computer clocks in your devices, network machines, and servers are generally accurate. But that’s not true! These clocks are manually maintained and backed by batteries which over time drift the clock, especially in the older machines.

So why is accurate time so important? Having exact time on your machine is quite significant because of several reasons. Many aspects of your computer activity are linked with time. Perfectly synched time is crucial for tracking security-related issues; troubleshooting can become quite difficult if the timestamps in log files are incorrect. Even for financial services, keeping accurate time is critical.

Many companies solve time-related issues by connecting their networks with NTP. So what is NTP? Let’s dig into it first:

What is NTP

The full form of NTP is “Network Time Protocol”, which has been one of the most authentic ways to synchronize the clock over a network. If your system uses NTP, you don’t need to check and set your time manually. It automatically updates the clock every time the device reboots. It is an extremely accurate way to update the clock of your device. Since the internet is everywhere, NTP is being used by every modern computer.

How to Enable NTP Synchronization on Linux

Most of the Linux distributions are using “systemd”, which comes with NTP for clock synchronization. To verify this, use the command given below:

It indicates that NTP is active. If, for some reasons, it is not active, then use the below-mentioned command to enable it:

How to Enable NTP for Linux Distributions without “systemd”

But what if your distribution does not come with “systemd”? Well, in that case, you can install NTP.

Источник

How To Set Up Time Synchronization on Ubuntu 20.04

How To Set Up Time Synchronization on Ubuntu 20.04

Accurate timekeeping is integral to modern software deployments. Without it, you may encounter data corruption, errors, and other issues that are difficult to debug. Time synchronization can help ensure your logs are being recorded in the correct order, and that database updates are appropriately applied.

Fortunately, Ubuntu 20.04 has time synchronization built-in and activated by default using systemd ’s timesyncd service. In this article, you will practice some general time-related commands, verify that timesyncd is active, and install an alternate network time service.

Prerequisites

Before starting this tutorial, you will need an Ubuntu 20.04 server with a non-root, sudo-enabled user and a firewall, as described in this Ubuntu 20.04 server setup tutorial.

Читайте также:  Размер папки linux сортировка

To view the time on your server, you will use the command date . Any user can run this command to print out the date and time:

Typically, your server will generate an output with the default UTC time zone.

Output
Thu Aug 5 15:55:20 UTC 2021

UTC is Coordinated Universal Time, the time at zero degrees longitude. While this may not reflect your current time zone, using Universal Time prevents confusion when your infrastructure spans multiple time zones.

If you want to change your time zone, however, you can use the timedatectl command.

First, run this command to generate a list of available time zones:

A list of time zones will print to your screen. You can press SPACE to page down, and b to page up. Once you find the correct time zone, make note of it then type q to exit the list.

Next, you can set the time zone with timedatectl set-timezone by replacing the highlighted portion with the time zone you found in the list. You’ll need to use sudo with timedatectl to make this change:

You can verify your changes by running date again:

Output
Thu Aug 5 11:56:01 EDT 2021

The time zone abbreviation will reflect the newly chosen value.

Now that you’ve practiced checking the clock and setting time zones, you can confirm that your time is being synchronized properly in the next section.

Controlling timesyncd with timedatectl

Previously, most network time synchronization was handled by the Network Time Protocol daemon or ntpd . This service connects to a pool of other NTP servers that provide it with constant and accurate time updates.

But now with Ubuntu’s default install, you can use timesyncd instead of ntpd . timesyncd works similarly by connecting to the same time servers, but is llightweight and more closely integrated with systemd on Ubuntu.

You can query the status of timesyncd by running timedatectl with no arguments. You don’t need to use sudo in this case:

Output
Local time: Thu 2021-08-05 11:56:40 EDT Universal time: Thu 2021-08-05 15:56:40 UTC RTC time: Thu 2021-08-05 15:56:41 Time zone: America/New_York (EDT, -0400) System clock synchronized: yes NTP service: active RTC in local TZ: no

This command 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 network time status information. System clock synchronized: yes reflects that the time is successfully synced, and NTP service: active means that timesyncd is up and running.

If your output shows that NTP service isn’t active, turn it on with timedatectl :

After this, run timedatectl again to confirm the network time status. It may take a minute for the sync to happen, but eventually System clock synchronized: will read yes and NTP service: will show as active .

Читайте также:  Linux default search path

Switching to ntpd

timesyncd will work in most circumstances. There are instances, however, when an application may be sensitive to any disturbance with time. In this case, ntpd is an alternative network time service you can use. ntpd uses sophisticated techniques to constantly and gradually keep the system time on track.

Before installing ntpd , you need to turn off timesyncd in order to prevent the two services from conflicting with one another. You can do this by disabling network time synchronization with the following command:

Verify that time synchronization is disabled:

Check that your output reads NTP service: inactive . This means timesyncd has stopped. Now you’re ready to install the ntp package with apt .

First, run apt update to refresh your local package index:

Then, run apt install ntp to install this package:

ntpd will begin automatically after your installation completes. You can verify that everything is working correctly by querying ntpd for status information:

Output
remote refid st t when poll reach delay offset jitter ============================================================================== 0.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000 1.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000 2.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000 3.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000 ntp.ubuntu.com .POOL. 16 p - 64 0 0.000 0.000 0.000 +t1.time.bf1.yah 129.6.15.28 2 u 16 64 1 61.766 -20.068 1.964 +puppet.kenyonra 80.72.67.48 3 u 16 64 1 2.622 -18.407 2.407 *ntp3.your.org .GPS. 1 u 15 64 1 50.303 -17.499 2.708 +time.cloudflare 10.4.1.175 3 u 15 64 1 1.488 -18.295 2.670 +mis.wci.com 216.218.254.202 2 u 15 64 1 21.527 -18.377 2.414 +ipv4.ntp1.rbaum 69.89.207.99 2 u 12 64 1 49.741 -17.897 3.417 +time.cloudflare 10.4.1.175 3 u 15 64 1 1.039 -16.692 3.378 +108.61.73.243 129.6.15.29 2 u 14 64 1 70.060 -16.993 3.363 +ny-time.gofile. 129.6.15.28 2 u 21 64 1 75.349 -18.333 2.763 golem.canonical 17.253.34.123 2 u 28 64 1 134.482 -21.655 0.000 ntp3.junkemailf 216.218.254.202 2 u 19 64 1 2.632 -16.330 4.387 clock.xmission. .XMIS. 1 u 18 64 1 24.927 -16.712 3.415 alphyn.canonica 142.3.100.2 2 u 26 64 1 73.612 -19.371 0.000 strongbad.voice 192.5.41.209 2 u 17 64 1 70.766 -18.159 3.481 chilipepper.can 17.253.34.123 2 u 25 64 1 134.982 -19.848 0.000 pugot.canonical 145.238.203.14 2 u 28 64 1 135.694 -21.075 0.000

ntpq is a query tool for ntpd . The -p flag requests information about the NTP servers (or peers) ntpd is connected to. Your output will be slightly different but will list the default Ubuntu pool servers plus a few others. Remember, it can take a few minutes for ntpd to establish connections.

Conclusion

In this article, you’ve successfully viewed the system time, changed time zones, worked with Ubuntu’s default timesyncd service, and installed ntpd . If you have advanced timekeeping needs, you can reference 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.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Источник

Оцените статью
Adblock
detector