NTP setting time with NTPDATE or NTP while NTP is running

You will most likely have NTP running on your computer syncing time to whatever servers are listed in /etc/ntp.conf. To check

ps aux | grep ntp | grep -v grep

Some history:

rdate used to set time equal to another PC in lan time
Just had script set on boot
Its important to have time equal in network. If cant get it equal to outside, have it all relatively equal at least.
ntpdate came out to set to another server (but thats only a client, it cant be used a server) – ntpdate just grabs time
Most ntp servers like ntpd set time and grab time
Just had script set on boot
ntpd came out and is better then ntpdate as it can set and grab time.

History from here: http://www.debian.org/doc/manuals/system-administrator/ch-sysadmin-time.html

If you have NTP running it will be a TIME SERVER and it will also SYNC/GRAB TIME from another server. It will grab time from the servers that are configured in /etc/ntp.conf

NOTE: NTP works on UDP port 123

It will adjust time when it gets away by alot and it will fix it on boot.

To manually set the time

First find out your server from /etc/ntpd.conf

cat /etc/ntpd.conf | grep server

# OUTPUT:
# server 1.2.3.4
# server some.thingelse.org iburst
# NOTE: 1.2.3.4 and some.thingelse.org are time servers, ignore iburst that just tells ntp about how to set the time

First stop NTP (also known as ntpd) because ntpdate cant set the time while ntp/ntpd is running as they both need to listen to port 123 to set the time. Then we will sync time with the old ntpdate (and new command ntpd -gq). Finally we will start the ntpd/ntp server again.

OLD WAY AT COMMAND LINE:

# OLD WAY
service ntp stop
ntpdate 172.18.10.249
service ntp start

NOTE: see below for a way without turning off ntp

OLD WAY AT BOOT UP FROM rc.local:

# OLD WAY
# NOTE: below is for putting in /etc/rc.local
( /etc/init.d/ntp stop
until ping -nq -c3 8.8.8.8; do
echo "Waiting for network..."
done
ntpdate -s time.nist.gov
/etc/init.d/ntp start )&

NOTE: see below for a way without turning off ntp

Ntpdate to sync time is nice, but since ntpdate is old and deprecated and now ntpd can set its own time

NEW WAY (because ntpdate is deprecated):

# NEW WAY
service ntp stop
ntpd -gq
service ntp start
# What the options mean:
# -g sets the time no matter what the skew is
# -q exits ntpd after clock is set

NOTE: see below for a way without turning off ntp

DONT FORGET AFTER TO SET HW TIME WITH NEW SYSTEM TIME:

hwclock -w

NOTE: more on hardware time setting below

NOTE: more info here  http://askubuntu.com/questions/254826/how-to-force-a-clock-update-using-ntp

NOTE: if you get an error “cant find servers” just run OLD WAY or GOLD WAY below

Checking Time difference when NTP is running

ntpd/ntp has a client program ntpq to do house keeping with. ntpq is ntpd secretary.

When NTP is running you can look at the skew of time between server and current time

host1 # ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
prod2k8core.ngl .LOCL. 1 u 33 64 1 0.548 -3.661 6.363

Here we can see my host host1, is -3.661 milliseconds off prod2k8

to see ips of servers

host1 # ntpq -pn
remote refid st t when poll reach delay offset jitter
==============================================================================
172.18.10.249 .LOCL. 1 u 48 64 37 0.699 262.525 7.592

NOTE: ntpq only works when ntpd/ntp is running, as its a client for ntpd/ntp
Notice current system time is off by 262 ms, which is less then 1 second so you wont notice much change.

Systemtime/Ostime vs Hardware time:

Your system has OS time (which is what we have been looking at above) and also HARDWARE time on your motherboard. The SYSTEM time is whateverything runs off when your working, on boot though everything works off HARDWARE time. Its best to have those insync.

To set the hardware time do this

First look at how far off it is

hwclock --show
# or
hwclock -r
# or
date && hwclock -r

NOTE: –show or -r will show the difference between OS and HW time but dont look at it as its off sometimes

# date && hwclock -r
Tue May 13 22:08:03 PDT 2014 
Tue 13 May 2014 10:07:28 PM PDT -0.880513 seconds

Notice that systemtime above is 30 seconds or so ahead of HW time, yet the difference shows as -0.8 seconds. The top line is SYS/OS time from date output, and bottom line is HW time from hwclock output

To fix the hardware time:

# hwclock -w

Check new:

# date && hwclock -r
Tue May 13 22:08:10 PDT 2014
Tue 13 May 2014 10:08:11 PM PDT -0.943081 seconds

Notice now it looks right:

NOTE: date will show current OS time, and hwclock will show HW time (the the difference will also be shown)

# To set the the HW time to be equal to OS time:
hwclock --systohc
# or
hwclock -w
# Think of -w as write, your writing the SYS time to the HW time

# To go vice verse, get the HW and set the OS/SYS time like that:
hwclock -s
# or
hwclock -s

 

Troubleshooting

Add to /etc/ntp.conf at the top

logfile /var/log/ntpd.log

/etc/default/ntp sets options/arguments for ntp on boot – its checked by /etc/init.d/ntp

More Quering commands for NTP with NTPQ and for NTPDATE with NTPDATE

NOTE: ntpq only works when ntp is running

NOTE: ntpdate doesnt have any other commands to run with it, so you set and query time with it (the onlything you cant do with it is server time) and run a smart daemon

More info, like time offsets with servers configured in /etc/ntp.conf

ntpq -pcrv
# OUTPUT:
#remote refid st t when poll reach delay offset jitter
#=============================================================================
#prod2k8core.ngl .LOCL. 1 u 42 64 17 0.550 5.848 6.129
#associd=0 status=c011 leap_alarm, sync_unspec, 1 event, freq_not_set,
#version="ntpd 4.2.6p5@1.2349-o Sat May 12 09:54:55 UTC 2012 (1)",
#processor="x86_64", system="Linux/3.14.2", leap=11, stratum=16,
#precision=-20, rootdelay=0.000, rootdisp=3.645, refid=INIT,
#reftime=00000000.00000000 Sun, Dec 31 1899 16:00:00.000,
#clock=d71d7bce.18f520f2 Tue, May 13 2014 22:21:18.097, peer=0, tc=3,
#mintc=3, offset=0.000, frequency=0.000, sys_jitter=0.000,
##clk_jitter=0.001, clk_wander=0.000

 

# More info with ntpdate
# Check time offsets:
ntpdate -q server

# Example:
ntpdate -q pool.ntp.org
# OUTPUT:
# server 72.20.40.62, stratum 2, offset 35.613188, delay 0.04973
# server 67.217.112.181, stratum 2, offset 35.613372, delay 0.11508
# server 162.210.196.6, stratum 2, offset 35.611635, delay 0.11003
# server 66.228.59.187, stratum 2, offset 35.612056, delay 0.09871
# 13 May 22:26:00 ntpdate[21648]: step time server 72.20.40.62 offset 35.613188 sec

 

NOTE: the offset value tells us how much the time is off

SET/SYNC TIME WITHOUT TURNING OFF NTP/NTPD SERVER – GOLD WAY

Set/Sync time without killing ntp/ntpd – The GOLD WAY

Only reason ntpdate doesnt work while ntp is running is because ntpdate complains about a used socket (port 123 udp). Well how about we change time without turning off ntp and use a different port then 123 udp:
-u option uses another port besides 123

# To set time with port other then 123 (it picks an unprivileged port for outgoing packets)
ntpdate -u server

Example:

# Our time is set currently to something thats off by 35 seconds:
ntpdate -q pool.ntp.org
#OUTPUT:
#server 72.20.40.62, stratum 2, offset 35.613188, delay 0.04973
#server 67.217.112.181, stratum 2, offset 35.613372, delay 0.11508
#server 162.210.196.6, stratum 2, offset 35.611635, delay 0.11003
#server 66.228.59.187, stratum 2, offset 35.612056, delay 0.09871
#13 May 22:26:00 ntpdate[21648]: step time server 72.20.40.62 offset 35.613188 sec

#Lets fix that:
ntpdate -u pool.ntp.org
#OUTPUT:
#13 May 22:26:44 ntpdate[21818]: step time server 72.20.40.62 offset 35.609697 sec

#If I fix it again, the timeoffset should be less:
ntpdate -u pool.ntp.org
#OUTPUT:
#13 May 22:26:56 ntpdate[22060]: adjust time server 72.20.40.62 offset 0.004998 sec

NOTE: can do all of the above while ntp is running

More information from other links

http://www.eecis.udel.edu/~mills/ntp/html/warp.html

https://help.ubuntu.com/10.04/serverguide/NTP.html

http://askubuntu.com/questions/429306/ntpdate-no-server-suitable-for-synchronization-found

http://raspberrypi.stackexchange.com/questions/2012/ntpd-not-actually-setting-the-time

Leave a Reply

Your email address will not be published. Required fields are marked *