@[TOC](第二十章 网络时间服务和chrony)
实验一:时间同步
实验目的
系统时间、硬件时间的设定和修改;ntp、chrony实现时间同步。
前提准备
可用的centos6、7系统。 centos6 :192.168.37.6 centos7 :192.168.37.7
实验步骤
- 时间同步 【例1】date命令查看系统时间
[root@6 ~]# date
Tue Jan 29 14:32:00 CST 2019
【例2】查看硬件时钟
[root@6 ~]# clock
Tue 29 Jan 2019 02:49:13 PM CST -0.334741 seconds
【例3】修改系统时间为2018年
[root@6 ~]# date -s '-1 year'
Tue Jan 29 14:32:55 CST 2019
[root@6 ~]# date
Tue Jan 29 14:33:00 CST 2019
但此时硬件时间依然没修改,
[root@6 ~]# clock
若要修改硬件时间则:
[root@6 ~]# clock -w
- ntp软件实现时间同步 centos6上默认安装了ntp软件包(包括客户端和服务器端),但是ntp同步需要⼀定时间才能完全同步时间的,⽽chrony 同步时间⽐ntp快。centos7默认安装了chronyd服务。
ntp服务器可以参看www.ntp.org.cn/pool
【例4】在 centos6上查看ntp软件、修改配置⽂件
[root@6 ~]# rpm -ql ntp
...
/etc/ntp.conf #定义同步规则
/etc/rc.d/init.d/ntpd #服务名
/usr/sbin/ntpd #ntp服务器主程序
...
[root@6 ~]# vim /etc/ntp.conf
...
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
(以上都注释掉)
server ntp.aliyun.com iburst #以下为阿里的ntp服务器
...
[root@6 ~]# service ntpd start #启动服务
[root@6 ~]# chkconfig ntpd on #开机启动
[root@6 ~]# ss -unl #监听了udp的123端口
[root@6 ~]# ntpq -p #查看同步状态
remote refid st t when poll reach delay offset jitter
==============================================================================
203.107.6.88 10.137.55.181 2 u 2 64 1 15.127 0.054 0.000
说明:ntp.aliyun.com是时间服务器的ip地址;iburst是加速同步时间。如果时间相差较⼤,是不能完成实时同步的。
【例5】ntpdate命令,⽴即同步
ntp时间相差较⼤,是不能完成实时同步,那么可以⽤ntpdate命令来完成、关闭ntpd服务
[root@6 ~]$ date -s '-1 year'
[root@6 ~]# ntpdate ntp.aliyun.com #同步阿里ntp、发现没同步
[root@6 ~]# service ntpd stop #必须关闭ntp
[root@6 ~]# ntpdate ntp.aliyun.com #使用阿里的ntp服务同步时间
【例6】根据上例,centos6开启了ntpd服务,也可当做时间服务器被它⼈使⽤
在centos7上,模拟错误时间,方便演示:
[root@7 ~]# date -s '-1 year'
[root@7 ~]# systemctl stop ntpd
#使用ntpdate时不能够开启ntpd服务、如果同步不了执行上面命令
[root@7 ~]# ntpdate 192.168.37.6 #不能同步centos6上设置的ntp服务器、是因为CentOS6配置文件不同意
【例7】ntp充当服务器
[root@6 ~]# vim /etc/ntp.conf
...
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
(以上都注释掉)
server ntp.aliyun.com iburst
restrict default nomodify notrap nopeer noquery 改为restrict default nomodify
...
[root@6 ~]# service ntpd start 启动服务
[root@6 ~]# chkconfig ntpd on 开机启动
[root@6 ~]# ss -unl 监听了udp的123端口;
#选择其中一个
方法1:
[root@7 ~]# ntpdate 192.168.37.6 #同步centos6上设置的ntp服务器
方法2:
[root@7 ~]# vim /etc/ntp.conf
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server 192.168.37.6 iburst
(注释掉并添加)
[root@7 ~]# systemctl start ntpd
[root@7 ~]# systemctl enable ntpd
实验二:chkrony时间同步
- 在centos7上chrony软件实现时间同步 chrony同ntp,也可配置成客户端和服务器端两种。 【例7】查看chrony
[root@7 ~]# rpm -qi chrony
说明: /usr/bin/chronyc是客户端程序; /usr/sbin/chronyd是服务器端查询; /usr/lib/systemd/system/chronyd.service是服务控制⽂件。
【例8】配置chrony为客户端,配置3个时间服务器地址
[root@7 ~]# vim /etc/chrony.conf
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
(注释掉上面)
server 114.67.237.130 iburst #设置服务器的IP地址,与服务器的IP地址进行时间同步
server ntp.aliyun.com iburst #设置其他几个服务器的IP地址,防止其他服务器宕机,无法实现时间同步
server ntp1.aliyun.com iburst
[root@7 ~]# systemctl start chronyd #启动服务
[root@7 ~]# systemctl enable chronyd #开机启动
[root@7 ~]# ss -untl #监听udp的323端口。
[root@7 ~]# date #时间会立即同步完成。
【例9】chronyc客户端⼯具
[root@7 ~]# chronyc #进入交互式命令行
chronyc> help #查看帮助子命令
chronyc> sources -v #同步时钟
210 Number of sources = 3
.-- 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
===============================================================================
^- 114.67.237.130 2 6 17 27 +1147us[+1147us] +/- 453ms
^* 203.107.6.88 2 6 377 16 -1335us[-2134us] +/- 20ms
^- 120.25.115.20 2 6 373 76 -485us[-1074us] +/- 23ms
chronyc> quit 退出
非交互式命令:
[root@7 ~]# chronyc sources -v
210 Number of sources = 3
.-- 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
===============================================================================
^- 114.67.237.130 2 6 37 25 +2778us[+2778us] +/- 455ms
^* 203.107.6.88 2 6 377 29 -57us[ -130us] +/- 18ms
^- 120.25.115.20 2 6 333 24 +139us[ +139us] +/- 23ms
查看开机是否启动:
[root@7 ~]# systemctl status chronyd #查看状态
[root@7 ~]# systemctl enable chronyd #设置开机自动启动:
在centos6上安装chrony软件包:
[root@6 ~]# yum -y install chrony
[root@6 ~]# service ntpd stop
#查看ntpd是否开机启动
[root@6 ~]# chkconfig --list ntpd
ntpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
#把centos7当中时间服务器:
[root@6 ~]# vim /etc/chrony.conf
#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
(注释掉上面信息)再添加下面一行
server 192.168.37.7 iburst
[root@6 ~]# service chronyd start
[root@6 ~]# 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
===============================================================================
^? 192.168.37.7 0 8 0 10y +0ns[ +0ns] +/- 0ns
在centos7上配置,充当时间同步服务器:
[root@7 ~]# vim /etc/chrony.conf
其中:
server 114.67.237.130 iburst
allow 0.0.0.0/0 #允许所有网段主机访问
local stratum 10
[root@7 ~]# systemctl restart chronyd
说明:
allow 192.168.37.0/24 #表示哪些主机可以向该主机同步时间;
allow 0.0.0.0/0表示允许所有主机向该主机同步时间;
local stratum 10表示当互联网不能访问时间服务器时,仍然使用本机时间提供时间服务。
再回到centos6,同步:
[root@6 ~]# chronyc sources -v #同步时间较慢。
显示:^* 192.168.37.7 即为成功。
总结:实现了使⽤⼀台主机向互联⽹上的时间服务器同步时间,在局域⽹内部,由其他主机向该主机同步时间即可。
在初始化脚本里,修改chrony配置文件。
sed 's/^server.*/#&/' /etc/chrony.conf
echo server 114.67.237.130 iburst >> /etc/chrony.conf
- timedatectl命令 【例10】列出所有时区
[root@7 ~]# timedatectl list-timezones
【例11】查看当前时区状态
[root@7 ~]# timedatectl status
【例12】修改时区
[root@7 ~]# timedatectl set-timezone Asia/Shanghai
[root@7 ~]# date
【例13】查看有效的配置⽂件
[root@7 ~]# grep -Ev "^(#.*|)$" /etc/chrony.conf
或:
[root@7 ~]# grep -Ev "^#|^$" /etc/chrony.conf