关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

云服务器centos7时间同步——NTP

发布时间:2023/11/15 14:51:55
香港云服务器

NTP同步方式在linux下一般两种:使用ntpdate命令直接同步和使用NTPD服务平滑同步。

使用ntpdate命令直接同步的方式存在风险,比如一些定时任务在已有时间内执行过,直接同步导致时间变回任务执行前的时间段,定时任务会重复执行。

使用NTPD服务平滑同步的方式不会让一个时间点在一天内经历两次,而是平滑同步时间,它每次同步时间的偏移量不会太陡,是慢慢来的。

查看是否安装NTP包

rpm -qa |grep ntp

如果有输出ntp和ntpdate版本信息,即已安装。

  • ntpdate :时间同步某台服务器
  • ntp :作为时间服务器

ntp 可用systemctl管理

# 查看服务状态service ntpd status# 启动ntpd服务systemctl start ntpd.service# 停止ntpd服务systemctl stop ntpd.service# 设置开机自启动systemctl enable ntpd.service# 停止开机自启动systemctl disable ntpd.service# 查看服务当前状态systemctl status ntpd.service# 重新启动服务systemctl restart ntpd.service# 查看所有已启动的服务systemctl list-units --type=service

作为ntp客户端,如何同步服务器端的时间?

以公用的一些时间同步服务器为例

systemctl stop ntpd # 使用ntpdate同步前需要关闭ntpd服务,不然会失败ntpdate cn.pool.ntp.orgsystemctl enable ntpd.servicesystemctl start ntpd.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服务之前,先手动同步一下时间

systemctl stop ntpd # 使用ntpdate同步前需要关闭ntpd服务,不然会失败ntpdate cn.pool.ntp.orgsystemctl enable ntpd.servicesystemctl start ntpd.service

修改NTPD服务的配置文件/etc/ntp.conf

[root@localhost ~]# vim /etc/ntp.conf#和上层NTP服务器频率误差的记录文件driftfile /var/lib/ntp/drift#对于默认的client拒绝所有操作,下面的restrict开通指定权限restrict default nomodify notrap nopeer noquery#允许本机地址的一切操作restrict 127.0.0.1restrict ::1#允许192.168.203段机器同步时间,指定可以和NTP通信的IP或者网段restrict 192.168.203.0 mask 255.255.255.0 nomodify notrap#远程的同步时间服务器地址(需要同步外网时间必改)#prefer表示优先同步的主机server cn.pool.ntp.org preferserver ntp1.aliyun.com iburstserver ntp2.aliyun.com iburstserver ntp3.aliyun.com iburstserver ntp4.aliyun.com iburstserver ntp5.aliyun.com iburst#远程时间服务器不可用时,以本地时间作为服务时间server 127.127.1.0#stratum指定层数,越小,离时间源,越近。 层级1为国际时间源服务器...fudge 127.127.1.0 stratum 10#允许上层服务器主动修改本机时间(需要同步外网时间必改)restrict cn.pool.ntp.org nomodify notrap noqueryrestrict ntp1.aliyun.com iburst nomodify notrap noqueryrestrict ntp2.aliyun.com iburst nomodify notrap noqueryrestrict ntp3.aliyun.com iburst nomodify notrap noqueryrestrict ntp4.aliyun.com iburst nomodify notrap noqueryrestrict ntp5.aliyun.com iburst nomodify notrap noqueryincludefile /etc/ntp/crypto/pw#指定日志文件logfile /var/log/ntp#指定日志级别,info、all、event三种可以选择logconfig all#启用公钥加密#crypto#密钥文件keys /etc/ntp/keys

设置ntp client客户端配置文件

vim /etc/ntp.confserver 10.0.0.12

重启ntp服务

[root@localhost /]# systemctl stop firewalld[root@localhost /]# setenforce 0[root@localhost /]# vim /etc/selinux/configSELINUX=disabled[root@localhost /]# systemctl restart ntpd

查看ntp服务同步状况

ntpd -q