CentOS7 的時間同步方法
阿新 • • 發佈:2018-11-11
centos7 時間同步和時區設定
首先centos7的時間同步是使用chrony這個工具(
[[email protected] ~]# systemctl status chrony
● chrony.service
Loaded: not-found (Reason: No such file or directory)
Active: inactive (dead)
1.安裝chrony
[[email protected] ~]# yum -y install chrony Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile
2.啟動chrony
[[email protected] ~]# systemctl start chronyd [[email protected] ~]# systemctl status chronyd ● chronyd.service - NTP client/server Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2016-10-24 17:52:52 CST; 3h 35min ago Main PID: 807 (chronyd) CGroup: /system.slice/chronyd.service └─807 /usr/sbin/chronyd Oct 24 17:52:44 localhost.localdomain systemd[1]: Starting NTP client/server... Oct 24 17:52:44 localhost.localdomain chronyd[807]: chronyd version 2.1.1 sta... Oct 24 17:52:45 localhost.localdomain chronyd[807]: Frequency -10.892 +/- 15.... Oct 24 17:52:52 localhost.localdomain systemd[1]: Started NTP client/server. Oct 24 17:53:49 localhost.localdomain chronyd[807]: Forward time jump detected! Oct 24 17:53:55 localhost.localdomain chronyd[807]: Selected source 202.118.1.81 Oct 24 17:53:55 localhost.localdomain chronyd[807]: System clock wrong by -45... Oct 24 17:53:09 localhost.localdomain chronyd[807]: System clock was stepped ... Oct 24 21:27:55 localhost.localdomain systemd[1]: Started NTP client/server. Hint: Some lines were ellipsized, use -l to show in full.
3。用本機電腦同步另一臺主機。做為伺服器端需要配置 /etc/chrony.conf
等等。。先看一下本機的時間(localhost)
在設定一下另一臺主機的時間(recehost)(錯誤的時間)[[email protected] ~]# timedatectl Local time: Mon 2016-10-24 21:30:33 CST Universal time: Mon 2016-10-24 13:30:33 UTC RTC time: Mon 2016-10-24 13:30:32 Time zone: Asia/Shanghai (CST, +0800) NTP enabled: yes NTP synchronized: yes RTC in local TZ: no DST active: n/a
[[email protected] ~]# timedatectl set-time "2018-11-11 11:11:11"
[[email protected] ~]# timedatectl
Local time: Sun 2018-11-11 11:11:17 CST
Universal time: Sun 2018-11-11 03:11:17 UTC
RTC time: Sun 2018-11-11 11:11:18
Timezone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: yes
DST active: n/a
好了,開始配置檔案了(注意:現在是伺服器端的修改)
22 allow 192.168.1.0/24
23
24 # Listen for commands only on localhost.
25 bindcmdaddress 127.0.0.1
26 bindcmdaddress ::1
27
28 # Serve time even if not synchronized to any NTP server.
29 local stratum 10
第22行設定為本網段
第29行的註釋取消
重啟時間同步服務,再關閉防火牆
[[email protected] ~]# vim /etc/chrony.conf
[[email protected] ~]# systemctl restart chronyd.service
[[email protected] ~]# systemctl stop firewalld.service
這下配置客戶端的配置檔案是同一個檔案(/etc/chrony.conf)
刪掉哪些沒用的server xxxxxxxxxx iburst
1 # Use public servers from the pool.ntp.org project.
2 # Please consider joining the pool (http://www.pool.ntp.org/j oin.html).
3 server 192.168.1.192 iburst
4 # Ignore stratum in source selection.
同樣需要重啟同步服務,關閉防火牆
[[email protected] ~]# systemctl restart chronyd.service
[[email protected] ~]# systemctl stop firewalld.service
使用下面的命令就可以檢視同步情況
[[email protected] ~]# chronyc sources -v
210 Number of sources = 1
.-- Source mode '^' = server, '=' = peer, '#' = local clock.
/ .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| / '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
|| .- xxxx [ yyyy ] +/- zzzz
|| / xxxx = adjusted offset,
|| Log2(Polling interval) -. | yyyy = measured offset,
|| \ | zzzz = estimated error.
|| | |
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 192.168.1.192 3 6 37 29 -1430ns[+7000ns] +/- 15ms
下來再看時間
[[email protected] ~]# timedatectl
Local time: Mon 2016-10-24 21:47:23 CST
Universal time: Mon 2016-10-24 13:47:23 UTC
RTC time: Mon 2016-10-24 21:47:24
Timezone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: yes
DST active: n/a
timedatectl status 顯示當前時間資訊
set-time 設定當前時間
set-timezone 設定當前時區
set-local-rtc 設定bios時間