一、时区划分方法
地球是自西向东自转,东边比西边先看到太阳,东边的时间也比西边的早。东边时刻与西边时刻的差值不仅要以时计,而且还要以分和秒来计算,这给人们带来不便。
为了克服时间上的混乱,1884年在华盛顿召开的一次国际经度会议(又称国际子午线会议)上,规定将全球划分为24个时区(东、西各12个时区)。规定英国(格林尼治天文台旧址)为中时区(零时区)、东1—12区,西1—12区。每个时区横跨经度15度,时间正好是1小时。最后的东、西第12区各跨经度7.5度,以东、西经180度为界。每个时区的中央经线上的时间就是这个时区内统一采用的时间,称为区时,相邻两个时区的时间相差1小时。
二、UTC、CMT、CST、DST 时间
UTC(Universal Time Coordinated)
世界协调时间;经严谨计算得到的时间,精确到秒,误差在0.9s以内, 是比GMT更为精确的世界时间。
GMT(Greenwich Mean Time)
格林威治标准时间 ; 英国伦敦格林威治定为0°经线开始的地方,地球每15°经度 被分为一个时区,共分为24个时区,相邻时区相差一小时;例: 中国北京位于东八区,GMT时间比北京时间慢8小时。
CST:
CST同时可以代表如下 4 个不同的时区
Central Standard Time (USA) UT-6:00 美国标准时间
Central Standard Time (Australia) UT+9:30 澳大利亚标准时间
China Standard Time UT+8:00 中国标准时间
Cuba Standard Time UT-4:00 古巴标准时间
DST(Daylight Saving Time)
夏季节约时间,即夏令时;是为了利用夏天充足的光照而将时间调早一个小时,北美、欧洲的许多国家实行夏令时;
三、硬件时间和系统时间
硬件时间
RTC(Real-Time Clock)或CMOS时间,一般在主板上靠电池供电,服务器断电后仍然继续运行。仅保存日期时间数值,无法保存时区和夏令时设置。
系统时间
一般在服务器启动时复制RTC时间,之后独立运行,保存了时间、时区和夏令时设置。
四、时间管理命令
1. 查看当前系统时间、时区
[root@test-1 ~]# timedatectl
Local time: Thu 2021-12-02 08:51:51 CST
Universal time: Thu 2021-12-02 00:51:51 UTC
RTC time: Thu 2021-12-02 00:51:51
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
[root@test-1 ~]# timedatectl status
Local time: Thu 2021-12-02 08:51:56 CST
Universal time: Thu 2021-12-02 00:51:56 UTC
RTC time: Thu 2021-12-02 00:51:56
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
2. 列出全世界所有的时区
[root@test-1 ~]# timedatectl list-timezones | grep Asia
...
Asia/Hong_Kong
Asia/Shanghai
Asia/Taipei
Asia/Urumqi
...
3. 设置时区
方法1:将时区设置为上海
[root@test-1 ~]# timedatectl set-timezone Asia/Shanghai
方法2:直接修改符号链接
[root@test-1 ~]# rm /etc/localtime
[root@test-1 ~]# ln -s ../usr/share/zoneinfo/Asia/Shanghai /etc/localtime
4. 设置时间
方法1:使用timedatectl
NTP enabled: yes时,使用了NTP服务器自动同步时间,若坚持要手动修改时间,先timedatectl set-ntp no。
设置日期和时间
[root@test-1 ~]# timedatectl set-time '2018-10-11 09:00:00'
设置日期
[root@test-1 ~]# timedatectl set-time '2018-10-11'
设置时间
[root@test-1 ~]# timedatectl set-time '09:00:00'
方法2:使用date
[root@test-1 ~]# date -s '2018-10-11 09:00:00'
5. 同步系统时间到硬件时间
方法1:不建议硬件时间随系统时间变化
设置硬件时间随系统时间变化
[root@test-1 ~]# timedatectl set-local-rtc 1
设置硬件时间不随系统时间变化
[root@test-1 ~]# timedatectl set-local-rtc 0
方法2
[root@test-1 ~]# hwclock --systohc
6. 是否启用自动同步时间
启用|停用自动同步时间
[root@test-1 ~]# timedatectl set-ntp yes|no
上面的命令其实是启用、停用时间服务器,若安装了chrony服务,则等同于对该服务启停,若只安装了ntp,则是对ntp服务启停。
对chrony服务启停
[root@test-1 ~]# systemctl start|stop chronyd
对ntp服务启停
[root@test-1 ~]# systemctl start|stop ntpd
五、自动同步时间服务及命令
CentOS7之前采用ntp服务自动同步时间,CentOS7推荐使用chrony同步时间,当然ntp仍然可以使用,chrony官网列举了诸多chrony优于ntp的功能。
此外我们若要立刻将系统时间同步为NTP服务时间,使用ntpdate命令,也可以配置计划任务定期使用ntpdate命令同步时间,从而就不用使用ntp或chrony服务,减少监听的端口,增加系统安全。
1. NTP服务
1.1 服务端部署——NTP服务
1.1.1 时区设置
[root@localhost ~]# timedatectl set-timezone Asia/Shanghai
1.1.2 安装
[root@localhost ~]# yum -y install ntp
[root@localhost ~]# rpm -q ntp
1.1.3 修改配置文件
国内ntp服务器参考:
https://www.pool.ntp.org/zone/cn
http://www.ntp.org.cn/pool.php#china
[root@localhost ~]# vim /etc/ntp.conf
driftfile /var/lib/ntp/drift
# 新增:日志目录
logfile /var/log/ntpd.log
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1
# 新增:授权172.16.128.0 网段上的所有机器可以从这台机器上查询和同步时间
restrict 172.16.128.0 mask 255.255.255.0 nomodify notrap
# 修改:时间服务器列表
server 0.cn.pool.ntp.org iburst
server 1.cn.pool.ntp.org iburst
server 2.cn.pool.ntp.org iburst
server 3.cn.pool.ntp.org iburst
# 新增:当外部时间不可用时,使用本地时间
server 172.16.128.171 iburst
fudge 127.0.0.1 stratum 10
# 新增:允许上层时间服务器主动修改本机时间
restrict 0.cn.pool.ntp.org nomodify notrap noquery
restrict 1.cn.pool.ntp.org nomodify notrap noquery
restrict 2.cn.pool.ntp.org nomodify notrap noquery
restrict 3.cn.pool.ntp.org nomodify notrap noquery
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
restrict(管理NTP权限控制)
参数:
- Ignore 拒绝所有类型的NTP连接
- nomodify 客户端不能使用ntpc与ntpq程序来修改服务器的时间参数,但可以同步
- noquery 不提供NTP服务
- notrap 不提供trap远程日志服务
- notrust 拒绝没有认证的客户端提供NTP
server(设置上端同步NTP)
参数:
- prefer 优先级设定
- iburst 当一个远程NTP服务器不可用时,向它发送一系列的并发包进行检测
- burst 当一个远程NTP服务器可用时,向它发送一系列的并发包进行检测
1.1.4 启动服务
[root@localhost ~]# systemctl enable ntpd
[root@localhost ~]# systemctl start ntpd
1.1.5 查看同步状态
[root@localhost ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*114.118.7.161 123.139.33.3 2 u 36 64 77 23.196 6.770 1.028
+120.25.115.20 10.137.53.7 2 u 24 64 77 20.913 6.897 1.506
-139.199.215.251 100.122.36.196 2 u 22 64 77 21.854 8.122 1.458
-119.28.183.184 100.122.36.196 2 u 19 64 77 33.594 4.266 2.011
+ntp6.flashdance 194.58.202.148 2 u 91 64 72 268.044 6.483 1.516
+119.28.206.193 100.122.36.4 2 u 20 64 77 32.332 7.080 1.344
[root@localhost ~]# ntpstat
synchronised to NTP server (114.118.7.161) at stratum 3
time correct to within 996 ms
polling server every 64 s
ntpq -p 输出详解
- remote:响应这个请求的 NTP 服务器的名称。
- refid:NTP 服务器使用的上一级 NTP 服务器。
- st:remote 远程服务器的级别。服务器从高到低级别设定为1 - 16,为了减缓负荷和网络堵塞,原则上建议避免直接连接到级别为1的服务器。
- t :u: unicast(单播), b: broadcast(广播), l: local(本地时钟)
- when:上一次成功请求之后到现在的秒数。
- poll:本地机和远程服务器多少时间进行一次同步(单位为秒)。初始运行 NTP 时,poll 值会比较小,和服务器同步的频率增加,建议尽快调整到正确的时间范围。调整之后,poll 值会逐渐增大,同步的频率也将会相应减小。
- reach:八进制值,用来测试能否和服务器连接。每成功连接一次,reach 的值将会增加。
- delay:从本地机发送同步要求到 NTP 服务器的 round trip time。
- offset:主机通过 NTP 时钟同步与所同步时间源的时间偏移量,单位为毫秒(ms)。offset 越接近于0,主机和 NTP 服务器的时间越接近。
- jitter:用来做统计的值。统计在特定连续的连接数里 offset 的分布情况。即 jitter 数值的绝对值越小,主机的时间就越精确。
remote 状态标识详解
- 空:表示没有远程通信的主机
- x:已不再使用
- -:已不再使用
- #:良好的远程节点或服务器但是未被使用
- +:良好的且优先使用的远程节点或服务器
- *:当前作为优先主同步对象的远程节点或服务器
- o:PPS 节点 (当优先节点是有效时)。实际的系统同步是源于秒脉冲信号(pulse-per-second,PPS),可能通过PPS 时钟驱动或者通过内核接口
1.2 客户端同步——NTP服务方式
1.2.1 时区设置
[root@localhost ~]# timedatectl set-timezone Asia/Shanghai
1.2.2 安装
[root@localhost ~]# yum -y install ntp
[root@localhost ~]# rpm -q ntp
1.2.3 修改配置文件
[root@localhost ~]# sed -i.bak '/centos.pool.ntp.org/s/^/#/' /etc/ntp.conf
[root@localhost ~]# sed -i.bak '/driftfile/i server ntpserver iburst' /etc/ntp.conf
[root@localhost ~]# sed -i.bak '/driftfile/i restrict ntpserver nomodify notrap noquery' /etc/ntp.conf
[root@localhost ~]# sed -i.bak '$a 172.27.9.131 ntpserver' /etc/hosts
1.2.4 启动服务
[root@localhost ~]# systemctl enable ntpd
[root@localhost ~]# systemctl start ntpd
1.2.5 查看同步状态
[root@localhost ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*ntpserver 114.118.7.161 3 u 1 64 3 0.221 12.641 0.495
[root@localhost ~]# ntpstat
synchronised to NTP server (172.27.9.131) at stratum 4
time correct to within 1049 ms
polling server every 64 s
1.3 客户端同步——NTPDATE服务方式
1.3.1 时区设置
[root@localhost ~]# timedatectl set-timezone Asia/Shanghai
1.3.2 安装
[root@localhost ~]# yum -y install ntp ntpdate
[root@localhost ~]# rpm -q ntp
1.3.3 修改配置文件
[root@localhost ~]# sed -i.bak '/centos.pool.ntp.org/s/^/#/' /etc/ntp.conf
[root@localhost ~]# sed -i.bak '/driftfile/i server ntpserver iburst' /etc/ntp.conf
[root@localhost ~]# sed -i.bak '/driftfile/i restrict ntpserver nomodify notrap noquery' /etc/ntp.conf
[root@localhost ~]# sed -i.bak '$a 172.27.9.131 ntpserver' /etc/hosts
1.3.4 启动服务
[root@localhost ~]# systemctl enable ntpdate
[root@localhost ~]# systemctl start ntpdate
1.4 客户端同步——ntpdate命令方式
[root@centos7 ~]# systemctl stop ntpd
[root@centos7 ~]# ntpdate ntpserver
7 Nov 16:33:02 ntpdate[2618]: adjust time server 172.27.9.131 offset -0.002048 sec
[root@centos7 ~]# crontab -l
0 1 * * * /usr/sbin/ntpdate ntpserver
2. Chrony服务
2.1 Chrony服务端
2.1.1 安装
[root@ntp ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@ntp ~]# yum -y install chrony
2.1.2 修改配置文件
[root@ntp ~]# vim /etc/chrony.conf
# 时间服务器列表
server 0.cn.pool.ntp.org iburst
server 1.cn.pool.ntp.org iburst
server 2.cn.pool.ntp.org iburst
server 3.cn.pool.ntp.org iburst
# 允许同步的网段
allow 192.168.10.0/24
2.1.3 启动服务
[root@ntp ~]# systemctl start chronyd
[root@ntp ~]# systemctl enable chronyd
2.1.4 验证服务
查看时间同步源
[root@ntp ~]# 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
===============================================================================
^- ntp7.flashdance.cx 2 6 13 0 -30ms[ -30ms] +/- 165ms
^- undefined.hostname.local> 2 6 7 2 +11ms[+7065us] +/- 106ms
^* 119.28.183.184 2 6 17 0 -473us[-4819us] +/- 32ms
^- ntp6.flashdance.cx 2 6 7 2 -59ms[ -63ms] +/- 160ms
查看时间同步源状态
[root@ntp ~]# chronyc sourcestats -v
210 Number of sources = 4
.- Number of sample points in measurement set.
/ .- Number of residual runs with same sign.
| / .- Length of measurement set (time).
| | / .- Est. clock freq error (ppm).
| | | / .- Est. error in freq.
| | | | / .- Est. offset.
| | | | | | On the -.
| | | | | | samples. \
| | | | | | |
Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev
==============================================================================
ntp7.flashdance.cx 4 3 11 +312.500 41625.355 -28ms 8674us
undefined.hostname.local> 4 3 7 -116.341 3042.866 +398us 678us
119.28.183.184 4 3 7 +185.734 12278.089 +5256us 2166us
ntp6.flashdance.cx 4 3 8 -789.719 15890.097 -75ms 3025us
2.2 Chrony客户端
2.2.1 安装
[root@ntp ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@ntp ~]# yum -y install chrony
2.2.2 修改配置文件
[root@ntp ~]# egrep -v "^#|^$" /etc/chrony.conf
server 192.168.10.8 iburst //同步时间服务器
stratumweight 0
driftfile /var/lib/chrony/drift
rtcsync
makestep 10 3
bindcmdaddress 127.0.0.1
bindcmdaddress ::1
keyfile /etc/chrony.keys
commandkey 1
generatecommandkey
noclientlog
logchange 0.5
logdir /var/log/chrony
2.2.3 启动服务
[root@ntp ~]# systemctl start chronyd
[root@ntp ~]# systemctl enable chronyd
2.1.4 验证服务
查看时间同步源
[root@ntp ~]# chronyc sources -v
210 Number of sources = 1
.-- 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
===============================================================================
^* 192.168.0.69 3 6 37 32 +395us[ +768us] +/- 37ms
查看时间同步源状态
[root@ntp ~]# chronyc sourcestats -v
210 Number of sources = 1
.- Number of sample points in measurement set.
/ .- Number of residual runs with same sign.
| / .- Length of measurement set (time).
| | / .- Est. clock freq error (ppm).
| | | / .- Est. error in freq.
| | | | / .- Est. offset.
| | | | | | On the -.
| | | | | | samples. \
| | | | | | |
Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev
==============================================================================
192.168.0.69 5 4 71 +0.690 46.667 +29us 15us