一、描述
CentOS 7 配置ntp时,在ntp.conf中配置server 10.10.192.1 iburst
与server 10.10.192.3 iburst
,同时执行systemctl start ntpd
启动对时服务,但是在第二天执行date
查看时,服务器时钟仍然存在一天的误差,ntp对时居然无效。
[root@localhost ~]# date
2023-01-30 01:02:25
#实际时间是2023-01-31才对
#此处date显示的内容是因为执行过alias date="date '+%Y-%m-%d %H:%M:%S'"
二、排查过程:时区显示的问题
-
从ntp配置方面排查了多次没有发现问题。
-
查看/var/log/message中ntp日志,发现具有对时操作,ntp服务正常
Jan 30 01:02:11 localhost ntpd[10398]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM
Jan 30 01:02:11 localhost ntpd[10398]: 0.0.0.0 c011 01 freq_not_set
Jan 30 01:02:19 localhost ntpd[10398]: 0.0.0.0 c61c 0c clock_step +0.947240 s
Jan 30 01:02:19 localhost ntpd[10398]: 0.0.0.0 c614 04 freq_mode
Jan 30 01:02:20 localhost ntpd[10398]: 0.0.0.0 c618 08 no_sys_peer
Jan 30 01:17:41 localhost ntpd[10398]: 0.0.0.0 c612 02 freq_set kernel 2.066 PPM
Jan 30 01:17:41 localhost ntpd[10398]: 0.0.0.0 c615 05 clock_sync
- 执行ntpstat查看状态,由于长时间对时,当前对时周期为1024秒
[root@localhost ~]# ntpstat
unsynchronised
polling server every 1024 s
- 百思不得其解之时,上网寻求帮助,看到一篇文章提示,要注意时区是否一致。随后使用原始的date命令查看:
[root@localhost ~]# unalias date
[root@localhost ~]# date
Mon Jan 30 20:19:46 EST 2023
注意到上面的时区是”EST“,这是Eastern Standard Time东部标准时间,主要用于北美。我国用的时间是中国标准时间CST(China Standard Time)
三、问题解决:修改时间时区
查看当前系统设置的时区
ba[root@localhost ~]# ll /etc/localtime
lrwxrwxrwx. 1 root root 38 2021-08-25 04:51:23 /etc/localtime -> ../usr/share/zoneinfo/America/New_York
[root@localhost ~]# echo $TZ
[root@localhost ~]# timedatectl
Local time: Mon 2023-01-30 20:33:31 EST
Universal time: Tue 2023-01-31 01:33:31 UTC
RTC time: Tue 2023-01-31 01:33:31
Time zone: America/New_York (EST, -0500)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: no
Last DST change: DST ended at
Sun 2022-11-06 01:59:59 EDT
Sun 2022-11-06 01:00:00 EST
Next DST change: DST begins (the clock jumps one hour forward) at
Sun 2023-03-12 01:59:59 EST
Sun 2023-03-12 03:00:00 EDT
[root@localhost ~]# date -R
Mon, 30 Jan 2023 20:34:03 -0500
#可以通过timedatectl list-timezones查看系统当前支持的时区
设置时区
- 声明变量,该方法仅是修改当前会话的时区,在重新登录时失效
[root@eas-heq61-12-12-46 ~]# export TZ="Asia/Shanghai"
[root@eas-heq61-12-12-46 ~]# date
Tue Jan 31 09:30:46 CST 2023
- 使用命令:timedatectl ,永久有效
[root@localhost ~]# date
Mon Jan 30 20:36:53 EST 2023
[root@localhost ~]# timedatectl
Local time: Mon 2023-01-30 20:38:21 EST
Universal time: Tue 2023-01-31 01:38:21 UTC
RTC time: Tue 2023-01-31 01:38:21
Time zone: America/New_York (EST, -0500)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: no
Last DST change: DST ended at
Sun 2022-11-06 01:59:59 EDT
Sun 2022-11-06 01:00:00 EST
Next DST change: DST begins (the clock jumps one hour forward) at
Sun 2023-03-12 01:59:59 EST
Sun 2023-03-12 03:00:00 EDT
[root@localhost ~]# timedatectl set-timezone Asia/Shanghai
[root@localhost ~]# date
Tue Jan 31 09:38:39 CST 2023
[root@localhost ~]# timedatectl
Local time: Tue 2023-01-31 09:38:47 CST
Universal time: Tue 2023-01-31 01:38:47 UTC
RTC time: Tue 2023-01-31 01:38:47
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
[root@localhost ~]# ll /etc/localtime
lrwxrwxrwx 1 root root 35 Jan 31 09:38 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai
网友评论