linux tools

ntp时间同步

2019-07-01  本文已影响26人  酷酷的伟

ntp时间同步

介绍
如果一个集群中,时间相差很大,那么会出现很多诡异的问题,我们也不想在一个无法解决的问题上浪费几天时间吧!总之,设置服务器之间时间同步,为了避免很多问题的发生!

image

1.准备工作

远程为所有服务器安装ntp服务

yum install ntp

systemctl start ntpd.service
systemctl enable ntpd.service

设置同步为中国时区

timedatectl set-timezone Asia/Shanghai

2. 设置web01为同步外网时间服务器

需要设置允许内网网段可以连接它,将它作为内网的时间同步服务器,所以需要一个简单配置。

vim /etc/ntp.conf

restrict 172.16.1.0 mask 255.255.255.0 #添加此行
表示允许此网段来同步此服务器

server 127.127.1.0
server设置127.127.1.0为其自身

image

重启ntpd服务,用ntpstat来检查效果
(需要几秒钟就可以看到效果)

[root@web01 ~]# systemctl restart ntpd
[root@web01 ~]# ntpstat 
synchronised to local net at stratum 6 
   time correct to within 7948 ms
   polling server every 64 s

3.在客户端节点上同步server的时间

ntpdate -u 172.16.1.7
-u ===指定使用无特权的端口发送数据包。 当在一个对特权端口的输入流量进行阻拦的防火墙后是很有益的, 并希望在防火墙之外和主机同步。防火墙是一个系统或者计算机,它控制从外网对专用网的访问。

vim /etc/ntp.conf

image

重启并开机自启动
systemctl start ntpd
systemctl enable ntpd

4.同步server的时间

ntpdate -u 172.16.1.7

[root@web02 ~]# ntpdate -u 172.16.1.7
18 Jun 16:20:06 ntpdate[21259]: adjust time server 172.16.1.7 offset 0.000797 sec

5.书写定时任务

crontab -e
#Ansible: sync time 
*/5 * * * * /sbin/ntpdate -u 172.16.1.7 >/dev/null 2>&1
上一篇下一篇

猜你喜欢

热点阅读