[ER2] Adjusting time and date settings on the Master Server

Introduction

This article explains how to set the time, configure a time zone, set up NTP (Network Time Protocol), and how to change hardware clock settings on your Master Server.

There are two sections:

  • Experienced system administrators should follow the Quick Instructions which lists the essential configuration commands that will work in most situations.
  • Those who would like to understand what they are doing in more depth should follow the Detailed Instructions which includes additional references for testing, troubleshooting, and advanced configuration.

timezones.png

 

Quick Instructions

Just cut and paste the blue commands (replace parts in green with appropriate values)

  • Set time zone

Set time zone (see Detailed Instructions if you don't know your time zone name):
[root@er-master ~]# ln -fs /usr/share/zoneinfo/America/New_York /etc/localtime
[root@er-master ~]# ln -fs /usr/share/zoneinfo/Europe/Dublin /etc/localtime
[root@er-master ~]# ln -fs /usr/share/zoneinfo/Asia/Singapore /etc/localtime

Display time zone information:
[root@er-master ~]# date +"%Z %z"

  • Set the Time

Set the Date/Time (insert correct values in localtime)
[root@er-master ~]# date -s "YYYY-MM-DD hh:mm:ss"

Check the current Date/Time
[root@er-master ~]# date

  • NTP - Quick Install / Check

Paste entire string onto command line to Install / Configure / Test NTP as appropriate
[root@er-master ~]# yum info ntp >/dev/null || yum install ntp; service ntpd status || service ntpd start; chkconfig ntpd on; chkconfig --list ntpd | grep :on >/dev/null && echo -e '\nRestart OK\n' || echo -e '\nRestart Fail\n'; ntpq -p

  • Hardware Clock (set to UTC or localtime)

If running in a VM under Windows or dual booting with Windows, use localtime
[root@er-master ~]# hwclock --systohc --localtime

All other configurations should set Hardware Clock to UTC
[root@er-master ~]# hwclock --systohc --utc

 

Detailed Instructions

  • TimeZone Configuration

The TimeZone can be set by copying the appropriate TimeZone file to /etc/localtime
[root@er-master ~]# cp /usr/share/zoneinfo/Europe/Dublin /etc/localtime

But it's more helpful to replace /etc/localtime with a symlink to the TimeZone file
[root@er-master ~]# ln -fs /usr/share/zoneinfo/Europe/Dublin /etc/localtime

 

First you need to know the correct name aka ZONE string for the TimeZone area that you want to set.

Here's a useful list https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

N.B. sometimes you will have multiple choices which are equivalent

Daylight Savings Time (DST)
It is Important to be aware that some TimeZones will automatically adjust for DST,
and some will keep a fixed offset from UTC. see GB vs GMT or US/Eastern vs EST examples below.
(also note GMT & EST are constant, CET isn't)
N.B. TimeZones don't all agree on dates  for switching to DST
IRL/UK/Europe use one set of dates, the USA uses a different set.

examples

Singapore
Asia/Singapore

both are links to SGT

Eire
Europe/Dublin
GB
GB-Eire
Europe/London

all of the above will set GMT / BST
(though the Irish ones will label BST as IST)

GMT
UTC
Zulu
Etc/Greenwich

all will set GMT / UTC all year round (i.e. no DST)

 

N.B sometimes care needed with setting to enable / disable DST

US/Eastern
America/New_York

both of the above will set EST / EDT i.e. GMT-5 Winter & GMT-4 Summer

EST
America/Jamaica
Etc/GMT+5

these will set EST aka GMT-5 all year round
Note the subtle difference in labelling in date command -05 used instead of EST when using the Etc option
also that sign is intentionally inverted i.e. GMT+5 does indeed give -05 hours rather then +05

CET
Europe/Oslo
Europe/Paris

these will set CET / CEST i.e. GMT+1 Winter & GMT+2 Summer
(maybe unexpected behaviour from CET when compared to GMT & EST which stay constant)

Etc/GMT-1
Africa/Algiers

these will force CET aka GMT+1 all year round
Note the subtle difference in labelling in date command +01 used instead of CET when using the Etc option
also that sign is intentionally inverted i.e. GMT-1 does indeed give +01 hours rather then -01

 

Set the ZONE string
[root@er-master /]# TZ=/usr/share/zoneinfo/Europe/Dublin

Set TimeZone (check the TimeZone file actually exists first!)
[root@er-master /]# [ -f $TZ ] && ln -fs $TZ /etc/localtime || echo "timezone $TZ not found"

Check TimeZone (show the link & relevant output from date command)
[root@er-master /]# ls -l /etc/localtime | cut -f2- -d/; date +"%Z %z"

Sample output

etc/localtime -> /usr/share/zoneinfo/Singapore
SGT +0800

etc/localtime -> /usr/share/zoneinfo/Eire
GMT +0000

Show DST changes (this year and next)
[root@er-master /]# ls -l /etc/localtime | cut -f7- -d/; zdump -v /etc/localtime | grep -A4 $(date +%Y) | sed '$!N;s/\n/ /' | awk '{print $9,$10,$11,$12,$13,$14" -> "$25,$26,$27,$28,$29,$30}' 

US/Eastern
Sun Mar 12 01:59:59 2017 EST -> Sun Mar 12 03:00:00 2017 EDT
Sun Nov 5 01:59:59 2017 EDT -> Sun Nov 5 01:00:00 2017 EST
Sun Mar 11 01:59:59 2018 EST -> Sun Mar 11 03:00:00 2018 EDT
Sun Nov 4 01:59:59 2018 EDT -> Sun Nov 4 01:00:00 2018 EST

Europe/Paris
Sun Mar 26 01:59:59 2017 CET -> Sun Mar 26 03:00:00 2017 CEST
Sun Oct 29 02:59:59 2017 CEST -> Sun Oct 29 02:00:00 2017 CET
Sun Mar 25 01:59:59 2018 CET -> Sun Mar 25 03:00:00 2018 CEST
Sun Oct 28 02:59:59 2018 CEST -> Sun Oct 28 02:00:00 2018 CET

If TimeZone wasn't found, then browse Zones under /usr/share/zoneinfo on the ER2 Master Server

To see the full list
[root@er-master /]# find /usr/share/zoneinfo -type f | cut -f5- -d/ | grep -v "^right\|^posix\|\.tab$" | sort

Probably easier to first pick a Geo
[root@er-master /]# echo $(ls -d /usr/share/zoneinfo/*/ | grep -v "right\|posix\|Etc" | cut -f5 -d/)

Africa America Antarctica Arctic Asia Atlantic Australia Brazil Canada Chile Europe Indian Mexico Pacific US

Set GEO
[root@er-master /]# GEO=Europe

Then look at regions within that Geo
[root@er-master /]# ls /usr/share/zoneinfo/$GEO

Amsterdam Berlin Chisinau Isle_of_Man Lisbon Mariehamn Paris San_Marino Stockholm Vaduz Zagreb
Andorra Bratislava Copenhagen Istanbul Ljubljana Minsk Podgorica Sarajevo Tallinn Vatican Zaporozhye
Astrakhan Brussels Dublin Jersey London Monaco Prague Saratov Tirane Vienna Zurich
Athens Bucharest Gibraltar Kaliningrad Luxembourg Moscow Riga Simferopol Tiraspol Vilnius
Belfast Budapest Guernsey Kiev Madrid Nicosia Rome Skopje Ulyanovsk Volgograd
Belgrade Busingen Helsinki Kirov Malta Oslo Samara Sofia Uzhgorod Warsaw

Set AREA
[root@er-master /]# AREA=Dublin

Set the TimeZone string
[root@er-master /]# TZ=/usr/share/zoneinfo/$GEO/$AREA
(now go back and set the TimeZone as per instructions above)

 

TimeZone data is updated periodically to reflect changes made by political bodies to time zone boundaries, UTC offsets, and daylight-saving rules.

If you need to have the latest timezone info
[root@er-master /]# yum update tzdata

 

  • Set the Time

The System Clock works on unix epoch time, which it converts to UTC, then applies the timezone offset to display localtime.

Basically this means that when setting the time use localtime rather than UTC
(but it's good to know that internally it uses epoch time so that it can gracefully handle TimeZone & DST changes)

Examples (showing epoch & UTC time)

Unix epoch time (number of seconds since 1970-01-01 00:00:00 in UTC)
[root@er-master ~]# date +%s

1490369584

Converted to UTC readable date
[root@er-master ~]# date -u

Fri Mar 24 15:33:04 UTC 2017

With timezone applied to display localtime (i.e. the default output)
[root@er-master ~]# date

Fri Mar 24 23:33:04 SGT 2017

To set the system clock (in localtime)
[root@er-master ~]# date -s 'YYYY-MM-DD hh:mm:ss'


History Lesson
Unix Time is represented by a 32 bit whole number (an integer) that can be positive or negative (signed). Unix was originally developed in the 60s and 70s so the "start" of Unix Time was set to January 1st 1970 at midnight GMT (Greenwich Mean Time) - this date/time was assigned the Unix Time value of 0. This is what is know as the Unix Epoch.

A 32 bit signed integer can represent whole numbers between -2147483648 and 2147483647. Since Unix Time starts at 0, negative Unix Time values go back in time from the Epoch and positive numbers go forward in time. This means that Unix Time spans from Unix Time value of -2147483648 or 20:45:52 GMT on December 13th 1901 to Unix Time value of 2147483647 or 3:14:07 GMT on January 19 in 2038. These dates represent the beginning, the pre-history and the end of Unix Time.

The end of Unix Time will occur on January 19, 2038 03:14:07 GMT. On January 19, 2038 03:14:08 GMT all computers that still use 32 bit Unix Time will overflow. This is known as the "Year 2038 problem". Some believe this will be a more significant problem than the "Year 2000 problem". The fix for the Year 2038 problem is to store Unix Time in a 64 bit integer. This is already underway in most 64 bit Operating Systems but many systems may not be updated by 2038.

 

  • NTP - Network Time Protocol 

Any time variation between the ER2 Master Server and the Targets running the node agents can cause problems. Scans may not start or pause at the expected times. Enterprise Recon will show warnings when it discovers variations in clock times between the Master Server and Targets.

The best way to resolve this type of issue is to use some mechanism to sync the clock on both Master and Targets to a common time source. NTP allows us to do that. NTP stands for Network Time Protocol, and it is an Internet protocol used to synchronize the clocks of computers to a common timebase (usually UTC)..

N.B. this will still allow individual computers to exist in different TimeZones with different DST settings etc...

NTP Overview http://www.ntp.org/ntpfaq/NTP-s-def.htm

NTP should be already installed in ER 2.0.18 and above

Command to check and install if necessary
[root@er-master ~]# yum info ntp || yum install ntp

Startup NTP if necessary
[root@er-master ~]# service ntpd status || service ntpd start

Ensure NTP is set to restart on reboot
[root@er-master ~]# chkconfig ntpd on && chkconfig --list ntpd

ntpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off

The above output is good


Test if NTP is working
[root@er-master ~]# ntpq -p

remote refid st t when poll reach delay offset jitter
==============================================================================
*ns0.fredprod.co 193.190.230.65 2 u 33 64 17 12.117 -2.139 2.771
bray.walcz.net 140.203.204.77 2 u 28 64 17 11.960 0.588 10.666
tshirt.heanet.i .PPS. 1 u 25 64 17 15.360 0.647 2.453
chris.magnet.ie 193.120.10.3 2 u 25 64 17 11.922 -5.314 1.008

Optional

To check which NTP servers are being used
[root@er-master ~]# grep "^[[:space:]]*server[[:space:]]" /etc/ntp.conf

server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst

Edit the config file to insert a different NTP pool if required. (In practice this setting only needs to be changed if your organisation uses a specific NTP pool within its internet.)

[root@er-master ~]# vi /etc/ntp.conf

#UK
server 0.uk.pool.ntp.org
server 1.uk.pool.ntp.org
server 2.uk.pool.ntp.org
server 3.uk.pool.ntp.org

#IRL
server 2.ie.pool.ntp.org
server 3.europe.pool.ntp.org
server 0.europe.pool.ntp.org

#US
server 0.north-america.pool.ntp.org
server 1.north-america.pool.ntp.org
server 2.north-america.pool.ntp.org
server 3.north-america.pool.ntp.org

Restart ntp daemon to activate changes to /etc/ntp.conf
[root@er-master ~]# service ntpd restart


If the server time is way off it may take a while to sync

You can fix this by manually setting the date
[root@er-master ~]# date -s "YYYY-MM-DD hh:mm:ss"

or by using the ntpdate command to set it to the time from one of the POOL servers

Choose the NTP Pool as used by the first server in our config
[root@er-master ~]# POOL=$(grep "^[[:space:]]*server[[:space:]]" /etc/ntp.conf | head -1 | cut -f2- -d.); echo "POOL=$POOL"

Stop NTP while resetting date/time using time from NTP POOL
[root@er-master ~]# service ntpd stop; ntpdate $POOL; service ntpd start; date

 

  • System Clock vs Hardware Clock

A Linux system actually has two clocks, a System Clock and a Hardware Clock

N.B. Everything time related above (i.e. all the Timezone & NTP stuff) refers to The System Clock and not the Hardware Clock.


The Hardware Clock is the battery powered "Real Time Clock" (also known as the "RTC" or "CMOS clock") which keeps track of time when the system is turned off but is not used when the system is running.

The System Clock (sometimes called the "kernel clock" or "software clock") is a software counter based on the timer interrupt. It does not exist when the system is not running, so it has to be initialized from the Hardware Clock (or some other time source) at boot time.

The System Time is the time that matters. The Hardware Clock’s basic purpose in a Linux system is to keep time when Linux is not running. You initialize the System Time to the time from the Hardware Clock when Linux starts up, and then never use the Hardware Clock again.

The System Clock works on unix epoch time, which it converts to UTC, then applies the timezone offset to display localtime

Traditionally the Hardware Clock time is set/adjusted in the BIOS settings.

In Linux we can use the hwclock command to set the Hardware Clock directly. We can also use it to sync either the Hardware Clock to the System Clock or vice-versa.

NTP automatically keeps the Hardware Clock in sync with the System Clock.

The Hardware Clock can be set to UTC or localtime

N.B. To avoid problems with TimeZone and DST switching, it is recommended to keep the hardware clock in UTC.

Except in the case case of a multi boot with a Windows system, which assumes the hardware clock runs in local time by default,
and all other systems need to accommodate to it by using local time as well.

It may also be needed with a virtual machine; if the virtual hardware clock provided by the host is running in local time,
the guest system needs to be configured to use local time, too.


Read time from Hardware Clock and show if running on UTC or localtime
[root@er-master ~]# hwclock --debug | grep Hardware

Hardware clock is on UTC time
Time read from Hardware Clock: 2017/03/24 15:35:55

Sync and force Hardware Clock setting to local time
[root@er-master ~]# hwclock --systohc --localtime


Sync and force Hardware Clock setting to UTC
[root@er-master ~]# hwclock --systohc --utc


Additional info

Sync the Hardware Clock to the System Clock - N.B. if NTP is running this happens automatically
[root@er-master ~]# hwclock --systohc

Sync the System Clock to the Hardware Clock - N.B. this will break any NTP or linux kernel attempts to keep Hardware Clock in sync
[root@er-master ~]# hwclock --hctosys

Show the time on the Hardware clock - N.B. always displays in localtime regardless of whether clock is running in UTC or localtime
[root@er-master ~]# hwclock --show

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.