Centos7配置时间服务器,一个服务端,多个客户端,客户端去同步服务端
服务端:
1、先修改时区,否则即使配置完ntp时间也不对,修改时区参照:https://www.cnblogs.com/effortsing/p/10070673.html
2、安装ntp服务
yum install ntp ntpdate -y
3、查找当前地区,最适合的时间服务器
步骤一:打开网站:
pool.ntp.org: NTP Servers in Asia, asia.pool.ntp.org
步骤二:复制自动推荐的最合适的同步服务器
server 0.asia.pool.ntp.org server 1.asia.pool.ntp.org server 2.asia.pool.ntp.org server 3.asia.pool.ntp.org
4、编辑 /etc/ntp.conf 复制上述查到的时间服务器,保存
cat >> /etc/ntp.conf << EOF
server 0.asia.pool.ntp.org
server 1.asia.pool.ntp.org
server 2.asia.pool.ntp.org
server 3.asia.pool.ntp.org
EOF
5、启动ntp服务
systemctl start ntpd
systemctl enable ntpd.service #设置开机启动服务
6、同步远程时间服务(下面两个是第2步查询到的前两个ntp)
[root@www ~]# ntpdate -q 2.asia.pool.ntp.org 3.asia.pool.ntp.org
server 211.233.84.186, stratum 0, offset 0.000000, delay 0.00000
server 46.19.96.19, stratum 0, offset 0.000000, delay 0.00000
server 133.243.238.163, stratum 0, offset 0.000000, delay 0.00000
server 188.93.95.200, stratum 0, offset 0.000000, delay 0.00000
server 203.217.204.135, stratum 0, offset 0.000000, delay 0.00000
server 192.248.1.162, stratum 0, offset 0.000000, delay 0.00000
server 183.177.72.202, stratum 0, offset 0.000000, delay 0.00000
server 118.189.138.5, stratum 1, offset 0.037402, delay 0.11124
5 Dec 00:44:46 ntpdate[4795]: adjust time server 118.189.138.5 offset 0.037402 sec
7、验证服务:ntpq -p
[root@www ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
t2.time.sg3.yah .INIT. 16 u – 64 0 0.000 0.000 0.000
ntp.hkg10.hk.le .INIT. 16 u – 64 0 0.000 0.000 0.000
203.95.213.129 .INIT. 16 u – 64 0 0.000 0.000 0.000
203.217.204.135 .INIT. 16 u – 64 0 0.000 0.000 0.000
查看当前时间:date -R
打开百度,搜索“时间”,对比时间是否一致
8、建立Job,每天定时自动同步远程服务
crontab -e
00 01 * * * root /usr/sbin/ntpdate -q 0.asia.pool.ntp.org 1.asia.pool.ntp.org
客户端:
1、先修改时区,否则即使配置完ntp时间也不对,修改时区参照:https://www.cnblogs.com/effortsing/p/10070673.html
2、安装ntp服务
yum install ntp ntpdate -y
3、启动ntp服务
cd /usr/lib/systemd/system
systemctl start ntpd #启动
systemctl enable ntpd.service #设置开机启动服务
4、修改硬件时间,保持和软件NTP时间同步
hwclock -w
5、测试服务器同步
ntpdate -d -b 192.168.1.204
6、如果正常,建立Job,自动运行,每天同步
00 01 * * * root /usr/sbin/ntpdate -q 192.168.1.204
报错解决:
客户端同步服务端报错如下:
9 Dec 22:42:18 ntpdate[10293]: no server suitable for synchronization found
解决:
1、添加解析
配置192.168.1.206的 hosts 添加服务端的ip地址和域名即可解决
sed -i ‘$a\192.168.1.204 www.test.com’ /etc/hosts
2、重启客户端主机、重启客户端 ntp
3、重启服务端主机、重启服务端 ntp
4、再次同步成功
[root@www etc]# ntpdate -d -b 192.168.1.204
9 Dec 22:50:33 ntpdate[10835]: ntpdate 4.2.6p5@1.2349-o Fri Apr 13 12:52:28 UTC 2018 (1)
Looking for host 192.168.1.204 and service ntp
host found : www.test.com
transmit(192.168.1.204)
receive(192.168.1.204)
transmit(192.168.1.204)
receive(192.168.1.204)
transmit(192.168.1.204)
receive(192.168.1.204)
transmit(192.168.1.204)
receive(192.168.1.204)
server 192.168.1.204, port 123
stratum 2, precision -25, leap 00, trust 000
refid [192.168.1.204], delay 0.02643, dispersion 0.00026
transmitted 4, in filter 4
reference time: dfb8603a.eb00f583 Sun, Dec 9 2018 22:47:06.917
originate timestamp: dfb8610f.7f3f2a5e Sun, Dec 9 2018 22:50:39.497
transmit timestamp: dfb8610f.802d9b91 Sun, Dec 9 2018 22:50:39.500
filter delay: 0.03288 0.02699 0.02654 0.02643
0.00000 0.00000 0.00000 0.00000
filter offset: -0.00560 -0.00401 -0.00404 -0.00417
0.000000 0.000000 0.000000 0.000000
delay 0.02643, dispersion 0.00026
offset -0.004177
9 Dec 22:50:39 ntpdate[10835]: step time server 192.168.1.204 offset -0.004177 sec