安裝及配置
#yum install chrony --RHEL7默認已安裝chrony,而沒有安裝ntpd.
#systemctl status chronyd --查看chronyd服務狀態
#systemctl enable chronyd --開機啟動
#vi /etc/chrony.conf --修改配置文件
#server 0.rhel.pool.ntp.org iburst --注釋這4行,由於是內網環境,所以無法跟外部時間服務器進行時間同步。
#server 1.rhel.pool.ntp.org iburst
#server 2.rhel.pool.ntp.org iburst
#server 3.rhel.pool.ntp.org iburst
server 192.168.100.1 iburst --添加這一行,表示與本機同步時間
local stratum 10 --該行取消注釋
# Allow NTP client access from local network.
allow 192.168.100.0/24 --允許哪些服務器到這台服務器來同步時間
#systemctl restart chronyd --修改完配置文件后,重啟chronyd服務
[root@rhel1 ~]# netstat -antulp | grep chronyd
udp 0 0 0.0.0.0:123 0.0.0.0:* 705/chronyd
udp 0 0 127.0.0.1:323 0.0.0.0:* 705/chronyd
udp6 0 0 ::1:323 :::* 705/chronyd
顯示系統的當前時間和日期
timedatectl
# timedatectl status
# 兩條命令效果等同
[root@rhel1 ~]# timedatectl
Local time: Sat 2017-10-07 10:55:22 CST
Universal time: Sat 2017-10-07 02:55:22 UTC
RTC time: Sat 2017-10-07 02:55:22
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
注意NTP synchronized值,只有時間服務器自己同步完成時間之后,才能為其它服務器提供時間同步服務。
2.設置日期與時間
timedatectl set-time "YYYY-MM-DD HH:MM:SS"
timedatectl set-time "YYYY-MM-DD"
timedatectl set-time "HH:MM:SS"
3.查看所有可用的時區
timedatectl list-timezones
# 亞洲
timedatectl list-timezones | grep -E "Asia/S.*"
4.設置時區
timedatectl set-timezone Asia/Shanghai
5.設置硬件時間
# 硬件時間默認為UTC
timedatectl set-local-rtc 1
# hwclock --systohc --localtime
# 兩條命令效果等同
6.啟用NTP時間同步
timedatectl set-ntp yes
# yes或no; 1或0也可以
問題:不明白這里啟用ntp時間同步有啥意義。難道是說可以在時間服務器上都安裝chronyd和ntpd服務來同時提供時間服務嗎????如果設置為yes,手動設置時間則報錯。看本文結尾。
7、查看時間源信息
[root@rhel1 ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* rhel1.rusky.com 10 7 377 22m -3603ns[ -19us] +/- 32us
[root@rhel1 ~]# 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
|| Reachability register (octal) -. | xxxx = adjusted offset,
|| Log2(Polling interval) --. | | yyyy = measured offset,
|| \ | | zzzz = estimated error.
|| | | \
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* rhel1.rusky.com 10 7 377 22m -3603ns[ -19us] +/- 32us
[root@rhel1 ~]#
客戶端時間同步配置:
-
yum install chrony –y
-
修改配置文件
[root@rhel2 ~]# vi /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.rhel.pool.ntp.org iburst --注釋這4行
#server 1.rhel.pool.ntp.org iburst
#server 2.rhel.pool.ntp.org iburst
#server 3.rhel.pool.ntp.org iburst
server 192.168.100.1 iburst ---添加該行,表示到這台服務器去同步時間。
- 重啟chronyd並設置開機啟動
[root@rhel2 ~]# systemctl restart chronyd.service
[root@rhel2 ~]# systemctl enable chronyd.service
- 查看時間同步狀態
[root@rhel2 ~]# timedatectl
Local time: Sat 2017-10-07 11:15:43 CST
Universal time: Sat 2017-10-07 03:15:43 UTC
RTC time: Sat 2017-10-07 03:02:26
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: yes --為yes表示已同步
RTC in local TZ: no
DST active: n/a
重啟chronyd服務后,需要過幾分鍾才會自動完成同步。
如果需要手動,可使用下面的命令:
#ntpdate 192.168.100.1
如果同步失敗,則可能 是服務端的時間未同步。即服務端NTP synchronized 為no。
============================
手動修改時間時,必須把NTP enabled 設置為no.
[root@rhel1 ~]# date
Sun Oct 8 11:00:51 CST 2017
[root@rhel1 ~]# timedatectl
Local time: Sat 2017-10-07 11:31:45 CST
Universal time: Sat 2017-10-07 03:31:45 UTC
RTC time: Sun 2017-10-08 03:01:57
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
如果把NTP enabled 修改為yes時,表示開啟自動同步時間,此時,是不能手動修改時間的。
如果為No時,表示關閉自動同步時間。
[root@rhel1 ~]# timedatectl set-ntp yes
[root@rhel1 ~]# timedatectl set-time "2017-10-08 11:00:50"
Failed to set time: Automatic time synchronization is enabled
[root@rhel1 ~]# timedatectl set-ntp no
[root@rhel1 ~]# timedatectl set-time "2017-10-08 11:00:50"
[root@rhel1 ~]# date
Sun Oct 8 11:00:51 CST 2017