Difference between revisions of "Network Time Protocol"

From ArchWiki
Jump to: navigation, search
m
m (redirect to generic intro to NTP)
(93 intermediate revisions by 18 users not shown)
Line 1: Line 1:
[[Category:Networking (English)]]
+
#REDIRECT [[Time#Time synchronization]]
[[Category:Daemons and system services (English)]]
 
[[Category:HOWTOs (English)]]
 
{{i18n_links_start}}
 
{{i18n_entry|English|:Network Time Protocol}}
 
{{i18n_entry|简体中文|:Network Time Protocol (简体中文)}}
 
{{i18n_links_end}}
 
This article describes two different possibilities to synchronize the Linux software clock (system clock) to Internet time servers.  '''Openntpd''' is explained, then the traditional '''ntpd''' method is explained.  For more information on computer timekeeping, see [[Time]].
 
 
 
= OpenNTPD =
 
 
 
'''OpenNTPD''' is a free, easy to use implementation of the '''N'''etwork '''T'''ime '''P'''rotocol. It provides the ability to sync the local clock to remote '''NTP''' servers and can act as NTP server itself, redistributing the local clock.  OpenNTPD is primarily developed by Henning Brauer as part of the OpenBSD Project, and is a new implementation of the NTP protocol. It is lighter and easier to configure than ntpd, but it is not as versatile.
 
 
 
Note: OpenNTPD is not currently maintained for linux (see [http://bbs.archlinux.org/viewtopic.php?id=68627 this thread]).
 
 
 
First, the OpenNTPD package must be installed (available in the Arch Linux community repository):
 
 
 
# pacman -S openntpd
 
 
 
The default configuration is actually usable if all you want is to sync the local computer. For more detailed settings, the {{Filename|/etc/ntpd.conf}} file must be edited:
 
 
 
To sync to a particular server, uncomment and edit the "server" directive. You can find the server's URL in your area at [http://www.pool.ntp.org/zone/@ www.pool.ntp.org/zone/@].
 
 
 
server ntp.example.org
 
 
 
The "servers" directive works the same as the "server" directive, however, if the DNS name resolves to multiple IP address, ALL of them will be synced to.  The default, "pool.ntp.org" is working and should be acceptable in most cases.
 
 
 
pool.ntp.org
 
 
 
Any number of "server" or "servers" directives may be used.
 
 
 
If you want the computer you run OpenNTPD on to also be a time server, simply uncomment and edit the "listen" directive.
 
 
 
For example:
 
 
 
listen on *
 
 
 
will listen on all interfaces, and
 
 
 
listen on 127.0.0.1
 
 
 
will only listen on the loopback interface.
 
 
 
However, your time server will only begin to serve time after it has synchronized itself to a high resolution. This may take hours, or days, depending on the accuracy of your system.
 
 
 
If you would like to run OpenNTPD at boot, add {{Codeline|openntpd}} the DAEMONS variable in your {{Filename|/etc/rc.conf}} following your network daemon.  If oenntpd is being used to set local system time only, it may be safely backgrounded like so:
 
@openntpd
 
 
 
To see the status of NTP syncing, visit {{Filename|/var/log/daemon.log}} and look for entries with "ntpd".
 
 
 
OpenNTPD adjusts the clock by small amounts at a time. It is designed this way to prevent sudden, large time fluctuations in your system, which could adversely affect system services (e.g., cron jobs). Thus, it can take some time to correct the time.
 
 
 
If your clock is off by more than 180 seconds you can try "{{Codeline|ntpd -s -d}}" in the console. If ntpd is already running, you can simply restart it with {{Codeline|sudo /etc/rc.d/openntpd restart}}, as the Arch openntpd package uses the "-s" flag by default. See {{Codeline|man ntpd}} for more info. You can also set the [[Time#Time_Set|system clock]] to as close to possible to the actual time and then let OpenNTPD fine tune the time.
 
 
 
== Troubleshooting ==
 
 
 
'''Error adjusting time'''
 
 
 
If you find your time set incorrectly and in log you see:
 
 
 
openntpd adjtime failed: Invalid argument
 
 
 
Try:
 
 
 
ntpd -s -d
 
 
 
'''Increasing time shift'''
 
 
 
Starting ''openntpd'' in the background could lead to synchronization errors between the actual time and the time stored on your computer. If you recognize an increasing time difference between your desktop clock and the actual time, try to start the ''openntpd'' daemon normal and not in the background.
 
 
 
'''Initialization Failure'''
 
 
 
Openntpd may fail to initialize properly if it is started before the network is fully configured. In some cases you may want to remove {{Codeline|openntpd}} from the DAEMONS array in {{Filename|/etc/rc.conf}} and add the following line to {{Filename|/etc/rc.local}}:
 
 
 
(sleep 300 && /etc/rc.d/openntpd start) &
 
 
 
This will wait 5 minutes before starting openntpd, which should give the system sufficient time to set up the network properly. If your network settings change often, you may also consider restarting the daemon regularly with cron.
 
 
 
If you are using netcfg, you can also start/stop openntpd as a POST_UP/PRE_DOWN command in your network profile:
 
 
 
POST_UP="/etc/rc.d/openntpd start || true"
 
PRE_DOWN="/etc/rc.d/openntpd stop || true"
 
 
 
Of course, you will have to specify this manually for each network profile.
 
 
 
== Using NetworkManager dispatcher ==
 
 
 
OpenNTPD can be brought up/down along with a network connection through the use of NetworkManager's dispatcher scripts. Place the following slightly modified {{Filename|/etc/rc.d/openntpd}} script as {{Filename|/etc/NetworkManager/dispatcher.d/openntpd-dispatch}}
 
 
 
<pre>
 
#!/bin/bash
 
 
 
CONF=/etc/conf.d/openntpd
 
 
 
. /etc/rc.conf
 
. /etc/rc.d/functions
 
 
 
[ -f $CONF ] && . $CONF
 
 
 
PID=`pidof -o %PPID /usr/sbin/ntpd`
 
case "$2" in
 
  up)
 
    stat_busy "Starting OpenNTPD"
 
    [ -z "$PID" ] && /usr/sbin/ntpd $PARAMS
 
    if [ $? -gt 0 ]; then
 
      stat_fail
 
    else
 
      PID=`pidof -o %PPID /usr/sbin/ntpd`
 
      echo $PID >/var/run/openntpd.pid
 
      add_daemon openntpd
 
      stat_done
 
    fi
 
    ;;
 
  down)
 
    stat_busy "Stopping OpenNTPD"
 
    [ ! -z "$PID" ]  && kill $PID &>/dev/null
 
    if [ $? -gt 0 ]; then
 
      stat_fail
 
    else
 
      rm_daemon openntpd
 
      stat_done
 
    fi
 
    ;;
 
  *)
 
    echo "usage: $0 {up|down}" 
 
esac
 
exit 0
 
</pre>
 
 
 
== Cron Job instead of Daemon ==
 
 
 
Running OpenNTPD as a daemon may not always be desirable if you have a special way you configure your network, or if you have a bad hardware clock.  You can add a cron job that will update the system clock hourly, daily..., set the hardware clock, and remove the [[Time#Time_Skew|time drift]] file.  Open your root crontab by typing "crontab -e" into terminal and (for example) to have your time set at 12:03am daily:
 
 
 
03  00  *  *  *      ntpd -s && hwclock -w && rm /var/lib/hwclock/adjtime
 
 
 
= ntpd =
 
# pacman -S ntp
 
 
 
==/etc/ntp.conf==
 
The first line in your ntp.conf file should contain the following line:
 
 
 
restrict default kod nomodify notrap nopeer noquery
 
 
 
This restricts everyone from modifying anything and prevents everyone from querying your time server. In the past "notrust" was used here too, but the function of this option has changed to mean authentication with a key is required.
 
 
 
Following this line, you need to tell ntpd what to allow through into your server.
 
 
 
restrict 127.0.0.1
 
restrict 1.2.3.4 nomodify
 
restrict 192.168.0.0 mask 255.255.255.0 nomodify
 
 
 
This tells ntpd that 1.2.3.4 and all IP addresses from the 192.168.0.0 range will be allowed to synchronize on this server, but they will not be allowed to modify anything. All other IP addresses in the world will still obey the default restrictions (the first line in the ntp.conf).
 
 
 
Now, is where the stratum 2 servers that our server will synchronize with come into play. The lines in ntp.conf will be used to tell ntpd what servers we would like to use for synchronizing (these are just examples; use ntp servers that are closest to your location). Please see http://ntp.isc.org/bin/view/Servers/NTPPoolServers for a list a closer servers. The iburst option is recommended, and sends a burst of packets if it cannot obtain a connection with the first attempt. The "burst" option should never be used without explicit permission and will likely result in blacklisting.
 
 
 
server ntp1.cs.wisc.edu iburst
 
server ntp3.cs.wisc.edu iburst
 
server ntp3.sf-bay.org iburst
 
 
 
Unless you have a good reason not to, it is advisable to use the pool.ntp.org servers: http://www.pool.ntp.org/.
 
Alternatively, a list of ntp servers is available at http://www.eecis.udel.edu/~mills/ntp/clock2a.html. Please pay attention to the Access Policies.
 
 
 
The only thing left to do is add the drift file (which keeps track of yours clocks time deviation) and the log file location:
 
driftfile /etc/ntp.drift
 
logfile /var/log/ntp.log
 
 
 
In case our server loses internet access, we need to add localhost as a server or it will stop serving time to the network. We add localhost as a "stratum 10" server so that it will never be used unless internet access is lost.
 
 
 
server  127.127.1.0
 
fudge  127.127.1.0 stratum 10
 
 
 
The complete file will look like this:
 
 
 
<pre>
 
# default restrictions
 
restrict default kod nomodify notrap nopeer noquery
 
 
 
# override the default restrictions here
 
restrict 127.0.0.1
 
#restrict 192.168.0.0 mask 255.255.255.0 nomodify ## uncomment this to serve time to a local network
 
 
 
# public NTP servers to sync with (all stratum 2)
 
server ntp1.cs.wisc.edu iburst
 
server ntp3.cs.wisc.edu iburst
 
server ntp3.sf-bay.org iburst
 
 
 
# ntp pool servers
 
server 0.pool.ntp.org iburst
 
server 1.pool.ntp.org iburst
 
server 2.pool.ntp.org iburst
 
server 3.pool.ntp.org iburst
 
 
 
## local server
 
server  127.127.1.0
 
fudge  127.127.1.0 stratum 10
 
 
 
# NTP drift file - corrects for hardware clock time deviation
 
driftfile /etc/ntp.drift
 
 
 
# NTP log file
 
logfile /var/log/ntp.log
 
</pre>
 
 
 
==/etc/rc.conf==
 
Disable HARDWARECLOCK to avoid interfering with ntpd. It is done by setting it to something else than "UTC" or "localtime":
 
HARDWARECLOCK="ntp"
 
 
 
{{Note| HARDWARECLOCK controls the setting of an hourly cron-job that adjusts the time for drift, causing ntp to go out of sync. You may want to disable the cron-job too (adjtime in cron.hourly).}}
 
 
 
Add ntpd to the DAEMONS array in rc.conf to have it start on boot:
 
DAEMONS=(syslog-ng network '''ntpd''' ...)
 
 
 
Note that before starting ntpd for the first time, you should set the time to a sane starting point (within a few minutes) manually, or using ntpdate. ntpd will not modify the time if it is too far off in order to prevent causing problems. If your clock tends to drift too far during shutdown, you should add ntpdate before ntpd in the DAEMONS array.
 
DAEMONS=(syslog-ng network '''ntpdate''' '''ntpd''' ...)
 
 
 
Do not background ntpdate, because it uses the same port as ntpd and will prevent it from starting.
 
 
 
==/etc/conf.d/ntp-client.conf==
 
This is for /etc/rc.d/ntpdate configuration. Edit NTP_CLIENT_SERVER for your ntp server.
 
<pre>
 
# change this to a server closer to your location
 
NTP_CLIENT_SERVER="pool.ntp.org"
 
 
 
# client options
 
NTP_CLIENT_OPTION="-b -u"
 
 
 
# timeout for the ntp-client
 
NTPCLIENT_TIMEOUT=10
 
 
 
# arguments passed to ntpd when started
 
NTPD_ARGS="-g"
 
</pre>
 
 
 
=External links=
 
* http://www.ntp.org/
 
* http://twiki.ntp.org/bin/view/Main/WebHome
 
* http://www.eecis.udel.edu/~mills/ntp/html/index.html
 
* http://www.openntpd.org
 

Revision as of 04:09, 6 July 2014