Difference between revisions of "Linux ntp"

From Ever changing code
Jump to navigation Jump to search
 
(13 intermediate revisions by the same user not shown)
Line 23: Line 23:
hwclock --hctosys #sync hardware clock -> system clock
hwclock --hctosys #sync hardware clock -> system clock
</source>  
</source>  
Display time details (Ubuntu 16.04)
Display time details (Ubuntu 16.04)
$ timedatectl
<source lang="bash">
      Local time: Tue 2017-08-08 09:31:14 UTC
$ timedatectl
  Universal time: Tue 2017-08-08 09:31:14 UTC
      Local time: Tue 2017-08-08 09:31:14 UTC
        RTC time: Tue 2017-08-08 09:31:14
  Universal time: Tue 2017-08-08 09:31:14 UTC
        Time zone: Etc/UTC (UTC, +0000)
        RTC time: Tue 2017-08-08 09:31:14
  Network time on: yes
      Time zone: Etc/UTC (UTC, +0000)
NTP synchronized: no
Network time on: yes
  RTC in local TZ: no
NTP synchronized: no
RTC in local TZ: no
</source>


Set timezone (Ubuntu 16.04)
sudo timedatectl set-timezone Europe/London


Force ntpd to sync time
;Force ntpd to sync time
sudo systemctl stop ntp.service
Worked for Ubuntu 18.4
sudo ntpd -gq #-gq tells the ntp daemon to correct the time regardless of the offset (g) and exit immediately (q) after setting the time; it can take 5-10 sec
<source lang="bash">
sudo systemctl start ntp.service  #you should notice in timedatectl output ''NTP synchronized: yes''
sudo systemctl stop ntp.service
sudo ntpd -gq  
# -g tells the ntp daemon to correct the time regardless of the offset  
# -q exit immediately after setting the time; it can take 5-10 sec
sudo systemctl start ntp.service  #you should notice in timedatectl output NTP synchronized: yes
</source>
 


Troubleshooting NTP server
Troubleshooting NTP server
sudo tcpdump port 123    #enable ntp packet logging
<source lang="bash">
timedatectl status     
sudo tcpdump port 123    #capture ntp packets
ntpdc -p    #offset in seconds
timedatectl status     
ntpq -p      #offset in milliseconds
ntpdc -p    #offset in seconds
ntpq -p      #offset in milliseconds
ntpq --numeric --peers  #show peers (time providers)
ntpq -c lpeer            #show peers (time providers)
ntpq -d <time_server>      #debugging mode (not sure if will update local clock)
ntpdate -d <time_server>    #debugging mode, in which ntpdate will go through all the steps, but doesn't adjust the local clock
ntpstats
ntpdate -s a_stratum_1_server_address
ntpdc -c loopinfo  #will display the combined offset in seconds, as seen at the last poll
ntpdc -c kerninfo  #will display the current remaining correction, just as ntptime does
</source>
Show difference/offset between local and remote time server
ntpdate -q <time_server>    #just query NTP server and it will display time difference/offset
 
= Set [https://en.wikipedia.org/wiki/List_of_tz_database_time_zones time zone] =
;Set timezone (Ubuntu 16.04, 20.04)
<source lang="bash">
# Check the current TZ
ls -l /etc/localtime
lrwxrwxrwx 1 root root 27 Feb 16 08:30 /etc/localtime -> /usr/share/zoneinfo/Etc/UTC
 
# List time zones
timedatectl list-timezones | grep Europe/London
Europe/London
# Set a timezone
sudo timedatectl set-timezone Europe/London


ntpq --numeric --peers  #show peers (time providers)
# Verify
ntpq -c lpeer            #show peers (time providers)
ls -l /etc/localtime
ntpq -d <time_server>       #debugging mode (not sure if will update local clock)
lrwxrwxrwx 1 root root 35 Mar 29 09:18 /etc/localtime -> ../usr/share/zoneinfo/Europe/London
ntpdate -d <time_server>    #debugging mode, in which ntpdate will go through all the steps, but doesn't adjust the local clock
ntpstats
ntpdate -s a_stratum_1_server_address


  ntpdc -c loopinfo  #will display the combined offset in seconds, as seen at the last poll
vagrant@vagrant ~ $ timedatectl
ntpdc -c kerninfo  #will display the current remaining correction, just as ntptime does
              Local time: Mon 2021-03-29 09:18:59 BST
          Universal time: Mon 2021-03-29 08:18:59 UTC
                RTC time: Mon 2021-03-29 08:18:58   
                Time zone: Europe/London (BST, +0100)
System clock synchronized: no                       
              NTP service: inactive                 
          RTC in local TZ: no
</source>


Show difference/offset between local and remote time server
 
  ntpdate -q <time_server>   #just query NTP server and it will display time difference/offset
;Set time zone on [https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/set-time.html Amazon Linux], to UTC
<source lang="bash">
cat /etc/*rel*
NAME="Amazon Linux AMI"
VERSION="2014.09"
 
$ date
Fri  4 Sep 15:32:12 BST 2020
 
# Do not change the 'UTC=true' entry to another value. This entry is for the hardware clock,
# and does not need to be adjusted when you're setting a different time zone on your instance.
sudo vi /etc/sysconfig/clock # edit if needed
ZONE="UTC"
UTC=true
 
# List available time zones
ls -l /etc/localtime
 
# Symlink (cp) 'localtime' file with content of desired TZ
sudo mv /etc/localtime /etc/localtime.original
sudo ln -s /usr/share/zoneinfo/UTC /etc/localtime
 
date # no reboot required
Fri 4 Sep 14:50:26 UTC 2020
</source>
 
= <code>chrony</code> =
Every pre-RHEL8 uses <code>ntpd</code> but recently <code>chrony</code> becomes a standard time client.
* [https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/set-time.html Setting the time for your Linux instance] AWS docs
<source lang=bash>
# RHEL8
sudo yum -y install chrony
sudo systemctl start chronyd && sudo systemctl enable chronyd
sudo vi /etc/chrony.conf # add NTP server, service restart is required to apply changes
</source>
 
 
Show time sources
<source lang=bash>
chronyc sources -v
210 Number of sources = 4
 
  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
/ .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /  '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.          |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \    |          |  zzzz = estimated error.
||                                |    |          \
MS Name/IP address        Stratum Poll Reach LastRx Last sample             
===============================================================================
^- electra.pinklemon.net        2  10  377  264  -1853us[-1861us] +/-  41ms
^- x.ns.gin.ntt.net              2  9  377  416  -1083us[-1088us] +/-  111ms
^- vps.ty-penguin.org.uk        2  10  377  1164  -1701us[-2120us] +/-  42ms
^* time.cloudflare.com          3  10  377    36  +231us[ +217us] +/- 9558us
 
chronyc tracking
Reference ID    : A29FC801 (time.cloudflare.com)
Stratum        : 4
Ref time (UTC)  : Wed Feb 28 09:43:05 2024
System time    : 0.000013391 seconds slow of NTP time
Last offset     : -0.000013688 seconds
RMS offset      : 0.001914673 seconds
Frequency      : 11.953 ppm fast
Residual freq  : -0.003 ppm
Skew            : 0.159 ppm
Root delay      : 0.018045317 seconds
Root dispersion : 0.000622072 seconds
Update interval : 1032.8 seconds
Leap status    : Normal
</source>


= References =
= References =
[https://help.ubuntu.com/lts/serverguide/NTP.html Ntp] Ubuntu wiki
[https://help.ubuntu.com/lts/serverguide/NTP.html Ntp] Ubuntu wiki

Latest revision as of 11:48, 28 February 2024

Most these commands have been used on Ubuntu systems.

Time manipulation and troubleshooting

timedatectl - In recent Ubuntu releases timedatectl replaces ntpdate. By default timedatectl syncs the time once on boot and later on uses socket activation to recheck once network connections become active.

timesyncd - In recent Ubuntu releases timesyncd replaces the client portion of ntpd. By default timesyncd regularly checks and keeps the time in sync. It also stores time updates locally, so that after reboots monotonically advances if applicable.

Update NTP managment system

sudo update-rc.d -f ntpdate remove #remove deprecated ntpdate service
sudo apt-get install ntp

Show ntp logs

cat /var/log/messages | grep ntp

Show RTC (Real Time Clock) hardware clock

hwclock --show  
hwclock -r
hwclock --show --utc
hwclock --systohc #sync system clock -> hardware clock
hwclock --hctosys #sync hardware clock -> system clock

Display time details (Ubuntu 16.04)

$ timedatectl
      Local time: Tue 2017-08-08 09:31:14 UTC
  Universal time: Tue 2017-08-08 09:31:14 UTC
        RTC time: Tue 2017-08-08 09:31:14
       Time zone: Etc/UTC (UTC, +0000)
 Network time on: yes
NTP synchronized: no
 RTC in local TZ: no


Force ntpd to sync time

Worked for Ubuntu 18.4

sudo systemctl stop ntp.service
sudo ntpd -gq 
# -g tells the ntp daemon to correct the time regardless of the offset 
# -q exit immediately after setting the time; it can take 5-10 sec
sudo systemctl start ntp.service   #you should notice in timedatectl output NTP synchronized: yes


Troubleshooting NTP server

sudo tcpdump port 123    #capture ntp packets
timedatectl status    
ntpdc -p     #offset in seconds
ntpq -p      #offset in milliseconds
ntpq --numeric --peers   #show peers (time providers)
ntpq -c lpeer            #show peers (time providers)
ntpq -d <time_server>       #debugging mode (not sure if will update local clock)
ntpdate -d <time_server>    #debugging mode, in which ntpdate will go through all the steps, but doesn't adjust the local clock
ntpstats
ntpdate -s a_stratum_1_server_address
ntpdc -c loopinfo   #will display the combined offset in seconds, as seen at the last poll
ntpdc -c kerninfo   #will display the current remaining correction, just as ntptime does

Show difference/offset between local and remote time server

ntpdate -q <time_server>    #just query NTP server and it will display time difference/offset

Set time zone

Set timezone (Ubuntu 16.04, 20.04)
# Check the current TZ
ls -l /etc/localtime
lrwxrwxrwx 1 root root 27 Feb 16 08:30 /etc/localtime -> /usr/share/zoneinfo/Etc/UTC

# List time zones
timedatectl list-timezones | grep Europe/London
Europe/London
# Set a timezone
sudo timedatectl set-timezone Europe/London

# Verify
ls -l /etc/localtime
lrwxrwxrwx 1 root root 35 Mar 29 09:18 /etc/localtime -> ../usr/share/zoneinfo/Europe/London

vagrant@vagrant ~  $ timedatectl 
               Local time: Mon 2021-03-29 09:18:59 BST
           Universal time: Mon 2021-03-29 08:18:59 UTC
                 RTC time: Mon 2021-03-29 08:18:58    
                Time zone: Europe/London (BST, +0100) 
System clock synchronized: no                         
              NTP service: inactive                   
          RTC in local TZ: no


Set time zone on Amazon Linux, to UTC
cat /etc/*rel*
NAME="Amazon Linux AMI"
VERSION="2014.09"

$ date
Fri  4 Sep 15:32:12 BST 2020

# Do not change the 'UTC=true' entry to another value. This entry is for the hardware clock, 
# and does not need to be adjusted when you're setting a different time zone on your instance.
sudo vi /etc/sysconfig/clock # edit if needed
ZONE="UTC"
UTC=true

# List available time zones
ls -l /etc/localtime

# Symlink (cp) 'localtime' file with content of desired TZ
sudo mv /etc/localtime /etc/localtime.original
sudo ln -s /usr/share/zoneinfo/UTC /etc/localtime

date # no reboot required
Fri  4 Sep 14:50:26 UTC 2020

chrony

Every pre-RHEL8 uses ntpd but recently chrony becomes a standard time client.

# RHEL8
sudo yum -y install chrony
sudo systemctl start chronyd && sudo systemctl enable chronyd
sudo vi /etc/chrony.conf # add NTP server, service restart is required to apply changes


Show time sources

chronyc sources -v
210 Number of sources = 4

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^- electra.pinklemon.net         2  10   377   264  -1853us[-1861us] +/-   41ms
^- x.ns.gin.ntt.net              2   9   377   416  -1083us[-1088us] +/-  111ms
^- vps.ty-penguin.org.uk         2  10   377  1164  -1701us[-2120us] +/-   42ms
^* time.cloudflare.com           3  10   377    36   +231us[ +217us] +/- 9558us

chronyc tracking
Reference ID    : A29FC801 (time.cloudflare.com)
Stratum         : 4
Ref time (UTC)  : Wed Feb 28 09:43:05 2024
System time     : 0.000013391 seconds slow of NTP time
Last offset     : -0.000013688 seconds
RMS offset      : 0.001914673 seconds
Frequency       : 11.953 ppm fast
Residual freq   : -0.003 ppm
Skew            : 0.159 ppm
Root delay      : 0.018045317 seconds
Root dispersion : 0.000622072 seconds
Update interval : 1032.8 seconds
Leap status     : Normal

References

Ntp Ubuntu wiki