Ntpd syncing is not working

Newbie Alert !!
I am trying to achieve periodic syncing of time on my board or when the time zone changes. So i am thinking a manual call when the timezone changes should be the right way against the periodic syncing.
I am using the below command line for achieving the same.
ntpd -q -c /etc/ntp.conf
But i am not getting any result with this command. I have back dated my linux OS to a very old time. After executing the above command, the ntpd is not correcting the time.
This is my ntp.conf
# This is the most basic ntp configuration file
# The driftfile must remain in a place specific to this
# machine - it records the machine specific clock error
driftfile /var/lib/ntp/drift
# This should be a server that is close (in IP terms)
# to the machine. Add other servers as required.
# Unless you un-comment the line below ntpd will sync
# only against the local system clock.
#
server 0.pool.ntp.org
pool 0.pool.ntp.org
#
# Using local hardware clock as fallback
# Disable this when using ntpd -q -g -x as ntpdate or it will sync to itself
#server 127.127.1.0
#fudge 127.127.1.0 stratum 14
# Defining a default security setting
restrict default

Please suggest a good way to achieve my task and correct my command line call.
Thanks in advance.

Edit:
@apuls @mburnicki Thanks to you both. Now my command line and .conf file is improving. Now the command line is working for me. Placing it here so that anyone with the same question can solve it. This is what i am using now.

ntpd -q  -c /etc/ntp.conf -d -g -x

The ntp.conf file i am using now

# This is the most basic ntp configuration file
# The driftfile must remain in a place specific to this
# machine - it records the machine specific clock error
driftfile /var/lib/ntp/drift
# This should be a server that is close (in IP terms)
# to the machine.  Add other servers as required.
# Unless you un-comment the line below ntpd will sync
# only against the local system clock.
#
#server 0.pool.ntp.org
pool 0.pool.ntp.org
pool 1.pool.ntp.org
pool 2.pool.ntp.org

#
# Using local hardware clock as fallback
# Disable this when using ntpd -q -g -x as ntpdate or it will sync to itself
#server 127.127.1.0
#fudge 127.127.1.0 stratum 14
# Defining a default security setting
restrict default
disable monitor

Since i am manually calling this command line, i am testing a scenario where the network connectivity is not there or due to some reason the server was not reached. I don’t think ntpd supports a timeout functionality. Please correct me if i am wrong. Hence i am doing some thing like this

timeout -t 60 ntpd -q  -c /etc/ntp.conf -d -g -x

Again with this approach i am facing another difficulty which is getting a return status. It always gives me a “0” for error and success.

If your time is more then 1000 sec behind the current date you have to use -g

-g
Normally, ntpd exits with a message to the system log if the offset exceeds the panic threshold, which is 1000 s by default. This option allows the time to be set to any value without restriction; however, this can happen only once. If the threshold is exceeded after that, ntpd will exit with a message to the system log. This option can be used with the -q and -x options. See the tinker command for other options.

1 Like

What do you mean by “when the time zone changes”? Since you say you’re a newbie, I hope you know that NTP works with UTC only. It knows nothing about time zones, but ntpd synchronizes your UTC system time continuously and smoothly.

Concerning your configuration:

server 0.pool.ntp.org is the old way to use the pool servers, It gets a single IP address and uses that one forever.

pool 0.pool.ntp.org is a better way to use the pool. It selects a few pool servers, and if one of them becomes unreachable it looks for a new one, so that there are always a couple of good servers that can be polled.

restrict default just say that you are going to define some default restrictions, but the restrictions are missing after these keywords. I don’t know how ntpd behaves in such case, but I suggest you first comment this line out and see if it works, and once it does you can re-add some useful restrictions.

1 Like

Thanks for your comments. My words might be confusing for you. Timezone of the board will be set from a remote server. After the timezone is set, i will execute the ntpd command also. If my Linux 0S is running on a specific timezone and when the ntpd corrects or sync the UTC time from a remote server, won’t that reflect the time in Local time zone also (output of the “date” command or simply the system date and time) ?

Linux uses Timezone files to display the local time to the user, in raw format they are text files simply stating the offset from UTC, daylight savings, and other bits of info. You can change the timezone file a dozen times in succession and it’s going to have zero effect on the underlying clock keeping.

Running ntpd after changing the timezone isn’t necessary unless you are wanting to do it just to ensure your local time hasn’t drifted too much.

and you are using the wrong command to do the job,
as you are not running an ntp daemon to keep syncing,
just use ntpdate

Hi @AndCycle
I don’t have a ntpdate binary on my OS. I also understand that ntpdate is deprecated or going to be deprecated.

You may want to have a look at chapter 4.5, “Computer UTC vs. Local Time” of this PDF:

I don’t know what system you are using, but usually ntpd runs permanently in the background to keep the system’s UTC time as accurate as possible.

The timezone files or TZ environment settings provide some rules if and how some local time is to be derived from the system UTC time.

There is no reason to restart ntpd just because the timezone rules have been updated. However, conversion from UTC to local time is usually done by some functions provided by the system’s run time library, e.g. glibc on Linux, which may read the current time zone rules for the process only at startup. So you may need to restart these applications whenever the time zone rules have been updated / changed for the new rules to take effect.

ntpdate has initially been used to set the UTC system time once after boot, to have the system time fairly correct when ntpd was started thereafter.

This was also a good thing to do if you run e.g. a data base application since the system time was also correct when ntpdate has been running before.

This still works, but ntpdate didn’t use the same configuration file as ntpd. So current versions of ntpd can be used to run once and terminate as soon as the system time has been initially adjusted, and then start ntpd for continuous operation as well as applications that require exact system time, e.g. in a boot script:

# Initially adjust system time, then terminate
ntpd -gGq

# Subsequent commands are run only after the command above
# has terminated, and thus the system time is correct

ntpd   # Now run continuously in the background
start application 1
start application 2
...

I am using a Linux system on an embedded device. I was initially using the busybox-ntpd which was not updating the date and time periodically. But the update is happening only during restart. I tried restarting the process alone to correct the date and time, but the correction is happening after 4-6 mins. I was thinking this is a huge gap for me.
Now i have enabled the ntpd package and disabled the busybox-ntpd, can i restart the process and update the date and time at once ?

I was not able to open the PDF file which you have shared. Looks like the link is not working.

I hope with “periodically” you don’t mean that the system time would be stepped in periodic intervals.

Instead, ntpd compares its own UTC system time to the time from remote server(s), does a lot of filtering to determine the current time offset and also how the time offset develops (i.e. how the time drifts), and then adjusts the system time smoothly and continuously so that most applications don’t even notice the adjustments.

I’ve never tried busybox-ntpd, but I doubt it’s a full-featured implementation, so it may behave differently.

This knowledge base article provides some basics:
https://kb.meinbergglobal.com/kb/time_sync/ntp/ntp_basics

The PDF link I mentioned before contains spaces escaped with %20 in the URL, and it looks like this was corrupted by the forum software.

A valid link to the PDF “Computer Time Synchronization Concepts” can be found on this web page:
https://www.meinbergglobal.com/english/info/#whitepaper

Thanks for the new link. Still i can’t find any solution to my so called problem.

  1. Why is ntpd demon not correcting the time if i ‘back date’ my system for few hours ?

  2. I am executing ntpd -q -g -x and the time is updated in few seconds. But the problem with this command line is that it doesn’t have a timeout. So timeout -t 60 ntpd -q -g -x was my solution. But this is not giving a proper exit status. It’s always returning “0” regardless of a failure or success case.

You may want to try without the -x option. That option slows down the clock and does not set backward the clock. I am not sure it helps on the faster termination of the ntpd, but it may be worthwhile to try.

Hi @NTPman, I am back dating my system manually for testing my scenarios.

That is great. Then, you may want to let us know how the testing scenario without the -x option behaves.

Test scenario: Running ntpd when there is no internet connectivity.
Expectation: Timeout and exit the process with a return status after few seconds/minutes.

Steps:

  1. When internet connectivity is stable

    ntpd -q -g

The command executes as expected and corrects the date and time.

  1. Without internet connectivity

    ntpd -q -g

The command gets executed and goes to inifinite timeout.

@NTPman please help me understand if this was the observation you were expecting to find out.

What is the kind of configuration do you have for “Without Internet”? It may behave totally differently, if there is no default route, compared to have default route, but all packets are lost. The first case should be always fast, the second may involve multiple DNS timeouts as well depending of the servers defined in /etc/ntp.conf .

@NTPman Please help me understand how the DNS timeout of servers will be a problem here. When the internet connectivity is out, the basic local configuration is what matters is what i understood. Are you saying that we can assign DNS timeout on our client machine ? I am confused.

In the ntp.conf file there are time servers defined. Their names must be resolved to IP addresses. If you do not have Internet connectivity, the name resolution (DNS) will fail (probably with time-out).