[root@client ~]# tzselect #配置正确时区
[root@www ~]# vim /etc/sysconfig/clock
[root@www ~]# vim /etc/ntp.conf
logfile /var/log/ntp.log
driftfile /var/lib/ntp/drift
interface listen 192.168.100.254
interface ignore wildcard
interface listen lo
# 1. 先处理权限方面的问题,包括放行上层服务器以及开放区网用户来源:
restrict default kod nomodify notrap nopeer noquery <==拒绝 IPv4 的用户
restrict -6 default kod nomodify notrap nopeer noquery <==拒绝 IPv6 的用户
restrict 220.130.158.71 <==放行 tock.stdtime.gov.tw 进入本 NTP 服务器
restrict 59.124.196.83 <==放行 tick.stdtime.gov.tw 进入本 NTP 服务器
restrict 59.124.196.84 <==放行 time.stdtime.gov.tw 进入本 NTP 服务器
restrict 127.0.0.1 <==底下两个是默认值,放行本机来源
restrict -6 ::1
restrict 192.168.100.0 mask 255.255.255.0 nomodify <==放行区网来源
# 2. 设定主机来源,请先将原本的 [0|1|2].centos.pool.ntp.org 的设定批注掉:
server 220.130.158.71 prefer <==以这部主机为最优先
server 59.124.196.83
server 59.124.196.84
server time.nuri.net
server 0.asia.pool.ntp.org
server 1.asia.pool.ntp.org
server ntp.tuna.tsinghua.edu.cn
# 3.预设时间差异分析档案与暂不用到的 keys 等,不需要更动它:
driftfile /var/lib/ntp/drift
keys /etc/ntp/keys
# 1. 启动 NTP
[root@www ~]# /etc/init.d/ntpd start
[root@www ~]# chkconfig --level 35 ntpd on
[root@www ~]# tail -f logfile /var/log/ntp.log
[root@www ~]# ntpstat #这个指令可以列出我们的 NTP 服务器有跟上层联机否。
[root@www ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*tock.stdtime.go 59.124.196.87 2 u 19 128 377 12.092 -0.953 0.942
+59-124-196-83.H 59.124.196.86 2 u 8 128 377 14.154 7.616 1.533
+59-124-196-84.H 59.124.196.86 2 u 2 128 377 14.524 4.354 1.079
这个 ntpq -p 可以列出目前我们的 NTP 与相关的上层 NTP 的状态,上头的几个字段的意义为:
#1. 自动同步时间的前提是找到NTP服务器,如下为NTP服务器
#中国国家授时中心:210.72.145.44 ----暂时无法使用
#NTP服务器(上海) :ntp.api.bz
#中国ntp服务器:cn.pool.ntp.org
#pool.ntp.org
#2. 时间同步工具
#rdate -s
#ntpdate -u(使用-u参数会返回误差,也可以使用-s)
#3. 自动同步时间举例:
[root@client ~]# tzselect #配置正确时区
[root@client ~]# /usr/sbin/ntpdate -u cn.pool.ntp.org #同步时间
[root@client ~]# hwclock -w #将时间写入blos
#4. 将以上自动同步时间加入开机启动校验
vim /etc/rc.d/rc.local
/usr/sbin/ntpdate -u cn.pool.ntp.org> /dev/null 2>&1; /sbin/hwclock -w
#5. 将以上自动同步时间写入定时任务执行
vim /etc/crontab 或者 crontab -e
00 09 * * * root /usr/sbin/ntpdate -u cn.pool.ntp.org > /dev/null 2>&1; /sbin/hwclock -w
原文:https://www.cnblogs.com/wangzengyi/p/12520835.html