一、编译安装
#centos 已经默认已经安装了NTP,Ubuntu等系统可用如下命令安装。
wget http://www.eecis.udel.edu/~ntp/ntp_spool/ntp4/ntp-4.2/ntp-4.2.8p12.tar.gz
tar -xzvf ntp-4.2.8p12.tar.gz
cd ntp-4.2.8p12
./configure
make
make install
二、服务端配置
2.1 配置ntp.conf
vim /etc/ntp.conf
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
driftfile /var/lib/ntp/drift
#default表示 地址0.0.0.0,掩码0.0.0.0 意为任意IP
#不允许所有IP操作 -6表示ipv6
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
#这是允许本机查询
restrict 127.0.0.1
restrict -6 ::1 # 6指ipv6
#在192.168.1.0/24网段内的服务器就可以通过这台NTP Server进行时间同步了
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
# Use public servers from the pool.ntp.org project.
# 公共的NTP服务地址,因为该服务仅在内网使用,且该主机为服务器,所以配置本机地址
server 127.127.1.0
#NTP使用伪IP地址127.127.8.n 访问安装在本地计算机上的Meinberg无线电时钟。为了访问自己的系统时钟,也称为本地时钟,NTP使用伪IP地址127.127.1.0。此IP地址不得与127.0.0.1混淆,后者是本地主机的IP ,即计算机的环回接口。
#stratum 层次 根据上层server的层次而设定 作为局域网的time service provider,通常将stratum设置为10
fudge 127.127.1.0 stratum 10
# Enable public key cryptography.
#crypto
includefile /etc/ntp/crypto/pw
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
# 定义1~65535个key 客户端的需要与服务器端的内容一致
keys /etc/ntp/keys
# Specify the key identifiers which are trusted.
# 指定/etc/ntp/keys文件种信任的key,只有指定的key才可以与服务器同步
trustedkey 1 2
#由于身份验证涉及额外计算,因此关键字authdelay指定此时间量
authdelay 0.000094
# Specify the key identifier to use with the ntpdc utility.
#requestkey 8
# Specify the key identifier to use with the ntpq utility.
#controlkey 8
# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats
2.2 配置服务端密钥
# vim /etc/ntp/keys
# PLEASE DO NOT USE THE DEFAULT VALUES HERE.
1 M 1111111
2 M 5555555
2.3 修改系统配置
# vim /etc/sysconfig/ntpd
修改如下内容:
# Drop root to id 'ntp:ntp' by default.
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"
#允许硬件系统时间同步
SYNC_HWCLOCK=yes
2.4 启动服务和设置开机启动
# systemctl start ntpd
# systemctl enable ntpd
三、客户端配置
3.1 修改 ntp.conf 文件
# vim /etc/ntp.conf
修改如下内容:
driftfile /var/lib/ntp/drift
restrict 127.0.0.1
restrict -6 ::1
# 指定服务器地址 和key
server 192.168.1.102 key 1
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
# 默认时间差超过1000s ntp就不会修改时间,禁用该功能。
tinker panic 0
3.2 配置同步密钥
# vim /etc/ntp/keys
# PLEASE DO NOT USE THE DEFAULT VALUES HERE.
1 M 1111111
2 M 5555555
3.3 修改系统配置
# vim /etc/sysconfig/ntpd
修改如下内容:
# Drop root to id 'ntp:ntp' by default.
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"
#允许硬件系统时间同步
SYNC_HWCLOCK=yes
3.4 启动ntp服务和设置开机自动
# systemctl start ntpd
# systemctl enable ntpd
四、客户端查看和服务端的同步情况
# ntpq -pn
指令“ntpq -p”可以列出目前我们的NTP与相关的上层NTP的状态,以上的几个字段的意义如下:
remote:即NTP主机的IP或主机名称。注意最左边的符号,如果由“+”则代表目前正在作用钟的上层NTP,如果是“*”则表示也有连上线,不过是作为次要联机的NTP主机。
refid:参考的上一层NTP主机的地址
st:即stratum阶层
when:几秒前曾做过时间同步更新的操作
poll:下次更新在几秒之后
reach:已经向上层NTP服务器要求更新的次数
delay:网络传输过程钟延迟的时间
offset:也就是和时间服务器还差多少毫秒
jitter:Linux系统时间与BIOS硬件时间的差异时间