NTP And System Clock Maintenance

On the Importance of NTP and Clock in the business scene:

Before you begin to explore technology, first pull one free articles children ^ _ ^
engaged in IT-related technologies boy knows, clock in important daily work, especially practitioners or longer items veteran drivers, should be able empathy! Just industry may not have much experience in this area & experience, where I divided clock & NTP importance of time-related business scenarios to help explain the next one.

Example :

Syslog servers deployed in a production environment, in order to collect various logs of network equipment, network equipment current I probably more than 500. If all my switch is no uniform time, so every day to collect millions of logs almost no effect, because the time may completely mess, no readability. 15:00 failure may occur, and some records are 2:00 in the morning, and some perhaps not at the same day or the month, so fast you can not quickly locate the point of failure from massive logs.

     类似的业务场景非常多,如果此时所有的设备都使用同一个时间标准、同一个NTP服务那么问题会迎刃而解。

Closer to home
not pull directly on dry goods

Greenwich Mean Time (GMT), it is 0:00 time zone, but we often see in the computer is UTC. UTC and GMT, although the value is equal to (the error is quite small), but has been identified as UTC is an international standard, so we should follow standard uses only UTC.

示例:

假如现在中国当地的时间是晚上8点的话,可以有下面两种表示方式

20:00 CST 【CST是Chinese Standard Time,也就是通常所说的北京时间. 】
12:00 UTC 【因为中国处在UTC+8时区,依次类推那么也就是12:00 UTC了.】

NTP Install

#yum -y install ntp

NTP And System Clock Maintenance

How to set up Linux Time Zone

  在Linux下glibc提供了事先编译好的许多timezone文件,放在/usr/share/zoneinfo这个目录下,基本涵盖了大部分的国家和城市

#zdump Hongkong --- view each time zone the current time we can use the command zdump

NTP And System Clock Maintenance

**设置系统时区的方法:**
1).修改/etc/localtime这个文件,这个文件定义了我么所在的local time zone
1.1).在/usr/share/zoneinfo下找到我们的time zone文件然后拷贝去到/etc/localtimezone(或者做个symbolic link)

示例:
#ln -sf /usr/share/zoneinfo/posix/Asia/Shanghai /etc/localtime       --把time zone换成上海所在的时区【软连接】
#date                                                                                               ---验证时间
#timedatectl                                                                                    ---显示当前时间\时区\NTP信息
#timedatectl set-timezone Asia/Hong_Kong                                   ---设置本机的时区为东八区香港时间【方案一】
#cp /usr/share/zoneinfo/Asia/Hong_Kong /etc/localtime                 ---设置本机的时区为东八区香港时间【方案二】

NTP And System Clock Maintenance
#tzselect --- manually select the region corresponding item provided three programs []
NTP And System Clock Maintenance

2).设置TZ环境变量的值. 许多程序和命令都会用到这个变量的值. TZ的值可以有多种格式,最简单的设置方法就是使用tzselect命令

Real Time Clock(RTC) and System Clock

 在PC上有两个时钟:一个称之为硬件时间时钟(RTC),一个称之为系统时钟(System Clock).
 硬件时钟:是指嵌在主板上的特殊的电路,它的存在就是平时我们关机之后还可以计算时间的原因.
 系统时钟:是操作系统的kernel所用来计算时间的时钟. 【各自独立运行】

Example:
#date - system time
NTP And System Clock Maintenance

#hwclock --show - hardware clock; equivalent to hwclock command.
NTP And System Clock Maintenance
#date z% + - zone when viewing system, different output z sensitive.
NTP And System Clock Maintenance

Tip: system log using hardware time, the system must be kept synchronized time and hardware time consistent.

#watch ntpq -p - view the health status of the NTP service, this command can only be effective in NTP SERVER run.
NTP And System Clock Maintenance

 remote           refid      st t when poll reach   delay   offset  jitter
 LOCAL(0)        .LOCL.          10 l  20d   64    0    0.000    0.000   0.000
*ntp.hkg10.hk.le 130.133.1.10     2 u   86 1024  377  285.173  -11.540  49.956
+de-user.deepini 195.13.23.5      3 u   57 1024  377  213.254   71.782   6.206
h199-182-204-19 23.252.63.82     2 u  56h 1024    0  176.455   81.056   0.000

remote: 它指的就是本地机器所连接的远程NTP服务器
refid: 它指的是给远程服务器(e.g. 193.60.199.75)提供时间同步的服务器
st: 远程服务器的层级别(stratum). 由于NTP是层型结构,有顶端的服务器,多层的Relay Server再到客户端. 所以服务器从高到低级别可以设定为1-16. 为了减缓负荷和网络堵塞,原则上应该避免直接连接到级别为1的服务器的.
t: 这个.....我也不知道啥意思^_^
when: 我个人把它理解为一个计时器用来告诉我们还有多久本地机器就需要和远程服务器进行一次时间同步
poll: 本地机和远程服务器多少时间进行一次同步(单位为秒). 在一开始运行NTP的时候这个poll值会比较小,那样和服务器同步的频率也就增加了,可以尽快调整到正确的时间范围.之后poll值会逐渐增大,同步的频率也就会相应减小
reach: 这是一个八进制值,用来测试能否和服务器连接.每成功连接一次它的值就会增加
delay: 从本地机发送同步要求到服务器的round trip time
offset: 这是个最关键的值, 它告诉了我们本地机和服务器之间的时间差别. offset越接近于0,我们就和服务器的时间越接近
jitter: 这是一个用来做统计的值. 它统计了在特定个连续的连接数里offset的分布情况. 简单地说这个数值的绝对值越小我们和服务器的时间就越精确

那么大家细心的话就会发现两个问题: 第一我们连接的是0.uk.pool.ntp.org为什么和remote server不一样? 第二那个最前面的+和*都是什么意思呢?

第一个问题,因为NTP提供给的是一个cluster server,所以每次连接的得到的服务器都有可能是不一样.因此,在指定NTP Server的时候,应该使用hostname而不是IP。
第二个问题和第一个相关,既然是集群服务.那么如何知道这些服务器的状态呢? 请参考以下释义:

        * 它告诉我们远端的服务器已经被确认为我们的主NTP Server,我们系统的时间将由这台机器所提供
        + 它将作为辅助的NTP Server和带有*号的服务器一起为我们提供同步服务. 当*号服务器不可用时它就可以接管
        - 远程服务器被clustering algorithm认为是不合格的NTP Server
        x 远程服务器不可用

Ntpd ntpdate and the difference

#rpm -qa | grep ntp                                                                   --查看服务器是否安装ntp,系统默认安装ntpdate;

NTP And System Clock Maintenance

官方技术支持解释是:
ntpd在实际同步时间时是一点点的校准过来时间的,最终把时间慢慢的校正对,而ntpdate不会考虑其他程序是否会阵痛,直接调整时间【跃变】.一个是校准时间,一个是调整时间。

提示:vmware虚拟机的时钟不太正常,比正常速度慢好多秒,所以在虚拟机上测试ntpd很难得到理想的结果,曾经纠结于此较长时间。

NTP Security Settings

只允许局域网内一部分的用户连接到服务器. 第二client不能修改服务器上的时间.

权限的设定主要以 restrict 这个参数来设定,主要的语法为: 
restrict IP地址 mask 子网掩码 参数 【其中 IP 可以是IP地址,也可以是 default ,default 就是指所有的IP 】

参数有以下几个:【注意:如果参数没有设定,那就表示该 IP (或子网)没有任何限制!】 
ignore :关闭所有的 NTP 联机服务 
nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。 
notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网 
noquery :不提供客户端的时间查询 

在/etc/ntp.conf文件中我们可以用restrict关键字来配置上面的要求
首先我们对于默认的client拒绝所有的操作
restrict default kod nomodify notrap nopeer noquery

然后允许本机地址一切的操作
restrict 127.0.0.1

最后,允许局域网内所有client连接到这台服务器同步时间.但是拒绝让他们修改服务器上的时间
restrict 192.168.1.0 mask 255.255.255.0 nomodify
把这三条加入到/etc/ntp.conf中就完成了简单配置. NTP还可以用key来做authentication.

Examples
NTP And System Clock Maintenance
#systemctl start ntpd - NTP server
#systemctl enable ntpd - set to open from the start
#systemctl stop ntpd - stop the NTP service
#systemctl status ntpd - View the NTP service status

NTP Server / client setup

只要在client的/etc/ntp.conf加上这你自己的服务器就可以了
server ntp1.leonard.com iburst
server ntp2.leonard.com iburst

NTP And System Clock Maintenance

# ntpdate X.X.X.X                                                                             --LINUX客户端向NTP服务器同步自己的时间

NTP And System Clock Maintenance

hwclock --systohc - the system time to hardware time

NTP And System Clock Maintenance

NTP clock synchronization principle and configuration

NTP And System Clock Maintenance

Guess you like

Origin blog.51cto.com/13637805/2412336