NTP时间服务器介绍
NTP 服务器是用于局域网服务器时间同步使用的,可以保证局域
网所有的服务器与时间服务器的时间保持一致,某些应用对时间实时
性要求高的必须统一时间。
免费的时间服务器
复旦大学免费NTP:"ntp.fudan.edu.cn
阿里云免费NTP:"ntp1.aliyun.com
开始搭建
1.本地防火墙开启123端口
NTP 服务器监听端口为 UDP 的 123,那就需要在本地防火墙开启
运行客户端访问 123 端口,进入vi /etc/sysconfig/iptables 添加如下规则:
-A INPUT -m state --state NEW -m udp -p udp --dport 123 -j ACCEPT
搭建ntp服务端
1.yum安装ntp
yum install ntp ntpdate -y
2.配置ntp.conf配置文件
#备份源配置文件
cp /etc/ntp.conf.bak
#编辑ntp.conf配置文件
进入:vi /etc/ntp.conf
做如下修改(添加黑体部分)
driftfile /var/lib/ntp/drift
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
server 127.127.1.0 # local clock
server ntp3.aliyun.com #阿里云时间服务器
fudge 127.127.1.0 stratum 10
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
启动/关闭/重启-ntp服务/查看其状态
(注意*: ntpd 启动后,客户机要等几分钟再与其进行时间同步,否
则会提示“no server suitable for synchronization found”错误。)
搭建ntp时间同步机(客户机)
1.安装ntp服务
yum install ntp ntpdate -y
2.测试
#查看当前时间
[root@bogon ~]# date
Tue Oct 8 01:02:05 PDT 2019
#修改机器时间
[root@bogon ~]# date -s "20120523 01:01:01"
Wed May 23 01:01:01 PDT 2012
#同步服务端时间
[root@bogon ~]# ntpdate 192.168.150.151
8 Oct 00:33:35 ntpdate[21750]: step time server 192.168.150.151 offset 232759902.686916 sec
#再次查看。。。
3.添加定时任务
#注意:修改192.168.150.151为自己服务端的ip
[root@bogon ~]# crontab -e
0 0 * * * /usr/sbin/ntpdate 192.168.150.151>>/data/logs/ntp.log 2>&1
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"/tmp/crontab.APhwqJ" 1L, 75C
参考自:https://blog.csdn.net/qq_34672033/article/details/87821694#NTP_1