安装ntp组件
网络时间协议ntp是时间服务器的一种协议。在时间服务器工作时,其可利用不同的工作模式与其他服务器或用户端进行时间的调整,实现时间的同步。
使用ntp需要安装ntp服务组件:
yum install -y ntpdate ntp
systemctl start ntpdate
systemctl start ntpd
systemctl enable ntpd
systemctl enable ntpdate
在ntpd服务进程启动的时候,可以使用ntpstat命令获取系统每次校正时间的间隔:
ntpstat
unsynchronised
polling server every 64 s
另外可使用ntpdate命令直接同步:
ntpdate -u cn.pool.ntp.org
写入系统硬件时间:
hwclock -w
ntp配置文件
/etc/ntp.cnf是ntp的主配置文件。
driftfile /var/lib/ntp/drift #用于上层服务器联系时所用时间的记录文件
pidfile /var/run/ntpd.pid
logfile /var/log/ntp.log
restrict default nomodify notrap nopeer noquery #拒绝其他计算机的数据请求包
restrict 127.0.0.1 #通过此接口控制和配置本地ntp时间服务器
restrict ::1
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap 允许指定的IP段同步时间服务器的时间
server s2a.time.edu.cn iburst minpoll 4 maxpoll 10 #第一层时间服务器地址
server 2.cn.pool.ntp.org iburst minpoll 4 maxpoll 10
server 1.asia.pool.ntp.org iburst minpoll 4 maxpoll 10
server 2.asia.pool.ntp.org iburst minpoll 4 maxpoll 10
server 0.centos.pool.ntp.org iburst minpoll 4 maxpoll 10
server 1.centos.pool.ntp.org iburst minpoll 4 maxpoll 10
server 2.centos.pool.ntp.org iburst minpoll 4 maxpoll 10
server 3.centos.pool.ntp.org iburst minpoll 4 maxpoll 10