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

本篇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.
  12. Afghanistan 18) Israel 35) Palestine
  13. Armenia 19) Japan 36) Philippines
  14. Azerbaijan 20) Jordan 37) Qatar
  15. Bahrain 21) Kazakhstan 38) Russia
  16. Bangladesh 22) Korea (North) 39) Saudi Arabia
  17. Bhutan 23) Korea (South) 40) Singapore
  18. Brunei 24) Kuwait 41) Sri Lanka
  19. Cambodia 25) Kyrgyzstan 42) Syria
  20. China 26) Laos 43) Taiwan
  21. Cyprus 27) Lebanon 44) Tajikistan
  22. East Timor 28) Macau 45) Thailand
  23. Georgia 29) Malaysia 46) Turkmenistan
  24. Hong Kong 30) Mongolia 47) United Arab Emirates
  25. India 31) Myanmar (Burma) 48) Uzbekistan
  26. Indonesia 32) Nepal 49) Vietnam
  27. Iran 33) Oman 50) Yemen
  28. Iraq 34) Pakistan
    #? 9 # 选择China
    Please select one of the following time zone regions.
  29. Beijing Time
  30. 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

ntp.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

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

restrict default kod nomodify notrap nopeer noquery # 拒绝所有IPv4的client连接此NTP服务器
restrict -6 default kod nomodify notrap nopeer noquery # 拒绝所有IPv6的client连接此NTP服务器

restrict 127.0.0.1 # 放行本机localhost对NTP服务的访问
restrict -6 ::1

Hosts on local network are less restricted.

#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap # 放行192.168.1.0网段主机与NTP服务器进行时间同步

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 [netmask_IP] [parameter]

Parameter 的
ignore :拒绝所有类型的NTP联机。
nomodify: 客户端不能使用ntpc与ntpq这两个程序来修改服务器的时间参数,但客户端可透过这部主机来进行网络校时;
noquery:客户端不能够使用ntpc与ntpq等指令来查询时间服务器,不提供NTP的网络校时。
notrap:不提供trap 这个运程事件登入的功能。
notrust:拒绝没有认证的客户端。
Kod:kod技术可以阻止“Kiss of Death “包对服务器的破坏。
Nopeer:不与其他同一层的NTP服务器进行时间同步。

利用server 设定上层NTP服务器,格式如下:
server [IP or hostname] [prefer]

参数主要如下:
perfer:表示优先级最高
burst :当一个运程NTP服务器可用时,向它发送一系列的并发包进行检测。
iburst :当一个运程NTP服务器不可用时,向它发送一系列的并发包进行检测。

2、配置NTPserver

调整NTP配置文件

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

Hosts on local network are less restricted.

允许局域网内设备与这台服务器进行同步时间.但是拒绝让他们修改服务器上的时间

restrict 192.168.23.0 mask 255.255.255.0 nomodify notrap
restrict 192.168.10.0 mask 255.255.255.0 nomodify notrap
restrict 192.168.20.0 mask 255.255.255.0 nomodify notrap
restrict 172.16.10.0 mask 255.255.255.0 nomodify notrap
restrict 192.168.100.0 mask 255.255.255.0 nomodify notrap

允许上层时间服务器修改本机时间

restrict times.aliyun.com nomodify
restrict ntp.aliyun.com nomodify
restrict cn.pool.ntp.org nomodify

定义要同步的时间服务器

server times.aliyun.com iburst prefer # prefer表示为优先,表示本机优先同步该服务器时间
server ntp.aliyun.com iburst
server cn.pool.ntp.org iburst

logfile /var/log/ntpstats/ntpd.log # 定义ntp日志目录
pidfile /var/run/ntp.pid # 定义pid路径

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、实现clock时间与system时间同步,配置/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 10 #本NTP服务器层次为10,已向120.25.115.19 NTP同步过
time correct to within 33 ms # 时间校正到相差33ms之内
polling server every 256 s # 每256秒会向上级NTP轮询更新一次时间

[root@vm ~]# ntpq -p # 查看本地NTP需进行同步的公网NTP服务器状态
remote refid st t when poll reach delay offset jitter

*203.107.6.88 100.107.25.114 2 u 306 1024 377 15.067 -0.283 1.171
+120.25.115.19 10.137.53.7 2 u 979 1024 377 36.893 -0.671 1.219
+119.28.183.184 100.122.36.196 2 u 1036 1024 377 49.942 7.536 18.648

参数详解

remote :本地主机所连接的上层NTP服务器,最左边的符号如下:
如果有[*] 代表目前正在使用当中的上层NTP服务器。
如果有[+] 代表也有连上上层NTP服务器,可以作为提高时间更新的候选NTP服务器
如果有[-] 代表同步的该NTP服务器被认为是不合格的NTP Server
如果有[x] 代表同步的外网NTP服务器不可用
refid :指的是给上层NTP服务器提供时间校对的服务器。
St:上层NTP服务器的级别。
When: 上一次与上层NTP服务器进行时间校对的时间(单位:s)
Poll :本地主机与上层NTP服务器进行时间校对的周期(单位:s)
reach:已经向上层 NTP 服务器要求更新的次数
delay:网络传输过程当中延迟的时间,单位为 10^(-6) 秒
offset:时间补偿的结果,单位为10^(-6) 秒
jitter:Linux 系统时间与 BIOS 硬件时间的差异时间, 单位为 10^(-6) 秒。

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
restrict 192.168.23.100 nomodify notrap noqurey

[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

[root@zabbix ~]# ntpstat
synchronised to NTP server (192.168.23.100) at stratum 4 #本NTP服务器层次为4,已向203.107.6.88 NTP同步过
time correct to within 37 ms # 时间校正到相差37ms之内
polling server every 128 s # 每128秒会向上级NTP轮询更新一次时间

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

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

image.png

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

image.png

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

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

[HX]ntp-service server disable

[HX]ntp-service ipv6 server disable

[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)——记录下来。这样即使网络有问题,本机仍然能维持一个相当精确的走时。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值