发布时间:2025-12-10 21:02:37 浏览次数:1
NTP同步方式在linux下一般两种:使用ntpdate命令直接同步和使用NTPD服务平滑同步。
使用ntpdate命令直接同步的方式存在风险,比如一些定时任务在已有时间内执行过,直接同步导致时间变回任务执行前的时间段,定时任务会重复执行。
使用NTPD服务平滑同步的方式不会让一个时间点在一天内经历两次,而是平滑同步时间,它每次同步时间的偏移量不会太陡,是慢慢来的。
查看是否安装NTP包
rpm-qa|grepntp
如果有输出ntp和ntpdate版本信息,即已安装。
ntp可用systemctl管理
#查看服务状态servicentpdstatus#启动ntpd服务systemctlstartntpd.service#停止ntpd服务systemctlstopntpd.service#设置开机自启动systemctlenablentpd.service#停止开机自启动systemctldisablentpd.service#查看服务当前状态systemctlstatusntpd.service#重新启动服务systemctlrestartntpd.service#查看所有已启动的服务systemctllist-units--type=service
作为ntp客户端,如何同步服务器端的时间?
以公用的一些时间同步服务器为例
systemctlstopntpd#使用ntpdate同步前需要关闭ntpd服务,不然会失败ntpdatecn.pool.ntp.orgsystemctlenablentpd.servicesystemctlstartntpd.service#cn.pool.ntp.org中国开源免费NTP服务器#ntp1.aliyun.com阿里云NTP服务器#ntp2.aliyun.com阿里云NTP服务器#time1.aliyun.com阿里云NTP服务器#time2.aliyun.com阿里云NTP服务器
如何配置服务器作为内网内的时间同步服务器?(NTP服务器的配置)
配置内网NTP-Server(10.0.0.12)
配置NTPD服务的服务器需要能访问外网,这里挑选了一台可以访问外网的Linux服务器配置内网的NTPD服务,作为NTP-Server,其他几台内网通过它来进行时间同步。
这里假设其IP为10.0.0.12,其他几台内网的服务器IP分别为10.0.0.13、10.0.0.14。
在配置NTPD服务之前,先手动同步一下时间
systemctlstopntpd#使用ntpdate同步前需要关闭ntpd服务,不然会失败ntpdatecn.pool.ntp.orgsystemctlenablentpd.servicesystemctlstartntpd.service
修改NTPD服务的配置文件/etc/ntp.conf
[root@localhost~]#vim/etc/ntp.conf#和上层NTP服务器频率误差的记录文件driftfile/var/lib/ntp/drift#对于默认的client拒绝所有操作,下面的restrict开通指定权限restrictdefaultnomodifynotrapnopeernoquery#允许本机地址的一切操作restrict127.0.0.1restrict::1#允许192.168.203段机器同步时间,指定可以和NTP通信的IP或者网段restrict192.168.203.0mask255.255.255.0nomodifynotrap#远程的同步时间服务器地址(需要同步外网时间必改)#prefer表示优先同步的主机servercn.pool.ntp.orgpreferserverntp1.aliyun.comiburstserverntp2.aliyun.comiburstserverntp3.aliyun.comiburstserverntp4.aliyun.comiburstserverntp5.aliyun.comiburst#远程时间服务器不可用时,以本地时间作为服务时间server127.127.1.0#stratum指定层数,越小,离时间源,越近。层级1为国际时间源服务器...fudge127.127.1.0stratum10#允许上层服务器主动修改本机时间(需要同步外网时间必改)restrictcn.pool.ntp.orgnomodifynotrapnoqueryrestrictntp1.aliyun.comiburstnomodifynotrapnoqueryrestrictntp2.aliyun.comiburstnomodifynotrapnoqueryrestrictntp3.aliyun.comiburstnomodifynotrapnoqueryrestrictntp4.aliyun.comiburstnomodifynotrapnoqueryrestrictntp5.aliyun.comiburstnomodifynotrapnoqueryincludefile/etc/ntp/crypto/pw#指定日志文件logfile/var/log/ntp#指定日志级别,info、all、event三种可以选择logconfigall#启用公钥加密#crypto#密钥文件keys/etc/ntp/keys
设置ntpclient客户端配置文件
vim/etc/ntp.confserver10.0.0.12
重启ntp服务
[root@localhost/]#systemctlstopfirewalld[root@localhost/]#setenforce0[root@localhost/]#vim/etc/selinux/configSELINUX=disabled[root@localhost/]#systemctlrestartntpd
查看ntp服务同步状况
ntpd-q