如何同步 Linux 集群系统时间

查看系统当前时间和时区
# 查看时间
[root@hdp-01 flink-1.12.1]# date
2021年 01月 21日 星期四 05:16:10 Local time zone must be set--see zic manual page
# 设置时区
[root@hdp-01 Asia]# TZ='Asia/Shanghai'; export TZ

# 显示时区
[root@hdp-01 flink-1.12.1]# date -R
Thu, 21 Jan 2021 05:16:16 +0000

# 按照指定格式显示日期时间
[root@hdp-01 flink-1.12.1]#  date '+%Y-%m-%d %H:%M:%S'
2021-01-21 05:16:35

# 查看时区
[root@hdp-01 flink-1.12.1]# cat /etc/sysconfig/clock
ZONE="Asia/Shanghai"

修改系统当前时区

要做到服务器集群的时间同步,集群中各台机器的时区必须相同的,我们在国内就使用中国时区,如果你的机器的时区不是"Asia/Shanghai",需要修改时区

CentOS 中时区是以文件形式存在,当前正在使用的时区文件位于
/etc/localtime,其他时区文件则位于/usr/share/zoneinfo下,中国时区的文件全路径是/usr/share/zoneinfo/Asia/Shanghai

  • 如果系统中有/usr/share/zoneinfo/Asia/Shanghai时区文件

    [root@hdp-01 flink-1.12.1]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
    
  • 如果系统中没有 /usr/share/zoneinfo/Asia/Shanghai 时区文件

    需要使用 tzselect 命令去生成时区文件,生成好的时区文件就在 /usr/share/zoneinfo 目录下,具体步骤去下:

    [root@hadoop01 ~]# tzselect
    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 # 选亚洲
    Please select a country.
     1) Afghanistan       18) Israel            35) Palestine
     2) Armenia       19) Japan         36) Philippines
     3) Azerbaijan        20) Jordan            37) Qatar
     4) Bahrain       21) Kazakhstan        38) Russia
     5) Bangladesh        22) Korea (North)     39) Saudi Arabia
     6) Bhutan        23) Korea (South)     40) Singapore
     7) Brunei        24) Kuwait            41) Sri Lanka
     8) Cambodia          25) Kyrgyzstan        42) Syria
     9) China         26) Laos          43) Taiwan
    10) Cyprus        27) Lebanon           44) Tajikistan
    11) East Timor        28) Macau         45) Thailand
    12) Georgia       29) Malaysia          46) Turkmenistan
    13) Hong Kong         30) Mongolia          47) United Arab Emirates
    14) India         31) Myanmar (Burma)       48) Uzbekistan
    15) Indonesia         32) Nepal         49) Vietnam
    16) Iran          33) Oman          50) Yemen
    17) Iraq          34) Pakistan
    #? 9 # 选中国
    Please select one of the following time zone regions.
    1) Beijing Time
    2) Xinjiang Time
    #? 1 # 只能选择北京时间和新疆时间,但即使选择了北京时间,最后生成的也是上海时区的文件
    
    The following information has been given:
    
        China
        Beijing Time
    
    Therefore TZ='Asia/Shanghai' will be used.
    Local time is now:  Thu Nov 23 04:44:37 CST 2017.
    Universal Time is now:  Wed Nov 22 20:44:37 UTC 2017.
    Is the above information OK?
    1) Yes
    2) No
    #? 1 # 确认
    
    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 # 很无奈,不是北京,但问题不大
    

    然后执行

    [root@hdp-01 flink-1.12.1]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
    

    验证

    [root@hdp-01 Asia]# cat /etc/sysconfig/clock
    
集群时间同步方法一:手动修改

让集群所有的服务器的时间同步,就用远程连接工具连接所有服务器,然后在所有的服务器中同时执行date -s命令设置时间,然后在所有的服务器中执行hwclock -w命令即可

[root@hdp-01 Asia]# date -s 12/25/2016
[root@hdp-01 Asia]# date -s 19:57:30
[root@hdp-01 Asia]# date -s "2017-11-22 20:50:30"

# 手动修改后,使用以下命令,把系统时间写入主板,这样,即使服务器关机或断电,时间也会更新
[root@hdp-01 Asia]# hwclock -w
集群时间同步方法二:自动同步网络时间(需要网络)
通过外网同步时间
[root@node01 ~]# ntpdate time.nist.gov
# 或者
[root@node01 ~]# ntpdate 192.43.244.18
  • 美国标准技术院时间服务器:time.nist.gov(192.43.244.18)
  • 上海交通大学网络中心NTP服务器地址:ntp.sjtu.edu.cn(202.120.2.101)
  • 中国国家授时中心服务器地址:cn.pool.ntp.org(210.72.145.44)

若以上提供的网络时间服务器不可用,请自行上网寻找可用的网络时间服务器

设置自动执行任务,定时更新时间
[root@node01 ~]# crontab -e
*/10 * * * * ntpdate 202.120.2.101

上面的配置表示,每隔十分钟从 202.120.2.101该时间服务器同步一次时间。

集群时间同步方法三:局域网内选一台服务器作为时间服务器

其他服务器从局域网内的时间服务器更新时间,同时局域网内的时间服务器向外网时间服务器同步时间

posted @ 2021-11-02 16:09  Binge-和时间做朋友  阅读(520)  评论(0编辑  收藏  举报