LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (http://www.linuxquestions.org/questions/slackware-14/)
-   -   NTPD client setup (http://www.linuxquestions.org/questions/slackware-14/ntpd-client-setup-4175463920/)

Captain Pinkeye 05-29-2013 05:20 AM

NTPD client setup
 
I want to set the NTP daemon up on my machine, to synchronize my time with the internets. So i properly chmodded the rc.ntpd to start on boot and put NTP servers into /etc/ntp.conf as follows:
Quote:

server 0.cz.pool.ntp.org
server 1.cz.pool.ntp.org
server 2.cz.pool.ntp.org
server 3.cz.pool.ntp.org
When i run ntpdate 0.cz.pool.ntp.org, it outputs info indicating the sync works.

My question is: it is all what it takes or am i missing something, like cron job for example?

The manpages and Slackware related info i googled up were server side oriented and overly complicated, so i'm posting a question here.

tronayne 05-29-2013 06:35 AM

Yes, that's pretty much it; you have added servers in /etc/ntp.conf (you really only need three), /etc/rc.d/rc.ntpd is executable and starts the daemon. After booting and a few minutes, if you open a terminal and
Code:

/usr/sbin/ntpq -pn
    remote          refid      st t when poll reach  delay  offset  jitter
==============================================================================
 127.127.1.0    .LOCL.          10 l  24h  64    0    0.000    0.000  0.000
*64.246.132.14  .CDMA.          1 u  266 1024  377  1352.81  29.120  22.999
+205.196.146.72  108.76.168.146  2 u  441 1024  377  1259.42  38.898  32.902
+50.23.135.154  204.9.54.119    2 u  332 1024  377  1239.88  14.177  35.513

You should see something similar to the above; the line with the asterisk is the external time server you are synchronized to.

On a new installation it will take a week or so for NTP to "walk" your system clock into synchronization (it does this with small adjustments). Long-term, the daemon just runs, your system clock stays synchronized and life is good.

You do not need any cron job or manual intervention with NTP; once going, you're good.

Now, the LOCL clock at 127.127.1.0? That's there so when or if your internet connection goes away NTP will "fall-back" to the system clock and continue running; when the internet "comes-back," it will then synchronize with one of the defined pool servers. It will also evaluate the pool servers periodically and "throw out" one that has become unusable (for whatever reason) and get another server address from the pool in its place (pool servers are volunteers and they occasionally go off line so NTP is "smart" enough to recognize that and select another server).

You also, once you've gotten your clock synchronized, do not need to use ntpdate; NTP, when it starts, will slew the clock (if it's way out of time) to the correct time then keep it there.

Hope this helps some.

Captain Pinkeye 05-29-2013 10:14 AM

Hi tronayne, thanks very much for the post, it's really very informative. But it looks i have a problem after all.

Quote:

root@1339:~# ntpq -pn
127.0.0.1: timed out, nothing received
***Request timed out
root@1339:~#
According to NTP.org General Issues:
Quote:

8.2.1.1.2. 127.0.0.1: timed out, nothing received, ***Request timed out

No response was received within the timeout interval. Either the network did drop the request or the answer, or it delayed it considerably, or the server did not respond. One reason for the latter would be a configuration line like this:

restrict default ignore
So i tried to comment out the "restrict default noquery nomodify" line in /etc/ntp.conf and restart, to no avail.

/var/log/syslog info related to last ntpd start is:
Quote:

May 29 16:28:39 1339 ntpd[1434]: ntpd 4.2.6p5@1.2349 Wed Jul 4 16:38:48 UTC 2012 (1)
May 29 16:28:39 1339 ntpd[1435]: proto: precision = 0.768 usec
May 29 16:28:39 1339 ntpd[1435]: Listen and drop on 0 v4wildcard 0.0.0.0 UDP 123
May 29 16:28:39 1339 ntpd[1435]: Listening on routing socket on fd #17 for interface updates
May 29 16:28:39 1339 ntpd[1435]: Deferring DNS for 0.cz.pool.ntp.org 1
May 29 16:28:39 1339 ntpd[1435]: Deferring DNS for 1.cz.pool.ntp.org 1
May 29 16:28:39 1339 ntpd[1435]: Deferring DNS for 2.cz.pool.ntp.org 1
May 29 16:28:39 1339 ntpd[1435]: Deferring DNS for 3.cz.pool.ntp.org 1
May 29 16:29:10 1339 ntpd[1435]: Listen normally on 1 wlan0 10.0.0.199 UDP 123
May 29 16:29:10 1339 ntpd[1435]: peers refreshed
May 29 16:29:12 1339 ntpd_intres[1445]: DNS 0.cz.pool.ntp.org -> 46.28.111.54
May 29 16:29:14 1339 ntpd_intres[1445]: DNS 1.cz.pool.ntp.org -> 81.27.192.20
May 29 16:29:16 1339 ntpd_intres[1445]: DNS 2.cz.pool.ntp.org -> 212.96.160.147
May 29 16:29:18 1339 ntpd_intres[1445]: DNS 3.cz.pool.ntp.org -> 193.85.174.5

ntpdate output is:
Quote:

:~# ntpdate 0.cz.pool.ntp.org
29 May 16:24:21 ntpdate[2328]: adjust time server 147.231.100.5 offset 0.192033 sec
therefore i originally thought it is "probably working", but now i've read that ntpdate is deprecated, so i don't know if it is relevant.


I suspect the router or firewall, or maybe provider's firewall. I tried to portforward the router, no change.

If i'm missing something obvious or something i haven't thought of, it would be nice to know. If it is iptables thing (didn't touch it since install) or something similar, i'm going to drop this, i don't really need those two secs anyway, i wasted over 3 hours troubleshooting it.

Which raises a question, did it actually work all the time i thought it worked, running other distros?

tronayne 05-29-2013 11:27 AM

Before you check or edit any of the following, stop the NTP daemon (/etc/rc.d/rc.ntpd stop, then make sure that there are no accidental NTP daemons running:
Code:

ps -ef | grep ntpd
root      2493    1  0 May26 ?        00:00:08 /usr/sbin/ntpd -g -p /var/run/ntpd.pid -l /var/log/n
root    26498 26455  0 12:12 pts/0    00:00:00 grep ntpd

You should NOT see the /usr/sbin/ntpd line; if you do, the PID is the first number on the line and
Code:

kill -9 PID (using the number).
If there are any more, do it again until the only thing that shows in "grep ntpd" line.

Your local clock should be defined like this in /etc/ntp.conf:
Code:

server  127.127.1.0    # local clock
fudge  127.127.1.0 stratum 10

Double check that yours is defined this way (it's not localhost, 127.0.0.1).

Here's my /etc/ntp.conf; other than the 0.us.pool.ntp.org entries, yours should be identical (plus or minus commented-out lines):
Code:

cat /etc/ntp.conf
# Sample /etc/ntp.conf:  Configuration file for ntpd.
#
# Undisciplined Local Clock. This is a fake driver intended for backup
# and when no outside source of synchronized time is available. The
# default stratum is usually 3, but in this case we elect to use stratum
# 0. Since the server line does not have the prefer keyword, this driver
# is never used for synchronization, unless no other other
# synchronization source is available. In case the local host is
# controlled by some external source, such as an external oscillator or
# another protocol, the prefer keyword would cause the local host to
# disregard all other synchronization sources, unless the kernel
# modifications are in use and declare an unsynchronized condition.
#
server        127.127.1.0        # local clock
fudge        127.127.1.0 stratum 10       
#server  pool.ntp.org
server  0.us.pool.ntp.org
server  1.us.pool.ntp.org
server  2.us.pool.ntp.org

#
# Drift file.
# Put this in a directory which the daemon can write to.
# No symbolic links allowed, either, since the daemon updates the file
# by creating a temporary in the same directory and then rename()'ing
# it to the file.
#
driftfile /etc/ntp/drift
#

multicastclient        224.0.1.1
broadcastdelay        0.008

#
# Keys file.  If you want to diddle your server at run time, make a
# keys file (mode 600 for sure) and define the key number to be
# used for making requests.
# PLEASE DO NOT USE THE DEFAULT VALUES HERE. Pick your own, or remote
# systems might be able to reset your clock at will.
#
#keys                /etc/ntp/keys
#trustedkey        65535
#requestkey        65535
#controlkey        65535

# Don't serve time or stats to anyone else by default (more secure)
restrict default noquery nomodify
# Trust ourselves.  :-)
restrict 127.0.0.1

That's pretty much the default /etc/ntp.conf that comes with Slackware with only the pool server entries.

It does work just fine on all my systems and should on yours.

Check your /etc/rc.d/rc.ntpd if you have edited it (don't bother if you haven't):
Code:

cat /etc/rc.d/rc.ntpd
#!/bin/sh
# Start/stop/restart ntpd.

# Start ntpd:
ntpd_start() {
  CMDLINE="/usr/sbin/ntpd -g"
  echo -n "Starting NTP daemon:  $CMDLINE"
  $CMDLINE -p /var/run/ntpd.pid
  echo
}

# Stop ntpd:
ntpd_stop() {
  echo -n "Stopping NTP daemon..."
  if [ -r /var/run/ntpd.pid ]; then
    kill -HUP $(cat /var/run/ntpd.pid)
    rm -f /var/run/ntpd.pid
  else
    killall -HUP -q ntpd
  fi
  echo
}

# Restart ntpd:
ntpd_restart() {
  ntpd_stop
  sleep 1
  ntpd_start
}

# Check if ntpd is running
ntpd_status() {
  if [ -e /var/run/ntpd.pid ]; then
    echo "ntpd is running."
  else
    echo "ntpd is stopped."
    exit 1
  fi
}

case "$1" in
'start')
  ntpd_start
  ;;
'stop')
  ntpd_stop
  ;;
'restart')
  ntpd_restart
  ;;
'status')
  ntpd_status
  ;;
*)
  echo "usage $0 start|stop|restart|status"
esac

If you can open a terminal and get
Code:

ping -c 5 0.cz.pool.ntp.org
PING 0.cz.pool.ntp.org (147.32.30.28) 56(84) bytes of data.
64 bytes from bobek.sh.cvut.cz (147.32.30.28): icmp_req=1 ttl=43 time=936 ms
64 bytes from bobek.sh.cvut.cz (147.32.30.28): icmp_req=2 ttl=43 time=1134 ms
64 bytes from bobek.sh.cvut.cz (147.32.30.28): icmp_req=3 ttl=43 time=1397 ms
64 bytes from bobek.sh.cvut.cz (147.32.30.28): icmp_req=4 ttl=43 time=952 ms
64 bytes from bobek.sh.cvut.cz (147.32.30.28): icmp_req=5 ttl=43 time=1145 ms

--- 0.cz.pool.ntp.org ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 8832ms
rtt min/avg/max/mdev = 936.746/1113.572/1397.421/166.823 ms, pipe 2

then there's no problem with DNS or port forwarding or firewall (the pool servers are open -- if you can see it, so can NTP).

You can't use ntpdate if the NTP daemon is running as you probably already know; you have to, as root, execute /etc/rc.d/rc.ntpd stop first. Your example above looks as though it worked -- it set the system clock.

I assume your locale is set correctly and that ntpdate set your system clock to the correct time for your locale?

So, if all the above is correct, start the damon:
Code:

/etc/rc.d/rc.ntpd start
Starting NTP daemon:  /usr/sbin/ntpd -g

ntpq -pn
    remote          refid      st t when poll reach  delay  offset  jitter
==============================================================================
*127.127.1.0    .LOCL.          10 l  20  64    1    0.000    0.000  0.000
 149.20.68.17    66.220.9.122    2 u  17  64    1  1934.79  299.351  0.000
 138.236.128.112 18.26.4.105      2 u  16  64    1  1683.85  108.399  0.000
 69.85.183.27    132.163.4.101    2 u  16  64    1  1357.86  31.530  0.000

It's synced to the local clock; wait about 5 minutes and execute ntpq -pn again and you should see that it's synced with a time server:
Code:

ntpq -pn
    remote          refid      st t when poll reach  delay  offset  jitter
==============================================================================
 127.127.1.0    .LOCL.          10 l  59  64  37    0.000    0.000  0.000
+149.20.68.17    216.218.254.202  2 u  52  64  37  1335.01  79.296  42.478
*138.236.128.112 18.26.4.105      2 u  51  64  37  1005.11  -112.51 111.580
+69.85.183.27    132.163.4.101    2 u  51  64  37  1357.86  31.530  23.751

It's synced (to 138.236.128.112, the one with the asterisk).

Hope this helps some.

kikinovak 05-29-2013 12:38 PM

There's an article I wrote on the subject here:

http://docs.slackware.com/howtos:network_services:ntp

Captain Pinkeye 05-29-2013 01:48 PM

Quote:

stop the NTP daemon ... You should NOT see the /usr/sbin/ntpd line...
Daemon stopped, no processes left.

This is my /etc/ntp.conf
Quote:

# Sample /etc/ntp.conf: Configuration file for ntpd.
#
# Undisciplined Local Clock. This is a fake driver intended for backup
# and when no outside source of synchronized time is available. The
# default stratum is usually 3, but in this case we elect to use stratum
# 0. Since the server line does not have the prefer keyword, this driver
# is never used for synchronization, unless no other other
# synchronization source is available. In case the local host is
# controlled by some external source, such as an external oscillator or
# another protocol, the prefer keyword would cause the local host to
# disregard all other synchronization sources, unless the kernel
# modifications are in use and declare an unsynchronized condition.
#
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10

#
# NTP server (list one or more) to synchronize with:
#server pool.ntp.org iburst
server 0.cz.pool.ntp.org
server 1.cz.pool.ntp.org
server 2.cz.pool.ntp.org

#
# Drift file. Put this in a directory which the daemon can write to.
# No symbolic links allowed, either, since the daemon updates the file
# by creating a temporary in the same directory and then rename()'ing
# it to the file.
#
driftfile /etc/ntp/drift
logfile /var/log/ntp.log

#
# Uncomment to use a multicast NTP server on the local subnet:
#multicastclient 224.0.1.1 # listen on default 224.0.1.1
# Set an optional compensation for broadcast packet delay:
#broadcastdelay 0.008

#
# Keys file. If you want to diddle your server at run time, make a
# keys file (mode 600 for sure) and define the key number to be
# used for making requests.
# PLEASE DO NOT USE THE DEFAULT VALUES HERE. Pick your own, or remote
# systems might be able to reset your clock at will.
#
#keys /etc/ntp/keys
#trustedkey 65535
#requestkey 65535
#controlkey 65535

#
# Don't serve time or stats to anyone else by default (more secure)
restrict default noquery nomodify

#
# Trust ourselves. :-)
restrict 127.0.0.1
Local clock is defined properly; apart from multicast (which i don't use) and logfile (which i added just now after reading kikinovak's howto) the files are the same.

/etc/rc.d/rc.ntpd is vanilla; i changed the CMDLINE="/usr/sbin/ntpd -g" to CMDLINE="/usr/sbin/ntpd -4 -g" to try to run it IPv4 only, but i reverted it later. I checked it word by word, it's the same as yours.

The ping command works properly:
Quote:

ping -c 5 0.cz.pool.ntp.org
PING 0.cz.pool.ntp.org (147.231.100.5) 56(84) bytes of data.
64 bytes from lx.ujf.cas.cz (147.231.100.5): icmp_req=1 ttl=53 time=25.3 ms
64 bytes from lx.ujf.cas.cz (147.231.100.5): icmp_req=2 ttl=53 time=115 ms
64 bytes from lx.ujf.cas.cz (147.231.100.5): icmp_req=3 ttl=53 time=24.5 ms
64 bytes from lx.ujf.cas.cz (147.231.100.5): icmp_req=4 ttl=53 time=90.7 ms
64 bytes from lx.ujf.cas.cz (147.231.100.5): icmp_req=5 ttl=53 time=92.7 ms

--- 0.cz.pool.ntp.org ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4005ms
rtt min/avg/max/mdev = 24.580/69.826/115.704/37.655 ms
ntpdate works, too:
Quote:

ntpdate 0.cz.pool.ntp.org
29 May 20:29:35 ntpdate[15827]: adjust time server 93.185.101.74 offset -0.002920 sec
Starting the daemon:
Quote:

root@1339:~# /etc/rc.d/rc.ntpd start
Starting NTP daemon: /usr/sbin/ntpd -g
root@1339:~# ntpq -pn
127.0.0.1: timed out, nothing received
***Request timed out
root@1339:~# ps -e | grep ntpd
16279 ? 00:00:00 ntpd

It is possible that it is problem with wicd?

Quote:

Originally Posted by kikinovak (Post 4961510)
There's an article I wrote on the subject here:

http://docs.slackware.com/howtos:network_services:ntp

Thanks very much, but unfortunately i didn't see anything that could help me :(


Well actually, if ntpdate corrects the time by itself i don't need the daemon running. But i'm really curious where is the problem.

Captain Pinkeye 05-29-2013 02:05 PM

Quote:

root@1339:~# /etc/rc.d/rc.ntpd status
ntpd is running.
root@1339:~# ntpq -pn
remote refid st t when poll reach delay offset jitter
==============================================================================
127.127.1.0 .LOCL. 10 l - 64 0 0.000 0.000 0.000
147.32.30.28 .XFAC. 16 u 1204 64 0 0.000 0.000 0.000
89.31.8.3 .XFAC. 16 u 1189 64 0 0.000 0.000 0.000
93.185.101.74 .XFAC. 16 u 1726 64 0 0.000 0.000 0.000
root@1339:~#
So actually YES, it is a problem with wicd. I configured rc.inet and it works, the very same process i started in the previous post. This is kinda sad, i like wicd, it can resume from sleep properly and has ncurses interface, making me X-independent when needed.

I think i'll mark this as SOLVED, thanks guys for help

---------------------
Is there any way to rename the thread, to better emphasize that it is a wicd issue?

tronayne 05-29-2013 03:53 PM

So, you're using wicd to connect to the internet (either wired or wi-fi)? Do you manually select one or the other after the system boots and you've logged in? And you let it run continuously?

That just may be your problem: the NTP daemon is started at boot (when no wicd-connected network is available) and, of course, it's going to time out before wicd gets started and a network gets selected (although it may sync up on its own once a network is connected).

Perhaps you could just start it manually after wicd is running and you've selected a network to connect to? NTP pretty much expects a hard-wired connection to the Internet, always available. One way around this is that, if you have an Ethernet network, simply plug the Ethernet cable in to the computer from your modem/router/whatever -- you can always use wicd to disconnect from the hard-wired network and connect to a wi-fi network (I do this with my laptop, it's usually connected via Ethernet cable to a router but, every so often, it's on "foreign ground" and only wif-fi is available and I use wicd to connect to an available network and (usually) restart ntpd to get it connected via wi-fi).

If Ethernet is available, you'll get connected automatically at boot time (and wicd will "know" that). If you want to switch to wi-fi, you just open wicd, turn off the Ethernet connection and turn on the wi-fi. Simple.

Hope this helps some.

Captain Pinkeye 05-29-2013 06:19 PM

Quote:

So, you're using wicd to connect to the internet (either wired or wi-fi)? Do you manually select one or the other after the system boots and you've logged in? And you let it run continuously?
I connect to wi-fi only. I configured wicd via wicd-curses right after first boot and never really touched it afterwards. It has its own rc.wicd which starts on every boot, i assume i'm connected before any prompt can show up. (by prompt i mean any TTY i can login into)
During my troubleshooting i restarted the NTP daemon at least 20x, with internet connection well established. Didn't work.

rc.wicd actually starts at the same time as networkmanager. This is rc.M snippet around line 200:
Quote:

# Start wicd or networkmanager:
if [ -x /etc/rc.d/rc.wicd -a -x /usr/sbin/wicd ]; then
sh /etc/rc.d/rc.wicd start
elif [ -x /etc/rc.d/rc.networkmanager ]; then
sh /etc/rc.d/rc.networkmanager start
fi
Strangely, the NTP starts at about line 120 (right after inet), but NM didn't have problems.
All inet connections were wi-fi as well.

So i can only confirm that the NTP daemon works well with rc.inet and NetworkManager but not with wicd, at least on this machine.

interndan 05-30-2013 10:52 AM

I create a file called settime in cron.daily with the following
Code:

/usr/sbin/ntpdate north-america.pool.ntp.org && /sbin/hwclock -w
make it executable (chmod +x) this checks time and updates hardware clock every day. Of course you need to change time server for your locale.

Captain Pinkeye 05-31-2013 05:12 AM

Quote:

Originally Posted by interndan (Post 4962162)
I create a file called settime in cron.daily with the following
Code:

/usr/sbin/ntpdate north-america.pool.ntp.org && /sbin/hwclock -w
make it executable (chmod +x) this checks time and updates hardware clock every day. Of course you need to change time server for your locale.

This is probably the simplest solution, thanks.

RSpendl 07-14-2013 03:05 AM

Check /etc/hosts
 
I've just realized that the ntpd daemon on my server does not accept queries on localhost whereas the identical configuration (i.e. same ntp.conf, same iptables allowing UDP on port 123) on another computer works. The "ntpd -p" command from the local console simply responded with a "localhost.localdomain: timed out, nothing received".

The only difference was in /etc/hosts file where the non-working computer had the localhost for IPv6 defined as:
::1 localhost6.localdomain6 localhost6

and the correct /etc/hosts entry (as far as ntpd is concerned) is
::1 localhost.localdomain localhost6 localhost

When I've changed the /etc/hosts entry on the server, it worked right away.


All times are GMT -5. The time now is 01:19 AM.