1. 程式人生 > 其它 >解決 CentOS 7 中時間快了 8 小時的問題

解決 CentOS 7 中時間快了 8 小時的問題

技術標籤:linux

解決 CentOS 7 中時間快了 8 小時的問題

1 檢視系統時間

1.2 方法一

$ date
  • 1

輸出如下:

[[email protected] ~]$ date
Tue Apr  9 19:44:57 CST 2019
  • 1
  • 2

1.2 方法二

$ timedatectl
  • 1

$ timedatectl status
  • 1

輸出如下:

[[email protected] ~]$ timedatectl
      Local time: Tue 2019-04-09 19:47:52 CST
  Universal time: Tue 2019-04-09 11:47:52 UTC
        RTC time: Tue 2019-04-09 11:47:52
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: n/a
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2 同步遠端 NTP(Network Time Protocol)伺服器

2.1 檢視是否啟用了 NTP 服務

$ timedatectl | grep -i ntp
  • 1

輸出如下:

[[email protected] ~]$ timedatectl | grep -i ntp
     NTP enabled: n/a
NTP synchronized: no
  • 1
  • 2
  • 3

n/a代表沒有,故需要先安裝並啟動 NTP 服務。

3 安裝並啟動 NTP 服務

3.1 安裝 NTP 伺服器包

$ sudo yum install -y ntp
  • 1

3.2 從 NTP 公共池時間伺服器選擇所需的物理伺服器

NTP Servers in China, cn.pool.ntp.org

NTP 池伺服器

3.3 編輯 NTP 配置檔案

$ sudo vi /etc/ntp.conf
  • 1

註釋掉預設的 NTP 公共池時間伺服器,並使用中國的 NTP 池伺服器。如下圖:

配置 NTP 伺服器

3.4 啟動 NTP 服務

檢視 NTP 服務狀態(是否已啟動):

$ sudo systemctl status ntpd
  • 1

啟動 NTP 服務:

$ sudo systemctl start ntpd
  • 1

加入系統自啟動服務:

$ sudo systemctl enable ntpd
  • 1

參考資料:

[[email protected] ~]# timedatectl
Local time: 五 2021-01-08 17:04:23 CST
Universal time: 五 2021-01-08 09:04:23 UTC
RTC time: 五 2021-01-08 09:04:22
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
[[email protected] ~]# /usr/sbin/ntpdate 192.168.10.192
8 Jan 09:08:34 ntpdate[19271]: step time server 192.168.10.192 offset -28646.683784 sec
[[email protected] ~]# timedatectl
Local time: 五 2021-01-08 09:08:36 CST
Universal time: 五 2021-01-08 01:08:36 UTC
RTC time: 五 2021-01-08 09:06:02
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
[[email protected] ~]# /sbin/clock -w
[[email protected] ~]# timedatectl
Local time: 五 2021-01-08 09:08:55 CST
Universal time: 五 2021-01-08 01:08:55 UTC
RTC time: 五 2021-01-08 01:08:55
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
[[email protected] ~]# date
2021年 01月 08日 星期五 09:09:09 CST
[[email protected] ~]# crontab -e
crontab: installing new crontab
[[email protected] ~]# date
2021年 01月 08日 星期五 09:10:30 CST
[[email protected] ~]# crontab -l
*/2 * * * * /usr/sbin/ntpdate -u 192.168.10.192 &>/dev/null
10 12 * * * /home/oracle/rman/delarch.sh
[[email protected] ~]# date
2021年 01月 08日 星期五 09:10:36 CST
[[email protected] ~]# timedatectl
Local time: 五 2021-01-08 09:10:40 CST
Universal time: 五 2021-01-08 01:10:40 UTC
RTC time: 五 2021-01-08 01:10:40
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
[[email protected] ~]# /usr/sbin/ntpdate -u 192.168.10.192 &>/dev/null
[[email protected] ~]# timedatectl
Local time: 五 2021-01-08 09:10:52 CST
Universal time: 五 2021-01-08 01:10:52 UTC
RTC time: 五 2021-01-08 01:10:53
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
[[email protected] ~]# cat /etc/ntp
ntp/ ntp.conf
[[email protected] ~]# cat /etc/ntp.conf
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

driftfile /var/lib/ntp/drift

# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default nomodify notrap nopeer noquery

# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict ::1

# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 0.rhel.pool.ntp.org iburst
server 1.rhel.pool.ntp.org iburst
server 2.rhel.pool.ntp.org iburst
server 3.rhel.pool.ntp.org iburst

#broadcast 192.168.1.255 autokey # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 autokey # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 autokey # manycast client

# Enable public key cryptography.
#crypto

includefile /etc/ntp/crypto/pw

# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys

# Specify the key identifiers which are trusted.
#trustedkey 4 8 42

# Specify the key identifier to use with the ntpdc utility.
#requestkey 8

# Specify the key identifier to use with the ntpq utility.
#controlkey 8

# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats

# Disable the monitoring facility to prevent amplification attacks using ntpdc
# monlist command when default restrict does not include the noquery flag. See
# CVE-2013-5211 for more details.
# Note: Monitoring will not be disabled with the limited restriction flag.
disable monitor
[[email protected] ~]# mv /etc/ntp.conf /etc/ntp.confbak
[[email protected] ~]# systemctl stop ntpd
[[email protected] ~]# timedatectl
Local time: 五 2021-01-08 09:12:16 CST
Universal time: 五 2021-01-08 01:12:16 UTC
RTC time: 五 2021-01-08 01:12:17
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
[[email protected] ~]# systemctl disable ntpd
[[email protected] ~]# timedatectl
Local time: 五 2021-01-08 09:12:27 CST
Universal time: 五 2021-01-08 01:12:27 UTC
RTC time: 五 2021-01-08 01:12:28
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
[[email protected] ~]# timedatectl
Local time: 五 2021-01-08 09:13:57 CST
Universal time: 五 2021-01-08 01:13:57 UTC
RTC time: 五 2021-01-08 01:13:58
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
[[email protected] ~]#