Hardware clocks and computers are not perfect.
They slowly drift away from the real time
drifting means that after a few days, the real time might be 12 o'clock,
five seconds, but our server might show 12 o'clock and six seconds.
In
this case, it drifted one second ahead.
Now, servers need an accurate clock.
But you might have noticed that nowadays,
we almost never have to manually set our clocks.
Our phones, laptops and servers somehow magically know what time it is.
That's because they periodically get the exact time from the internet
and they get it from what are called time servers. More specifically NTP servers.
NTP is an abbreviation for network time protocol.
Now, most modern operating systems include time synchronization software.
By default
in
Ubuntu, the default one is included in the suite of systemd utilities
and it's called system D dash time sync D.
But before learning how to work with that utility,
we need to cover another important thing
in the real world.
We have a lot of time zones when it's 147 in Germany, it's 747 in Singapore.
And if we launch a server in Germany, it might be preconfigured to use that zone.
Now imagine we have servers in five different countries
looking at logs can get confusing.
We'll have to constantly add or subtract a few hours
to figure out when a certain logged event took place
relative to our own country's time.
So the first thing we might want to do after launching
a server is to set a specific time zone for it,
preferably the time zone where we live in or where our company's main office is
a time related operations can be performed with a time date CTL utility
to see the available time zones. We can type in
time date, CTL list dash time zones.
And there's a bunch of them and we can navigate this list with
the page up and page down keys or with the arrow keys.
And we can quit by pressing Q,
we can see that these zones are specified by typing the
name of the continent first followed by a slash character,
then the name of a city and usually it's a capital city of a country or state.
Now, not when the city contains two words,
we separate them with an underscore character, not a space character.
Now, we can check if our change took effect simply by typing
time date, CTL
and I can already have it installed. As you see,
then we can tell timedate
CTL to turn on synchronization with NTP
servers.
Finally, we can use the timedate CTL command with no arguments to see if the NTP
service is now active.
Now, we can also check the status of systemd's time sync daemon with this command,
it should show us that the service is active and enabled to autostart at boot.
We'll also see a few log lines showing us the NTP server. It's using
to change the settings of systemd's time synchronization daemon.
We can edit this file,
Etsy, Systemd
timesyncd.com.
Now, if we want to specify what NTP servers it should use,
we can uncomment this line by removing the preceding hashtag sign
and we can list multiple servers on the same line.
For example, we can type this out
note the format for the host names of these NTP servers.
They're often laid out in a similar way.
If the main NTP website is hosted at ntp.org,
then the NTP servers might be referenced by a subdomain
such as us.pool.ntp.org. In
this case, us is the country code for the United States.
And each collection of NTP servers is assigned to a
further subdomain represented by a number from zero to N.
So the first collection will be assigned to the zero.us dot pool.ntp.org subdomain
and the second collection will be one.us dot pool.ntp.org and so on.
Now, here's what our file looks like with our modifications.
The rest of the commented lines show us time sync these default options,
the fallback NTP will be used if nothing is specified in the NTP line.
Hence that was used before we made our modifications,
the route distance MAC sec entry specifies the maximum time in seconds that
it can take the NTP server to send network data to our machine.
If the NTP server exceeds this time, then the time sync,
the utility will move on to a different NTP server.
The pole intervals specify how often the time
sync the utility should contact NTP servers.
For example,
we can see a minimum pole interval of 32 seconds here and a maximum of 2048.
This means that it should wait at least 32 seconds to ask for new data,
but it shouldn't wait more than 2048 seconds.
After making its first request time, sync,
D will adjust the pole interval between these two minimum and maximum values.
So it can be something random like 248 seconds.
It will choose the poll interval based on the clock drift that it observes.
If the server's clock doesn't drift too much, it can pull less often.
So let's save our file
and we will restart the service to apply our new settings.
Now, if we type timedate CTL,
then add a space character and press the tab key two times.
We can see a few other commands that can help us here,
show time sync and time sync status.
So to check if the
NTP servers we provided are actually used, we can type in timedate
CTL, show
time sync
and there they are.
And we can also run timedate CTL time sync
status.
And here we can see both the current pole interval and the root distance for the
NTP server used. And this wraps up our lesson about time synchronization.
So I'll see you in the next one.