安装ntp组件

网络时间协议ntp是时间服务器的一种协议。在时间服务器工作时,其可利用不同的工作模式与其他服务器或用户端进行时间的调整,实现时间的同步。

使用ntp需要安装ntp服务组件:

1yum install -y ntpdate ntp
2
3
4systemctl start ntpdate
5systemctl start ntpd
6
7systemctl enable ntpd
8systemctl enable ntpdate

在ntpd服务进程启动的时候,可以使用ntpstat命令获取系统每次校正时间的间隔:

1 ntpstat
2unsynchronised
3   polling server every 64 s

另外可使用ntpdate命令直接同步:

1ntpdate -u cn.pool.ntp.org

写入系统硬件时间:

1hwclock -w 

ntp配置文件

/etc/ntp.cnf是ntp的主配置文件。

 1driftfile  /var/lib/ntp/drift #用于上层服务器联系时所用时间的记录文件
 2pidfile   /var/run/ntpd.pid
 3logfile /var/log/ntp.log
 4
 5
 6restrict default nomodify notrap nopeer noquery #拒绝其他计算机的数据请求包
 7
 8restrict 127.0.0.1  #通过此接口控制和配置本地ntp时间服务器
 9restrict ::1
10
11# Hosts on local network are less restricted.
12#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap 允许指定的IP段同步时间服务器的时间
13
14server s2a.time.edu.cn iburst minpoll 4 maxpoll 10  #第一层时间服务器地址
15server 2.cn.pool.ntp.org iburst minpoll 4 maxpoll 10
16server 1.asia.pool.ntp.org iburst minpoll 4 maxpoll 10
17server 2.asia.pool.ntp.org iburst minpoll 4 maxpoll 10
18server 0.centos.pool.ntp.org iburst minpoll 4 maxpoll 10
19server 1.centos.pool.ntp.org iburst minpoll 4 maxpoll 10
20server 2.centos.pool.ntp.org iburst minpoll 4 maxpoll 10
21server 3.centos.pool.ntp.org iburst minpoll 4 maxpoll 10