【运维知识进阶篇】一键部署时间同步服务器

这篇文章给大家介绍下如何将当前服务器变成一个时间同步服务器,可以让其他服务器时间与该服务器时间进行同步。我将操作方法整合进了脚本中,实现一键部署时间同步服务器。

时间服务器部署

[root@Ansible ~]# cat /bash/ntpdate_server.sh 
#!/bin/bash

# 安装 NTP 服务
yum -y install ntp

# 配置ntp.conf文件
cat << EOF >> /etc/ntp.conf
server 127.127.1.0
fudge 127.127.1.0 stratum 8

restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
EOF

# 启动ntp服务并设置开机自启动
systemctl start ntpd.service
systemctl enable ntpd.service

# 开放udp 123端口
firewall-cmd --add-port=123/udp --permanent
firewall-cmd --reload

echo "已将10.0.0.61配置为NTP时间服务器!"
[root@Ansible ~]# sh /bash/ntpdate_server.sh
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                            | 3.6 kB     00:00     
epel                            | 4.7 kB     00:00     
extras                          | 2.9 kB     00:00     
updates                         | 2.9 kB     00:00     
(1/2): epel/x86_64/updateinfo     | 1.0 MB   00:05     
(2/2): epel/x86_64/primary_db     | 7.0 MB   00:21     
Package ntp-4.2.6p5-29.el7.centos.2.x86_64 already installed and latest version
Nothing to do
FirewallD is not running
FirewallD is not running
已将10.0.0.61配置为NTP时间服务器!

客户机测试 

[root@LB00 ~]# date -s 20080808
Fri Aug  8 00:00:00 CST 2008
[root@LB00 ~]# date
Fri Aug  8 00:00:02 CST 2008
[root@LB00 ~]# ntpdate 10.0.0.61
13 May 16:43:45 ntpdate[5658]: step time server 10.0.0.61 offset 465836637.498873 sec

成功校对,思路就是安装NTP服务,配置NTP文件,开启NTP服务,开放NTP端口

服务端修改时间,客户端再次同步

[root@Ansible ~]# date -s 20080808
Fri Aug  8 00:00:00 CST 2008
[root@Ansible ~]# systemctl restart ntpd

[root@LB00 ~]# ntpdate 10.0.0.61
 8 Aug 00:02:10 ntpdate[5665]: step time server 10.0.0.61 offset -465842879.257505 sec

同样没有问题,只是服务端要重启下服务,重新读取下配置文件,也就是读取服务端当前的时间。


我是koten,10年运维经验,持续分享运维干货,感谢大家的阅读和关注!

猜你喜欢

转载自blog.csdn.net/qq_37510195/article/details/130657769