迷,什么都是道理;悟,什么都不是道理。

CentOS   MySQL

导航

CentOS 7 时间与同步

目录

时间概念  时间设置   NTP

 

时间概念 

UTC:世界协调时间(Universal Time Coordinated)是世界上不同国家用来调节时钟和时间的主要时间标准。也就是零时区的时间
GMT:格林威治标准时间 (Greenwich Mean Time)
区别:UTC 与 GMT 基本同一概念,UTC 比 GMT 更精确。 CST时间:中央标准时间 Central Standard Time (USA) UT
-6:00(美国cst时间:零区时减6个小时。-6:00为负数相比零时区晚了6个小时。) Central Standard Time (Australia) UT+9:30(澳大利亚cst:加9个半小时) China Standard Time UT+8:00(中国cst:加8个小时) Cuba Standard Time UT-4:00 (古巴cst:减4个小时) 如:当UTC时间为0点时,中国CST时间为8点,因为零时区和中国北京时区相差8个时区。

 

[ root@stock.data.news.100 pts/0 2021-01-31/31@7 10:51:54 /Server/datas/downloads/script ] 
# timedatectl 
      Local time: Sun 2021-01-31 10:52:03 CST  # 当下操作系统的时间
  Universal time: Sun 2021-01-31 02:52:03 UTC  # 当下操作系统的零时区时间
        RTC time: Sun 2021-01-31 02:52:02
       Time zone: Asia/Shanghai (CST, +0800)   # 当下时区,比零时区提前8个小时00分。UTC(02:52:03) + 0800(08:时间,00:分)= CTS(10:52:03)
     NTP enabled: yes   # yes 开启了与时间服务器同步功能: timedatectl set-ntp yes
NTP synchronized: no    # 当yes的时候,表示该服务已经与时间服务器完成同步,可以为其他客户端提供时间同步。
 RTC in local TZ: no    # RTC(Real Time Clock) :主板上的硬件时钟芯片,为主板提供时间。
      DST active: n/a

 

系统时间设置

[ root@stock.data.news.100 pts/0 2021-01-31/31@7 16:37:20 /Server/datas/downloads/script ] 
# timedatectl help
timedatectl [OPTIONS...] COMMAND ...

Query or change system time and date settings.

  -h --help                Show this help message
     --version             Show package version
     --no-pager            Do not pipe output into a pager
     --no-ask-password     Do not prompt for password
  -H --host=[USER@]HOST    Operate on remote host
  -M --machine=CONTAINER   Operate on local container
     --adjust-system-clock Adjust system clock when changing local RTC mode

Commands:
  status                   Show current time settings
  set-time TIME            Set system time  设置系统时间(默认系统CTS)   timedatectl set-time 'yyyy-mm-dd hh:mm:ss' | 'yyyy-mm-dd' | 'hh:mm:ss'
效果等同date date --set 16:38:00 或 date --set '2020-01-01 16:38:00'
效果等同date 设置 UTC date --set --utc '2020-01-01 16:38:00'
set-timezone ZONE Set system time zone 设置系统时区 # timedatectl set-timezone Asia/Shanghai list-timezones Show known time zones 显示可以时区 # timedatectl list-timezones | grep -i 'shanghai' set-local-rtc BOOL Control whether RTC is in local time 将系统时间更新到硬件时钟中,当 1(yes) :开启;0(no):关闭。 开启的效果等同 hwclock --systohc --localtime set-ntp BOOL Control whether NTP is enabled 当bool为 yes 或 0 时启用系统时间与NTP(时间服务器)同步 # timedatectl set-ntp yes
当设置(yes)了与NTP同步后,timedatectl set-time time 则不起效果
启用该功能的前提是 NTP服务 能与 NTP服务器 自动同步

 

设置硬件(主板)时钟

[ root@stock.data.news.100 pts/0 2021-01-31/31@7 17:37:27 /Server/datas/downloads/script ] 
# date;hwclock
Sun Jan 31 17:38:10 CST 2021
Sun 31 Jan 2021 05:38:11 PM CST  -0.489765 seconds # 主板时间 比 系统时间 快了0.489765秒

# hwclock --systohc 将系统时间更新到主板时间,其中 sys 代表系统时间,hc 代表主板时间
# hwclock --hctosys 将主板时间更新到系统时间

# hwclock --systohc --date '2020-01-01 17:23:00' --utc 手动设置硬件时钟,其中 --utc 表示设置的时间的格式。

 

 

相关时间同步工具(其中 chrony 与 ntpdate / ntp 使用方式基本一致)

yum install chrony ntpdate ntp -y

 

NTP 公共时间服务器池(NTP Public Pool Time Servers),为客户端提供时间同步。(使用原则:串联同步。本地客户端 --同步--> 本地服务端 --同步--> 公网时间服务器)

 

本地服务器端设置

[ root@VM-0-9-centos pts/2 2021-02-02/28@2 15:05:31 ~ ] 
# grep -E '^server' /etc/ntp.conf 
server time1.tencentyun.com iburst  # time1.tencentyun.com 为 腾讯云的时间服务器,其他的为关键字
server time2.tencentyun.com iburst
server time3.tencentyun.com iburst
server time4.tencentyun.com iburst
server time5.tencentyun.com iburst

[ root@VM-0-9-centos pts/2 2021-02-02/28@2 15:06:46 ~ ]
# systemctl start ntpd

[ root@VM-0-9-centos pts/2 2021-02-02/28@2 15:06:46 ~ ]
# systemctl enable ntpd

[ root@VM-0-9-centos pts/2 2021-02-02/28@2 15:06:46 ~ ]
# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*169.254.0.79    100.122.36.196   2 u  177 1024  377    0.833   -0.110   0.319
-169.254.0.80    100.122.36.196   2 u  742 1024  377    0.871   -0.619   0.381
+169.254.0.81    100.122.36.4     2 u  612 1024  377    0.834   -0.262   0.259
+169.254.0.82    100.122.36.196   2 u  667 1024  377    0.834   -0.041   0.304
-169.254.0.83    100.122.36.4     2 u  592 1024  377    1.010   -0.458   0.409

# 防火墙开放 ntp 端口
[ root@VM-0-9-centos pts/2 2021-02-02/28@2 15:08:29 ~ ]
# firewall-cmd --permanent --zone=public --add-port=123/udp

[ root@VM-0-9-centos pts/2 2021-02-02/28@2 15:08:29 ~ ]
# firewall-cmd --reload

 

国内常用的时间服务器

cn.ntp.org.cn    # 中国
edu.ntp.org.cn   # 中国教育网
ntp1.aliyun.com  # 阿里云
ntp2.aliyun.com  # 阿里云
cn.pool.ntp.org  # 最常用的国内NTP服务器
s1a.time.edu.cn # 北京邮电大学
s1b.time.edu.cn # 清华大学
s1c.time.edu.cn # 北京大学

 

本地客户端

[ root@VM-0-10-centos pts/2 2021-02-02/28@2 15:22:26 ~ ] 
# grep -E '^server' /etc/ntp.conf
server 172.16.0.9 iburst  # 本地时间服务器 ip

[ root@VM-0-9-centos pts/2 2021-02-02/28@2 15:22:31 ~ ]
# ntpdate -u 172.16.0.9 # 与本地服务器同步,其后使用 ntpq -p 查看。同时需要开启 ntpd 服务。

 

本地客户端 使用ntpdate同步也可以

[ root@stock.data.news.100 pts/0 2021-02-02/28@2 17:27:41 ~ ] 
# crontab -l
#
#

[ root@stock.data.news.100 pts/0 2021-02-02/28@2 17:28:20 ~ ]
# echo '# time sync' >> /var/spool/cron/root

[ root@stock.data.news.100 pts/0 2021-02-02/28@2 17:28:42 ~ ]
# echo '*/5 * * * * /usr/sbin/ntpdate 172.16.0.9 &>/dev/null;/usr/sbin/hwclock --systohc ' >>/var/spool/cron/root

[ root@stock.data.news.100 pts/0 2021-02-02/28@2 17:29:52 ~ ]
# crontab -l
#
#
# time sync
*/5 * * * * /usr/sbin/ntpdate 172.16.0.9 &>/dev/null;/usr/sbin/hwclock --systohc

 

 

chrony(chronyd:守护进程,chronyc:命令行工具,端口:323/udp,123/udp)与 NTP 的服务端/客户端的使用理念一致。

[ root@VM-0-9-centos pts/2 2021-02-02/28@2 15:45:50 ~ ] 
# cat /etc/chrony.conf
# 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   # 与 NTP 一样
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst

# Record the rate at which the system clock gains/losses time. 根据实际时间计算出本地时间的增减比率保存到一个文件中,重启后给本地系统时间做出补偿。默认开启。--可关闭
driftfile /var/lib/chrony/drift

# Allow the system clock to be stepped in the first three updates  # 根据系统时钟太慢或太快时,根据阈值调整系统时钟。可关闭
# if its offset is larger than 1 second.
makestep 1.0 3

# Enable kernel synchronization of the real-time clock (RTC).  同步到硬件时钟。--开启
rtcsync

# Enable hardware timestamping on all interfaces that support it. --可关闭
#hwtimestamp *

# Increase the minimum number of selectable sources required to adjust --可关闭
# the system clock.
#minsources 2

# Allow NTP client access from local network. 允许那些网络访问(allow)或拒绝(deny)本服务器,--开启
#allow 192.168.0.0/16

# Serve time even if not synchronized to a time source. 当 server 指令中的时间服务器不可用时,依然可以为客户端提供授时。--可开启
#local stratum 10

# Specify file containing keys for NTP authentication. --可关闭
#keyfile /etc/chrony.keys

# Specify directory for log files. --可关闭
logdir /var/log/chrony

# Select which information is logged. --可关闭
#log measurements statistics tracking

 

[ root@VM-0-9-centos pts/2 2021-02-02/28@2 15:45:52 ~ ] 
# systemctl enable chronyd.service 

[ root@VM-0-9-centos pts/2 2021-02-02/28@2 15:45:52 ~ ] 
# systemctl start chronyd.service 

 

chronyc 命令

[COMMAND]    意义
help                命令可以查看更多chronyc的交互命令
accheck             检查是否对特定主机可访问当前服务器
activity            显示有多少NTP源在线/离线
sources [-v]        显示当前时间源的同步信息   -- 常用
sourcestats [-v]    显示当前时间源的同步统计信息
add server          手动添加一台新的NTP服务器
clients             报告已访问本服务器的客户端列表
delete              手动移除NTP服务器或对等服务器
settime             手动设置守护进程时间
sracking            显示系统时间信息

 

posted on 2021-02-02 16:20  3L·BoNuo·Lotus  阅读(230)  评论(0编辑  收藏  举报