您的位置:首页 > 运维架构 > Linux

[NTP] Linux 系统时区设置,NTP时间服务器部署及ntp.conf配置参数详解

2018-10-17 15:42 2631 查看

本篇blog主要介绍了[NTP] Linux 系统时区设置,NTP时间服务器部署及ntp.conf配置参数详解,并通过案例介绍了系统时区配置及时间同步的多种方法,不同Client客户端的相关配置,最后补充了ntp及ntpdate的相关联系及区别。


Linux系统时区设置

一、什么是NTP

NTP是用来使计算机时间同步化的一种协议,全称是Network Time Protocol。它可以在大规模的设备范围内同步矫正时间到几ms级别的精度,在网络稳定的局域网内,精度甚至可以达到微秒级别。 今天NTP协议已经发展到V4版本,V1版本在1988年6月推出,主要RFC为RFC-1059;今天我们大规模使用的V4版本的NTP,还没有正式的RFC出版,但是简单的协议内容,在RFC2030里已有描述。

二、如何查看系统时区

1、使用date命令查看系统时区

[root@vm ~]# date   
Tue Oct 16 16:37:24 CST 2018     # CST代表了中国标准时间(+8区)

[root@vm ~]# date -R
Tue, 16 Oct 2018 16:37:27 +0800    # 内容详细的显示了+0800时区,即东八区中国标准时间

2、查看clock系统时区

[root@vm ~]# more /etc/sysconfig/clock     # 查看/etc/localtime 定义的系统时区
# The time zone of the system is defined by the contents of /etc/localtime.

ZONE="Asia/Shanghai"

3、通过zdump命令查看相应时区时间

[root@vm ~]# zdump Africa/Bissau
Africa/Bissau  Tue Oct 16 09:23:59 2018 GMT

[root@vm ~]# zdump Asia/Taiwan
Asia/Taiwan  Tue Oct 16 09:25:46 2018 Asia

三、如何设置系统时区

1、第一种方法:使用tzselect命令设置系统时区

使用tzselect命令获取TZ值

[root@vm ~]# tzselect    # 使用tzselect命令,获取TZ值
Please identify a location so that time zone rules can be set correctly.
Please select a continent or ocean.
 1) Africa
 2) Americas
 3) Antarctica
 4) Arctic Ocean
 5) Asia
 6) Atlantic Ocean
 7) Australia
 8) Europe
 9) Indian Ocean
10) Pacific Ocean
11) none - I want to specify the time zone using the Posix TZ format.
#? 5    # 选择Asia
Please select a country.
 1) Afghanistan		  18) Israel		    35) Palestine
 
5b4
;2) Armenia		  19) Japan		    36) Philippines
 3) Azerbaijan		  20) Jordan		    37) Qatar
 4) Bahrain		  21) Kazakhstan	    38) Russia
 5) Bangladesh		  22) Korea (North)	    39) Saudi Arabia
 6) Bhutan		  23) Korea (South)	    40) Singapore
 7) Brunei		  24) Kuwait		    41) Sri Lanka
 8) Cambodia		  25) Kyrgyzstan	    42) Syria
 9) China		  26) Laos		    43) Taiwan
10) Cyprus		  27) Lebanon		    44) Tajikistan
11) East Timor		  28) Macau		    45) Thailand
12) Georgia		  29) Malaysia		    46) Turkmenistan
13) Hong Kong		  30) Mongolia		    47) United Arab Emirates
14) India		  31) Myanmar (Burma)	    48) Uzbekistan
15) Indonesia		  32) Nepal		    49) Vietnam
16) Iran		  33) Oman		    50) Yemen
17
5b28
) Iraq		  34) Pakistan
#? 9    # 选择China
Please select one of the following time zone regions.
1) Beijing Time
2) Xinjiang Time
#? 1    # 选择Beijing Time

The following information has been given:

China
Beijing Time

Therefore TZ='Asia/Shanghai' will be used.
Local time is now:	Tue Oct 16 16:46:58 CST 2018.
Universal Time is now:	Tue Oct 16 08:46:58 UTC 2018.
Is the above information OK?
1) Yes
2) No
#? 1    # 确认TZ值TZ='Asia/Shanghai'

You can make this change permanent for yourself by appending the line
TZ='Asia/Shanghai'; export TZ
to the file '.profile' in your home directory; then log out and log in again.

Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Asia/Shanghai

在全局配置中添加TZ变量

tzselect命令只告诉你选择的时区的写法,并不会自动生效。需在用户变量/.profile、/~bash_profile或者全局变量/etc/profile中设置并导入获取的TZ变量值。 

[root@vm ~]# vi /etc/profile    # 添加TZ变量环境变量 ,把获取的TZ值加入到全局变量中
TZ='Asia/Shanghai'; export TZ
"/etc/profile" 79L, 1871C written

[root@vm ~]# source /etc/profile    # 使能TZ变量生效

[root@vm ~]# date -R    # 验证时区
Tue, 16 Oct 2018 17:08:05 +0800

[root@vm ~]# more /etc/sysconfig/clock 
# The time zone of the system is defined by the contents of /etc/localtime.
# This file is only for evaluation by system-config-date, do not rely on its
# contents elsewhere.
ZONE="Asia/Shanghai"

2、第二种方法:复制相应的时区文件,替换系统默认时区文件或者创建链接文件,设置系统时区

复制时区文件,替换系统时区默认文件

[root@vm ~]# ls /usr/share/zoneinfo/    # 在/usr/share/zoneinfo/ 文件下有很多时区文件
Africa      Asia       Canada   Cuba   EST      GB       GMT-0      HST      iso3166.tab  Kwajalein    Mexico   NZ       Portugal    PST8PDT  Singapore  Universal  W-SU
America     Atlantic   CET      EET    EST5EDT  GB-Eire  GMT+0      Iceland  Israel       leapseconds  MST      NZ-CHAT  posix       right    Turkey     US         zone1970.tab
Antarctica  Australia  Chile    Egypt  Etc      GMT      Greenwich  Indian   Jamaica      Libya        MST7MDT  Pacific  posixrules  ROC      tzdata.zi  UTC        zone.tab
Arctic      Brazil     CST6CDT  Eire   Europe   GMT0     Hongkong   Iran     Japan        MET          Navajo   Poland   PRC         ROK      UCT        WET        Zulu

[root@vm ~]# rm -rf /etc/localtime     # 删除系统默认时区文件

[root@vm ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime    # 复制时区文件到默认路径 或者

或者通过创建链接文件,/usr/share/zoneinfo/Asia/ShangHai文件链接到/etc/localtime文件

[root@vm ~]# rm -rf /etc/localtime 

[root@vm ~]# ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 
`/etc/localtime' -> `/usr/share/zoneinfo/Asia/Shanghai'

通过实验,在设置系统时区过程中,TZ变量优先级高于本地/etc/localtime文件优先级。也就是说,若TZ变量及/etc/localtime 都存在的情况下 ,TZ变量会优先生效 。若想要设置timezone(系统时区),故建议直接在全局变量配置文件中添加TZ变量即可,即使用第一种配置方式设置linux系统时区。


Linux NTP服务器部署安装及配置

一、linux NTP服务器部署安装

1、验证服务器端NTP服务是否已安装

[root@vm ~]# rpm -qa ntp*    
ntpdate-4.2.6p5-12.el6.centos.2.x86_64
ntp-4.2.6p5-12.el6.centos.2.x86_64

2、若系统为安装ntp服务 ,可通过rpm或yum进行安装

[root@vm ~]# yum install ntp -y

[root@vm ~]# rpm -ql ntp   
/etc/ntp.conf    # ntp服务器的主配置文件
/etc/rc.d/init.d/ntpd    # 开机启动ntpd脚本文件

3、国内稳定NTP时间同步服务器域名

times.aliyun.com

time1.aliyun.com

cn.pool.ntp.org

0.cn.pool.ntp.org

1.cn.pool.ntp.org

4、使用ntpdate同步时间服务器

[root@vm ~]# ntpdate times.aliyun.com
17 Oct 10:05:27 ntpdate[26878]: step time server 120.25.115.19 offset 4.115593 sec

[root@vm ~]# ntpdate cn.pool.ntp.org
17 Oct 10:05:47 ntpdate[26908]: adjust time server 85.199.214.100 offset 0.037943 sec

[root@vm ~]# ntpdate 0.cn.pool.ntp.org
17 Oct 10:06:00 ntpdate[26915]: adjust time server 120.25.115.20 offset -0.003371 sec

二、linux NTP服务配置

1、NTP服务 ntp.conf 参数详解

默认ntp.conf内容如下

[root@vm ~]# vi /etc/ntp.conf

driftfile /var/lib/ntp/drift
#系统时间与BIOS事件的偏差记录

restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
# 限制所有的client连接此NTP服务器进行同步、查询等相关操作

restrict 127.0.0.1
restrict -6 ::1
# 本机localhost对NTP服务拥有全部的权限

# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
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

#broadcast 192.168.1.255 autokey        # broadcast server
#broadcastclient                        # broadcast client
#broadcast 224.0.1.1 autokey            # multicast server
#multicastclient 224.0.1.1              # multicast client
#manycastserver 239.255.254.254         # manycast server
#manycastclient 239.255.254.254 autokey # manycast client

# Enable public key cryptography.
#crypto

includefile /etc/ntp/crypto/pw

# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography. 
keys /etc/ntp/keys

各项参数详解

restrict 控制相关权限。

语法为: restrict IP地址 mask 子网掩码 参数

其中IP地址可是单个主机、网段或者是default ,default 就是指所有的IP

参数有以下几个:
ignore  :关闭所有的 NTP 联机服务
kod : 访问违规时发送 KoD 包。
nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网
noquery :不提供客户端的时间查询:用户端不能使用ntpq,ntpc等命令来查询ntp服务器
notrap :不提供trap远端登陆:拒绝为匹配的主机提供模式 6 控制消息陷阱服务。陷阱服务是 ntpdq 控制消息协议的子系统,用于远程事件日志记录程序。
nopeer :用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟
restrict -6 表示IPV6地址的权限设置。

fudge 127.127.1.0 stratum 0   # 这行是时间服务器的层次。设为0则为顶级,如果要向别的NTP服务器更新时间,请不要把它设为0

restrict default ignore    # 设置默认策略为允许任何主机进行时间同步

restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap    # 允许192.168.1.1-192.168.1.255主机与NTP服务器进行时间同步,但不允许更改服务器端参数;同时NTP服务器拒绝为匹配的主机提供trap远程登录及日志记录。

logfile /var/log/ntpstats/ntpd.log    # 定义ntp日志目录

2、配置NTPserver

调整NTP配置文件

[root@vm ~]# vi /etc/ntp.conf
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

driftfile /var/lib/ntp/drift    #系统时间与BIOS事件的偏差记录

# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
# 限制所有的client连接此NTP服务器进行同步、查询等相关操作

# Permit all access over the loopback interface.  This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1    # 本机localhost对NTP服务拥有全部的权限
restrict -6 ::1

# Hosts on local network are less restricted.
restrict 192.168.23.0 mask 255.255.255.0 nomodify  # 允许局域网内client连接到这台服务器同步时间.但是拒绝让他们修改服务器上的时间
restrict 192.168.10.0 mask 255.255.255.0 nomodify
restrict 192.168.20.0 mask 255.255.255.0 nomodify
restrict 172.16.10.0 mask 255.255.255.0 nomodify 
restrict 192.168.100.0 mask 255.255.255.0 nomodify

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server times.aliyun.com prefer    # 表示本机NTP优先进行时间同步的服务器
server time1.aliyun.com iburst
server cn.pool.ntp.org iburst
server 0.cn.pool.ntp.org iburst
server 1.cn.pool.ntp.org iburst

#broadcast 192.168.1.255 autokey        # broadcast server
#broadcastclient                        # broadcast client
#broadcast 224.0.1.1 autokey            # multicast server
#multicastclient 224.0.1.1              # multicast client
#manycastserver 239.255.254.254         # manycast server
#manycastclient 239.255.254.254 autokey # manycast client

# Enable public key cryptography.
#crypto
includefile /etc/ntp/crypto/pw

fudge 127.127.1.0 stratum 3    # 设置本机NTP等级为3 ,允许其他客户端同步该ntp服务器,也能实现该ntp服务器与公网ntp服务器同步
logfile /var/log/ntpstats/ntpd.log    # 定义ntp日志目录

# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography. 
keys /etc/ntp/keys

ntp服务启动

[root@vm ~]# chkconfig ntpd on

[root@vm ~]# service ntpd start
Starting ntpd:                                             [  OK  ]

[root@vm ~]# ss -tunlp | grep ntp
udp    UNCONN     0      0         192.168.23.100:123                   *:*      users:(("ntpd",24250,19))
udp    UNCONN     0      0              127.0.0.1:123                   *:*      users:(("ntpd",24250,18))
udp    UNCONN     0      0                      *:123                   *:*      users:(("ntpd",24250,16))
udp    UNCONN     0      0      fe80::92b1:1cff:fe10:3a44:123                  :::*      users:(("ntpd",24250,21))
udp    UNCONN     0      0                    ::1:123                  :::*      users:(("ntpd",24250,20))
udp    UNCONN     0      0                     :::123                  :::*      users:(("ntpd",24250,17))

3、配置/etc/sysconfig/ntpd文件

ntp服务,默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd文件,在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes 这样,就可以让硬件时间与系统时间一起同步。

[root@vm ~]# vi /etc/sysconfig/ntpd      # 实现硬件时间与系统时间同步  
# Drop root to id 'ntp:ntp' by default.
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"
SYNC_HWCLOCK=yes

4、查看ntp服务器同步状态

[root@vm ~]# ntpstat     # 确认本地NTP与上层NTP服务器是否联通
synchronised to NTP server (120.25.115.19) at stratum 3     #本NTP服务器层次为3,已向120.25.115.19 NTP同步过
   time correct to within 33 ms    # 时间校正到相差33ms之内
   polling server every 256 s    #&
5b4
nbsp;每256秒会向上级NTP轮询更新一次时间

[root@vm ~]# watch ntpq -p    # 查看本地NTP需进行同步的公网NTP服务器状态
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*120.25.115.19   10.137.53.7      2 u  131  128  377   36.747   -7.176   1.091
+203.107.6.88    100.107.25.114   2 u  106  128  377   14.224   -6.557   1.017
+119.28.206.193  100.122.36.196   2 u  127  128  377   49.294   -5.359   1.101
-amy.chl.la      213.251.128.249  2 u    6  128  235  378.766  -24.676  54.061
x5.103.139.163   .GPS.            1 u  100  128  257&nb
6688
sp; 265.566  -44.008  11.612
+120.25.115.20   10.137.53.7      2 u   45  128  377   38.392   -6.198   4.650

参数详解

remote:进行时间同步的上层NTP的ip或主机名
    “*” 表示时间同步的主NTP Server;
    “+”表示时间同步的辅助NTP Server,和带有“*”号的服务器一起为本地NTP提供同步服务,当*号服务器不可用时它就可以接管;
    “-”表示同步的NTP服务器被clustering algorithm认为是不合格的NTP Server;
    “x”表示同步的外网NTP服务器不可用
refid:为外网NTP服务器提供时间同步服务的上一层ntp主机地址
st :stratum阶层
when :多少秒前曾经同步过时间
poll:本地机和远程服务器多少时间进行一次同步(单位为秒)
reach: 已经向上层ntp服务器要求更新的次数
delay: 网络延迟
offset: 时间补偿
jitter:系统时间与bios时间差


NTP client 与NTP服务器时间同步配置

一、linux 客户端与ntp服务器时间同步部署

可通过两种方法实现,第一种配置周期性任务计划时间同步,第二种方法配置ntp服务 

1、配置周期性时间同步任务

[root@zabbix ~]# yum install crontabs -y

[root@zabbix ~]# chkconfig crond on

[root@zabbix ~]# service crond start    # 启动crond服务

[root@zabbix ~]# vi /etc/crontab    # 配置周期性时间同步任务
# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
  0  1  *  *  * root /usr/sbin/ntpdate 192.168.23.100 &> /dev/null ;/sbin/hwclock -w &> /dev/null  
  # 表示每天的1:00执行/usr/sbin/ntpdate 192.168.23.100 NTP服务器,同时同步硬件时钟,但不显示任何信息

2、linux客户端配置ntp服务

[root@zabbix ~]# yum install ntp ntpdate -y

[root@zabbix ~]# more /etc/sysconfig/clock 
ZONE="Asia/Shanghai"

[root@zabbix ~]# date -R
Wed, 17 Oct 2018 13:53:34 +0800

[root@zabbix ~]# ntpdate 192.168.23.100
17 Oct 13:55:08 ntpdate[11772]: adjust time server 192.168.23.100 offset 0.463749 sec

[root@zabbix ~]# vi /etc/ntp.conf # 调整同步的时间服务器,其他配置默认即可
server 192.168.23.100  prefer
server time1.aliyun.com iburst

[root@zabbix ~]# vi /etc/sysconfig/ntpd      # 实现硬件时间与系统时间同步  
# Drop root to id 'ntp:ntp' by default.
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"
SYNC_HWCLOCK=yes

[root@zabbix ~]# service ntpd start
Starting ntpd:                                             [  OK  ]

[root@zabbix ~]# ntpq -p    # 查看时间同步状态
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 bogon           120.25.115.19    3 u    3   64    1    0.651   35.932   0.000
 203.107.6.88    10.165.84.13     2 u    -   64    1   17.134   31.774   1.699
 
[root@zabbix ~]# ntpstat 
synchronised to NTP server (203.107.6.88) at stratum 3 #本NTP服务器层次为3,已向203.107.6.88 NTP同步过
   time correct to within 981 ms    # 时间校正到相差981ms之内
   polling server every 64 s     # 每64秒会向上级NTP轮询更新一次时间

二、Windows Server客户端与ntp服务器时间同步部署

1、进入日期与时间,选择更改设置

    

2、输入ntp服务器地址,并点击“立即更新”,确定即可。

    

三、华为网络设备客户端与ntp服务器时间同步部署

<HX>sys
Enter system view, return user view with Ctrl+Z.

[HX]ntp-service server disable     

[HX]ntp-service ipv6 server disable 

[BICI-HX]clock timezone ShangHai add 08:00:00    # 定义时区+8区

[HX]ntp-service unicast-server 192.168.23.100    # 指向同步ntp的服务器地址

[HX]dis ntp-service status     # 查看本地ntp状态,“synchronized”表示同步完成状态
 clock status: synchronized 
 clock stratum: 4 
 reference clock ID: 192.168.23.100
 nominal frequency: 100.0000 Hz 
 actual frequency: 100.0000 Hz 
 clock precision: 2^17
 clock offset: 77.7719 ms 
 root delay: 42.63 ms 
 root dispersion: 81.05 ms 
 peer dispersion: 50.72 ms 
 reference time: 07:06:50.505 UTC Oct 17 2018(DF715E0A.817A56DE)
 synchronization state: clock synchronized

补充:

ntpd、ntpdate的区别,下面是网上关于ntpd与ntpdate区别的相关资料。如下所示所示:

使用之前得弄清楚一个问题,ntpd与ntpdate在更新时间时有什么区别。ntpd不仅仅是时间同步服务器,它还可以做客户端与标准时间服务器进行同步时间,而且是平滑同步,并非ntpdate立即同步,在生产环境中慎用ntpdate,也正如此两者不可同时运行。

时钟的跃变,对于某些程序会导致很严重的问题。许多应用程序依赖连续的时钟,例如数据库事务。ntpdate调整时间的方式就是我们所说的”跃变“:在获得一个时间之后,ntpdate使用settimeofday(2)设置系统时间,这有几个非常明显的问题:

第一,这样做不安全。ntpdate的设置依赖于ntp服务器的安全性,***者可以利用一些软件设计上的缺陷,拿下ntp服务器并令与其同步的服务器执行某些消耗性的任务。由于ntpdate采用的方式是跳变,跟随它的服务器无法知道是否发生了异常(时间不一样的时候,唯一的办法是以服务器为准)。

第二,这样做不精确。一旦ntp服务器宕机,跟随它的服务器也就会无法同步时间。与此不同,ntpd不仅能够校准计算机的时间,而且能够校准计算机的时钟。

第三,这样做不够优雅。由于是跳变,而不是使时间变快或变慢,依赖时序的程序会出错(例如,如果ntpdate发现你的时间快了,则可能会经历两个相同的时刻,对某些应用而言,这是致命的)。因而,唯一一个可以令时间发生跳变的点,是计算机刚刚启动,但还没有启动很多服务的那个时候。其余的时候,理想的做法是使用ntpd来校准时钟,而不是调整计算机时钟上的时间。

NTPD 在和时间服务器的同步过程中,会把 BIOS 计时器的振荡频率偏差——或者说 Local Clock 的自然漂移(drift)——记录下来。这样即使网络有问题,本机仍然能维持一个相当精确的走时。


综上,完成Linux 系统时区设置,NTP时间服务器搭建,安装,配置及不同NTP client的配置等。

转载请注明出处:http://blog.51cto.com/itwish 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐