配置参考
参考:https://mp.weixin.qq.com/s/7Lg8pgo1K6x1xEl9oLLf2Q
1, 安装chrony服务
yum install -y chrony
#=================== timeserver1端================
[root@timeserver1 ~]# grep -v ^# /etc/chrony.conf |grep -v ^$
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
allow 10.223.0.0/16
local stratum 10
logdir /var/log/chrony
[root@timeserver1 ~]# ip a |grep 192.168
inet 192.168.56.31/25 brd 192.168.56.127 scope global noprefixroute enp3s0
[root@timeserver1 ~]# systemctl restart chronyd
#=================== client端================
[root@client ~]# sed -i -e '/^pool/server 192.168.56.31 iburst' \
-e 's/^pool/#pool/' /etc/chrony.conf
[root@client ~]# grep -v ^# /etc/chrony.conf |grep -v ^$
server 192.168.56.31 iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
logdir /var/log/chrony
[root@client ~]# systemctl restart chronyd
[root@client ~]# chronyc sources -v
210 Number of sources = 1
.-- Source mode '^' = timeserver1, '=' = 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
===============================================================================
^* timeserver1 10 6 17 5 +19us[ +283us] +/- 127us
2, 安装ntpd服务
集群节点: s101[192.168.56.111] --> s102[192.168.56.112] --> s103[192.168.56.113]
设置: s101为时间服务器,其他节点同步s101的时间
- 1, 所有节点 ,统一 使用 yum 安装ntp
- 2, 修改/etc/ntpd.conf
a, master 节点: ntpd服务
(默认使用外网的时间服务器,可以连外网时不需要修改配置文件)
当集群不能访问外网: 修改 /etc/ntp.conf
sed '/^server/s/^/#/' /etc/ntp.conf -i
echo "
## 配置自己,充当时间同步服务器
server 127.127.1.0 iburst
fudge 127.127.1.0 stratum 10
" >> /etc/ntp.conf
## 修改时区: 中国.上海
ln -sfT /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#修改 ntpd启动参数
echo "SYNC_HWCLOCK=yes" >> /etc/sysconfig/ntpd
#重启ntp, 并设置开机启动:
service ntpd restart
chkconfig ntpd on
b, slave节点: ntpdate服务
修改 /etc/ntp.conf
echo "
## 配置自己,充当时间同步服务器
server 127.127.1.0 iburst
fudge 127.127.1.0 stratum 10
server s101
restrict s101 nomodify notrap noquery
# 允许上层时间服务器主动修改本机时间
" >> /etc/ntp.conf
echo "
# master的ip/主机名
s101 " > /etc/ntp/step-tickers
#清空始终漂移位置
> /var/lib/ntp/drift
## 修改时区: 中国.上海
ln -sfT /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
## 修改 ntpd启动参数
sed -i 's/SYNC_HWCLOCK=no/SYNC_HWCLOCK=yes/' /etc/sysconfig/ntpdate
## 重启ntp, 并设置开机启动:
ntpdate -u s101 #-u 参数:在ntpd 启动时也可以手动同步时间
service ntpdate restart
chkconfig ntpdate on
c, 状态查看
#查看是否和指定的: 服务器同步(要过一段时间才显示如下,否则显示unsynchronised)
[test@s101 ~]$ ntpstat
unsynchronised
time server re-starting
polling server every 8 s
[test@s101 ~]$ ntpstat
synchronised to NTP server (185.255.55.20) at stratum 3
time correct to within 1084 ms
polling server every 64 s
[test@s101 ~]$ ntpq -pn
remote refid st t when poll reach delay offset jitter
==============================================================================
*162.159.200.123 10.20.12.67 3 u 42 256 135 159.502 -78.917 28.187
############ 字段说明 #############
[root@s101 ~]# man ntpq
....
+----------------------+--------------------------------------------------------------------------------------------------------+
| Variable | Description |
+----------------------+--------------------------------------------------------------------------------------------------------+
| [tally] | single-character code indicating current value of the select field of the peer status word |
+----------------------+--------------------------------------------------------------------------------------------------------+
| remote | host name (or IP number) of peer |
+----------------------+--------------------------------------------------------------------------------------------------------+
| refid | association ID or kiss code |
+----------------------+--------------------------------------------------------------------------------------------------------+
| st | stratum |
+----------------------+--------------------------------------------------------------------------------------------------------+
| t | u: unicast, b: broadcast, l: local |
+----------------------+--------------------------------------------------------------------------------------------------------+
| when | sec/min/hr since last received packet |
+----------------------+--------------------------------------------------------------------------------------------------------+
| poll | poll interval (log2 s) |
+----------------------+--------------------------------------------------------------------------------------------------------+
| reach | reach shift register (octal) |
+----------------------+--------------------------------------------------------------------------------------------------------+
| delay | roundtrip delay |
+----------------------+--------------------------------------------------------------------------------------------------------+
| offset | offset |
+----------------------+--------------------------------------------------------------------------------------------------------+
| jitter | jitter |
+----------------------+--------------------------------------------------------------------------------------------------------+
3,使用命令date -s
配置crond定时计划,执行以下脚本
for((i=101;i<=103;i++))
do
#获取当前机器时间: 年月日, 时分秒
datestr=` date '+%y-%m-%d %H:%M:%S' ` # 简写为 '+%F %T'
#同步其他主机时间
echo "ssh s$i date -s--------$datestr $datestr2"
ssh s$i date -s $datestr
ssh s$i hwclock -w #写入bios
echo
done