- # mv /etc/ntp.conf /etc/ntp.conf.bak
- # VIM /etc/ntp.conf 新增如下代码:
driftfile /var/lib/ntp/drift 有不用加
- logfile /var/log/ntp.log
- pidfile /var/run/ntpd.pid
includefile /etc/ntp/crypto/pw 有不用加
keys /etc/ntp/keys 有不用加
# 允许任何 IP 的客户端进行时间同步, 但不允许修改 NTP 服务端参数, default 类似于 0.0.0.0
restrict default kod nomodify notrap nopeer noquery 有不用加
restrict -6 default kod nomodify notrap nopeer noquery 有不用加
# 允许通过本地回环接口进行所有访问
restrict 127.0.0.1 有不用加
restrict -6 ::1 有不用加
# 只允许 192.168.1 网段的客户端进行时间同步, 但不允许修改 NTP 服务端参数
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap nopeer noquery 有不用加
- restrict 0.0.0.0 mask 0.0.0.0 nomodify notrap restrict default #default 指所有的 IP
- # 将原有的 server 0.CentOS.pool.ntp.org iburst 全部注释掉
server ntp1.aliyun.com iburst 新增
- # 指定上层 NTP 服务器为阿里云提供的公网 NTP 服务器, 并设置访问权限
- restrict ntp1.aliyun.com nomodify notrap nopeer noquery
- restrict ntp2.aliyun.com nomodify notrap nopeer noquery
- restrict ntp3.aliyun.com nomodify notrap nopeer noquery
- restrict ntp4.aliyun.com nomodify notrap nopeer noquery
- restrict ntp5.aliyun.com nomodify notrap nopeer noquery
- restrict ntp6.aliyun.com nomodify notrap nopeer noquery
- restrict ntp7.aliyun.com nomodify notrap nopeer noquery
- server ntp1.aliyun.com iburst minpoll 4 maxpoll 10
- server ntp2.aliyun.com iburst minpoll 4 maxpoll 10
- server ntp3.aliyun.com iburst minpoll 4 maxpoll 10
- server ntp4.aliyun.com iburst minpoll 4 maxpoll 10
- server ntp5.aliyun.com iburst minpoll 4 maxpoll 10
- server ntp6.aliyun.com iburst minpoll 4 maxpoll 10
- server ntp7.aliyun.com iburst minpoll 4 maxpoll 10
- # 阿里云提供的公网 NTP 服务器不可用时, 采用本地时间作为同步标准
- server 127.127.1.0
- fudge 127.127.1.0 stratum 10
备注: 常用指令说明
kod: 访问违规时发送 KOD 包
nomodify: 客户端不能更改服务端的参数信息, 但是客户端可以通过服务端进行网络校时
notrap: 不提供 trap 远程事件登录
nopeer: 用于阻止主机尝试与服务器对等, 并允许欺诈性服务器控制时钟
noquery: 不提供客户端时间查询, 客户端不能使用 ntpq,ntpdc 等命令来查询 NTP 服务器
notrust: 客户端除非通过认证, 否则该客户端来源将被视为不信任网域
iburst: 如果服务端在一个标准轮询间隔内没有应答, 客户端会发送一定数量的包 (8 个而不是通常的 1 个) 给 NTP 服务端, 如果在短时间内呼叫 NTP 服务端几次, 都没有出现可辨识的应答, 那么本地时间将不会发生变化
minpoll:NTP 消息轮询间隔的最小值, 默认为 6(64 秒)
maxpoll:NTP 消息轮询间隔的最大值, 默认为 10(1024 秒)
7, 修改配置文件 / etc/sysconfig/ntpd, 末尾新增代码: SYNC_HWCLOCK=yes
备注: 默认情况下, CentOS 系统时间和硬件时间并不会自动同步. 在 CentOS 运行过程中, 系统时间和硬件时间以异步的方式运行, 互不干扰. 硬件时间的运行是靠 BIOS 电池来维持, 而系统时间是靠 CPU tick 来维持. 在系统开机时, 会自动从 BIOS 中读取硬件时间, 并设置为系统时间. NTP 一般只会同步系统时间, 如果需要使硬件时间和系统时间同步, 就需要修改配置文件 / etc/sysconfig/ntpd.
8, 修改用于初始同步的服务器列表配置文件 / etc/ntp/step-tickers:
- # VIM /etc/ntp/step-tickers, 新增如下代码:
- ntp1.aliyun.com
- ntp2.aliyun.com
- ntp3.aliyun.com
- ntp4.aliyun.com
- ntp5.aliyun.com
- ntp6.aliyun.com
- ntp7.aliyun.com
9, 启动 ntpd 服务:
service ntpd start 启动 ntp 服务器
- ss -tunlp | grep -w :123
- yum -y install lsof
- lsof -i :123
chkconfig ntpd on 设为开机启动
chkconfig --list ntpd
10, 在 5~10 分钟后查看时间同步状态: ntpstat
备注:
120.25.115.20 为 ntp1.aliyun.com 域名解析后的地址
本地服务器与上层服务器的时间偏差小于 28ms
本地服务器每隔 64s 主动同步一次时间
查看所有作为时钟源校正过本地 NTP 服务器的上层 NTP 服务器列表:# ntpq -p
来源: http://www.bubuko.com/infodetail-3086911.html