Timedatactl 时间同步
时间同步只同步时间,不同步时区,时区需要自已配置
UTC时区不变,我们东8区,本地时间为UTC+8小时,就是现在本地的时间
查询语句:
列出了已知时区的数据库:timedatectl list-timezones
更改当前时区系统设置:timedatectl set-timezone Asia/Shanghai
更改当前时间和日期 timedatectl set-time 09:00:00
启用或禁用NTP同步:timedatectl set-ntp true|false
[root@desktop0 ~]# timedatectl set-time"2018-01-20 14:05:00"
[root@desktop0 ~]# timedatectl
Local time: Sat 2018-01-20 14:05:02 CST
Universal time: Sat 2018-01-20 06:05:02 UTC
RTC time: Sat 2018-01-20 06:05:02
Timezone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: no
RTCin local TZ: no
DST active: n/a
Chrony即可以配置成服务端也可以配置成客户端:
将chrony配置成client客户端同步classroom的服务端
[root@desktop0 ~]# yum install chrony
Loaded plugins: langpacks
Package chrony-1.29.1-1.el7.x86_64 alreadyinstalled and latest version
Nothing to do
[root@desktop0 ~]# systemctl start chronyd
[root@desktop0 ~]# systemctl enable chronyd
[root@desktop0 ~]# systemctl restart chronyd
修改配置文件
Vim /etc/chrony.conf
server classroom.example.com iburst
[root@desktop0 ~]# systemctl restartchronyd
查看:
[root@desktop0~]# timedatectl
Local time: Sat 2018-01-20 19:59:10 CST
Universal time: Sat 2018-01-20 11:59:10 UTC
RTC time: Sat 2018-01-20 11:59:10
Timezone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTPsynchronized: no #还没有同步
RTCin local TZ: no
DST active: n/a
详情查看是否同步:
[root@desktop0~]# 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
===============================================================================
^*classroom.example.com 8 6 17 59 -15ms[ -15ms] +/- 41ms
[root@desktop0 ~]# timedatectl
Local time: Sat 2018-01-20 20:00:52 CST #本地时间
Universal time: Sat 2018-01-20 12:00:52 UTC #全世界标准时间
RTC time: Sat 2018-01-20 12:00:52 #系统时间
Timezone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTPsynchronized: yes #已同步
RTCin local TZ: no
DST active: n/a
将chrony配置成服务端
Vim /etc/chronyd.conf
# Allow NTP client access from localnetwork.
#allow 192.168/16
allow 172.25.0.0/24
# Listen for commands only on localhost.
bindcmdaddress 127.0.0.1
bindcmdaddress ::1
bindcmdaddress 0.0.0.0
# Serve time even if not synchronized toany NTP server.
#local stratum 10
#systemctl restart chronyd
[root@server0 ~]# firewall-cmd --permanent--add-service=ntp
[root@server0 ~]# ss -antulp | grep 123
tcp UNCONN 0 0 *:123 *:* users:(("chronyd",23553,1))
tcp UNCONN 0 0 :::123 :::* users:(("chronyd",23553,2))
配置要连接的客户端:
Vim /etc/chronyd.conf
server server0.example.com iburst
[root@desktop0 ~]# 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
===============================================================================
^*server0.example.com 9 6 7 31 -18us[-28811s] +/- 2972us
[root@desktop0 ~]# date
Sat Jan 20 22:15:00 CST 2018
[root@desktop0 ~]# timedatectl
Local time: Sat 2018-01-20 22:16:30 CST
Universal time: Sat 2018-01-20 14:16:30 UTC
RTC time: Sat 2018-01-20 06:16:19
Timezone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes #已同步
RTCin local TZ: no
DST active: n/a