[TOC]
### **主机规划**
* Chrony服务器:192.168.2.101
* Chrony客户端:192.168.2.102
注意,如果主机上安装并启动了`ntpd`,则`chronyd`会启动失败。只能启动其中的一个。
### **服务器安装Chrony**
执行以下命令安装:
```
$ sudo yum -y install chrony
```
然后修改配置文件`/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 127.0.0.1 iburst
# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift
# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
makestep 1.0 3
# Enable kernel synchronization of the real-time clock (RTC).
rtcsync
# Enable hardware timestamping on all interfaces that support it.
#hwtimestamp *
# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2
# Allow NTP client access from local network.
allow 192.168.2.0/24
# Serve time even if not synchronized to a time source.
local stratum 10
# Specify file containing keys for NTP authentication.
#keyfile /etc/chrony.keys
# Specify directory for log files.
logdir /var/log/chrony
# Select which information is logged.
#log measurements statistics tracking
```
在下面的配置文件中去们需要注意:
> server ntp.aliyun.com iburst
我们把内网的服务器向阿里云同步时间,可以降低网络时延。如果你的这台内网服务器没有办法连接外网,则设置为`127.0.0.1`,后面有一个参数可以让这台内网服务器提供时钟服务。
> allow 192.168.2.0/24
只允许上面这个网段的主机连接服务器。如果不限制IP段,则填写为`0.0.0.0/0`(不过在实验中发现有些环境用`0.0.0.0/0`服务器无法监听`0.0.0.0:123`的udp端口,而有些环境可以,所以如果出现该问题就按照`192.168.2.0/24`类似的设置)
> local stratum 10
该配置的意思是,如果该服务器无法从自己配置的server同步时钟(比如上面配置了阿里云的,但是这台主机无法连接外网),依然提供时钟同步服务。***这个很重要,一定要开启。***
然后执行以下命令启动
```
$ sudo systemctl start chronyd && sudo systemctl enable chronyd
```
如果成功,则可以看到以下的监听状态(如果没有监听123端口,则重启一下chronyd):
```
$ sudo netstat -apn | grep chrony
udp 0 0 0.0.0.0:123 0.0.0.0:* 74420/chronyd
udp 0 0 127.0.0.1:323 0.0.0.0:* 74420/chronyd
udp6 0 0 ::1:323 :::* 74420/chronyd
unix 2 [ ] DGRAM 267585 74420/chronyd /var/run/chrony/chronyd.sock
unix 2 [ ] DGRAM 267578 74420/chronyd
```
在上面的配置中,我们配置了`server ntp.aliyun.com iburst`,其实这个是没有用的,因为我们的主机连不了外网,同步时间永远会失败。所以,我们看到服务器上`NTP synchronized`为`no`,表示时钟并没有同步。
```
$ timedatectl
Local time: Tue 2020-11-03 18:01:37 CST
Universal time: Tue 2020-11-03 10:01:37 UTC
RTC time: Tue 2020-11-03 10:01:36
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
```
### **客户端安装Chrony**
执行下面的命令安装
```
$ sudo yum -y install chrony
```
然后,编辑`/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 192.168.2.101 iburst
# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift
# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
makestep 1.0 3
# Enable kernel synchronization of the real-time clock (RTC).
rtcsync
# Enable hardware timestamping on all interfaces that support it.
#hwtimestamp *
# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2
# Allow NTP client access from local network.
#allow 192.168.0.0/16
# Serve time even if not synchronized to a time source.
#local stratum 10
# Specify file containing keys for NTP authentication.
#keyfile /etc/chrony.keys
# Specify directory for log files.
logdir /var/log/chrony
# Select which information is logged.
#log measurements statistics tracking
```
在上面的配置文件中,我们需要注意:
> server 192.168.2.101 iburst
我们需要把server设置为内网的时钟服务器
> `#allow 192.168.0.0/16`
> `#local stratum 10`
由于我们不需要让该主机提供时钟服务(即作为时钟服务器),所以我们需要把这两行保持注释。
然后执行下面的命令启动:
```
$ sudo systemctl start chronyd && systemctl enable chronyd
```
然后,执行下面的命令查看,发现`NTP synchronized`为yes,说明已经发生了时钟同步
```
$ timedatectl
Local time: Tue 2020-11-03 19:43:37 CST
Universal time: Tue 2020-11-03 11:43:37 UTC
RTC time: Tue 2020-11-03 11:43:37
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
```
我们还可以执行以下的命令查看该主机连接的时钟服务器,其中`^*`中的`*`表示从该服务器进行了时钟同步。如果显示为`^?`则说明没有从该服务器同步时钟。
```
$ chronyc sources
210 Number of sources = 1
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 192.168.2.101 10 7 377 63 +15us[ +18us] +/- 141us
```
### **总结**
从上面的例子可以看出,chronyd即可以作为客户端,从阿里云同步时钟上,也可以作为服务器,向内网中的其他主机提供时钟同步服务。
### **Reference**
* https://my.oschina.net/u/4418268/blog/3352772
* https://cloud.tencent.com/developer/article/1546322
* https://cloud.tencent.com/developer/article/1521908
- 常用命令
- 用户与用户组
- 创建用户与用户组
- 快速脚本
- umask
- Yum源
- 基础Yum源
- Epel源
- 制作Yum源
- 同步Yum源
- 为Yum源配置代理
- 下载RPM及依赖
- 系统与内核
- 获取内核的rpm包
- 升级内核
- Iptables
- 基本语法
- 匹配条件
- 基础匹配条件
- 扩展匹配条件
- Addrtype
- Set
- TCP
- Mark
- Multiport
- 目标
- 基本目标
- 扩展目标
- DNAT
- LOG
- CT
- NOTRACK
- MARK
- IP set
- 连接追踪
- 初识连接追踪
- 连接追踪详解
- NAT
- 思路与参考汇总
- 数据结构
- FAQ
- Keepalived
- 单网卡多VIP
- 安装Keepalived
- 双网卡绑VIP
- 别名VIP和辅助VIP
- LVS
- 安装LVS
- Ipvsadm命令
- 磁盘与分区
- 基础知识
- 创建分区
- 格式化与挂载
- Fstab
- LVM
- LVM扩容
- Swap分区
- Tmpfs
- 网络相关
- 重命名网卡
- resolv.conf
- Tcpdump
- nslookup与dig
- ifcg-xxx
- 主机名
- 软件安装
- NFS
- Squid
- Redsocks
- Shadowsocks
- 时钟同步
- Chrony
- FTP
- 文件句柄
- 简介
- 设置文件句柄
- 其他
- SSH密钥登录
- 进程组-会话-终端
- X11转发
- 环境变量
- 常见问题
- 系统进程数
- 系统调用
- 系统调用FAQ
- 用户程序如何进行系统调用