NTP的服务端和客户端配置

NTP

内网集群中,有很多台服务器的情况下,运行了一段时间之后每台服务器的时间可能都不一样了。不一样的时间,会带来很多问题,要是偏差很大的话,有的框架会出错。
ntp可以解决这个问题,它可以步进式地逐渐调整时间。

安装

yum install ntp

配置

服务端配置

1. /etc/ntp.conf

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
restrict default kod nomodify notrap nopeer noquery
# restrict -6 default kod nomodify notrap nopeer noquery  #针对ipv6设置

# 允许本地所有操作
restrict 127.0.0.1
#restrict -6 ::1

# 允许的局域网络段或单独ip
restrict 10.0.0.0 mask 255.0.0.0 nomodify motrap
restrict 192.168.0.0 mask 255.255.255.0 nomodify motrap
restrict 192.168.1.123 mask 255.255.255.255 nomodify motrap

# 使用上层的internet ntp服务器
server cn.pool.ntp.org prefer
server 0.asia.pool.ntp.org
server 3.asia.pool.ntp.org
server 0.centos.pool.ntp.org iburst

# 如果无法与上层ntp server通信以本地时间为标准时间
server   127.127.1.0    # local clock
fudge    127.127.1.0 stratum 10

# 计算本ntp server 与上层ntpserver的频率误差
driftfile /var/lib/ntp/drift

# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys

#日志文件
logfile /var/log/ntp.log

2. /etc/sysconfig/ntpd

1
2
3
4
5
6
# Drop root to id 'ntp:ntp' by default.
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid"
# Set to 'yes' to sync hw clock after successful ntpdate
SYNC_HWCLOCK=yes #make no into yes; BIOS的时间也会跟着修改
# Additional options for ntpdate
NTPDATE_OPTIONS=""

客户端配置

1. /etc/ntp.conf

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 这行意思是设置客户端与服务器端时间差距,默认最大为1000,为0为不限制
tinker panic 0

#  (minpoll表示客户端向服务器端同步的最少时间,单位为2的次幂最小为3,秒)
server 15.17.26.24 prefer  minpoll 3 maxpoll 3
#(maxpoll表示客户端向服务器端同步的最长时间,单位为2的次幂最大为10,秒)
server 15.17.26.24 iburst  minpoll 3 maxpoll 3

#
restrict 15.17.26.24

里面的15.17.26.24就是ntp服务器的ip

启动

systemctl start ntpd

查看状态

  1. ntpq -p 可以查看当前客户端和服务器的连接情况

  2. netstat -ulnp 查看ntpd是否运行在123端口

问题

1. ntpd -p提示No association ID’s returned

执行systemctl status ntpd查看重启是否执行成功,要是有显示failed的话。需要把之前的ntpd进程直接kill掉再systemctl start ntpd

参考资料