{channel.title!channel.name} - 楚淮公司周东林

centos7修改时区配置ntp同步

centos安装的时候选择的是英文版时区当时记得选的是Asia/Shanghai,不知怎么的配置了NTP时间还是不对。


1.查看系统的时区

[root@zhoudl etc]# timedatectl     
      Local time: Wed 2018-09-19 10:23:21 CST
  Universal time: Wed 2018-09-19 02:23:21 UTC
        RTC time: Wed 2018-09-19 02:23:21
       Time zone: America/New_York (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

看 Time zone: 一行是America/New_York (CST, +0800),所以怎么同步时间都和本地时间不一致。

2.修改时区

[root@zhoudl etc]# timedatectl set-local-rtc 1       # 将硬件时钟调整为CST时间, 0 为设置为 UTC 时间。硬件时间建议使用 UTC 时间不然会用如下报错

Warning: The system is configured to read the RTC time in the local time zone.
         This mode can not be fully supported. It will create various problems
         with time zone changes and daylight saving time adjustments. The RTC
         time is never updated, it relies on external facilities to maintain it.
         If at all possible, use RTC in UTC by calling
         'timedatectl set-local-rtc 0'.

timedatectl set-timezone Asia/Shanghai              # 设置系统时区为上海

​3.通过阿里云的时间同步服务器同步本地时间

ntpdate ntp1.aliyun.com

​延伸:如果不考虑各个发行版的差异化, 从更底层出发的话, 修改时间时区比想象中要简单:
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

上面这步当时我测试了需要重启操作系统才能成功

 

4.配置ntp服务步骤

4.1  安装服务 yum install ntp ntpdate

4.2 修改etc/ntp.conf文件把阿里云的时间服务器加上

server time1.aliyun.com  prefer true iburst
server ntp1.aliyun.com true iburst

4.3 systemctl restart ntpd  # 启动ntp服务

4.4 ntpq -p   #查看网络中的ntp服务器其中 * 表示正在使用的服务器

[root@zhoudl etc]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*203.107.6.88    10.165.84.13     2 u   20   64    1   24.308    1.678   0.165
+120.25.115.20   10.137.53.7      2 u   19   64    1   31.172    0.102   0.240

4.5 ntpdate   #同步本地时间

[root@zhoudl etc]# ntptime
ntp_gettime() returns code 0 (OK)
  time df4c36b3.e2a0a904  Wed, Sep 19 2018 10:45:07.885, (.885264236),
  maximum error 1163450 us, estimated error 541 us, TAI offset 0
ntp_adjtime() returns code 0 (OK)
  modes 0x0 (),
  offset 294.742 us, frequency 0.010 ppm, interval 1 s,
  maximum error 1163450 us, estimated error 541 us,
  status 0x2001 (PLL,NANO),
  time constant 6, precision 0.001 us, tolerance 500 ppm,

[root@zhoudl etc]# timedatectl
      Local time: Wed 2018-09-19 11:22:48 CST
  Universal time: Wed 2018-09-19 03:22:48 UTC
        RTC time: Wed 2018-09-19 03:22:49
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

依然有瑕疵虽然date 看系统时间对了但是 RTC time Universal time 时间和本地时间不一致,建议不用处理,因为硬件时钟默认使用UTC时间,硬件时钟不能保存时区和夏令时调整,修改后就无法从硬件时钟中读取出准确标准时间,因此不建议修改。修改后系统会出现下面的警告。但是有些应用代码调用的是硬件时间会比本地时间早8小时,改不改自行斟酌。

Warning: The system is configured to read the RTC time in the local time zone.
         This mode can not be fully supported. It will create various problems
         with time zone changes and daylight saving time adjustments. The RTC
         time is never updated, it relies on external facilities to maintain it.
         If at all possible, use RTC in UTC by calling
         'timedatectl set-local-rtc 0'.

[root@zhoudl etc]# date
Wed Sep 19 10:58:04 CST 2018

4.6 clock -w  #将日期写入CMOS

[root@zhoudl etc]# timedatectl                  
      Local time: Wed 2018-09-19 10:55:00 CST
  Universal time: Wed 2018-09-19 02:55:00 UTC
        RTC time: Wed 2018-09-19 10:55:00
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: yes
      DST active: n/a

 

 


浏览.发表于:2018-09-19 10:40:28