前言:
NTP是网络时间协议(Network Time Protocol),它是用来同步网络中各个计算机的时间的协议。
NTP服务一般是集群/服务器搭建之时,就已经安装配置好,但是NTP本身存在对外开放的接口,导致服务被人为恶意攻击的可能。对于软件程序漏洞,一般的做法是升级到高阶版本,即可修复漏洞。下文即NTP服务安装配置与升级的操作手册。
NTP服务配置
服务器端
vim /etc/ntp.conf
需要修改以下几项:
- restrict:表示设置客户端连接到本NTP服务器的权限
其规则为:restrict ip mask netmask_ip parameter
其中IP 可以是地址位,也可以是default;parameter有以下选项:
- ignore:关闭所有的NTP链接服务
- nomodify:表示client不能更改server端的参数信息
- notrust:该client除非通过认证,否则该客户端的来源将被视为不信任网域
- noquery:不提供客户端的时间查询
- server:表示设置本NTP服务器连接到远程NTP服务器的地址
server 127.127.1.0
fudge 127.127.1.0 stratum 10
以上两行表示如果server指定的服务器连接失败,则采用本地的时间来同步时间
- 配置日志:
statsdir /var/log/ntp/
logfile /var/log/ntp/ntp.log
# 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
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
#restrict default kod nomodify notrap nopeer noquery
#restrict -6 default kod nomodify notrap nopeer noquery
restrict 192.168.1.222 nomodify notrap nopeer noquery
restrict 192.168.1.255 mask 255.255.255.0 nomodify notrap
# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict -6 ::1
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server 202.120.2.101
server 202.112.10.36
server 59.124.196.83
#
#
#
server 127.127.1.0
Fudge 127.127.1.0 stratum 10
#broadcast 192.168.1.255 autokey # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 autokey # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 autokey # manycast client
# 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.
keys /etc/ntp/keys
# Specify the key identifiers which are trusted.
#trustedkey 4 8 42
# 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
客户机
客户端测试
通过linux客户端测试ntp server
# /usr/sbin/ntpdate 192.168.16.222
5 Aug 15:43:22 ntpdate[30958]: adjust time server 192.168.16.222 offset 0.000576 sec
然后设置定时任务,1秒同步一次:
*/1 * * * * /usr/sbin/ntpdate 192.168.16.222 >> /root/ntpdate.log 2>&1
版本升级
1. 系统与软件版本
1.1 系统版本
CentOS6.5 x86_64
1.2 ntpd软件版本
ntp-4.2.8p9.tar.gz
1.3 下载地址
官方下载地址:http://support.ntp.org/bin/view/Main/SoftwareDownloads
参考文档:http://www.linuxfromscratch.org/blfs/view/svn/basicnet/ntp.html
2. 安装前提
2.1 安装依赖包
[root@ntpserver ~]# yum install gcc gcc-c++ openssl-devel libstdc++* libcap*
2.2 备份旧版本的配置文件
[root@ntpserver ~]# cp -ar /etc/ntp /etc/ntp.bak
[root@ntpserver ~]# cp /etc/ntp.conf /etc/ntp.conf.bak
[root@ntpserver ~]# cp /etc/init.d/ntpd /etc/init.d/ntpd.bak
[root@ntpserver ~]# cp /etc/sysconfig/ntpd /etc/sysconfig/ntpd.bak
[root@ntpserver ~]# cp /etc/sysconfig/ntpdate /etc/sysconfig/ntpdate.bak
2.3 卸载yum安装的ntpd服务
[root@ntpserver ~]# yum erase ntp ntpdate
3. 编译安装ntp-4.2.8p9
3.1 创建/var/lib/ntp目录
[root@ntpserver ~]# install -v -m710 -o ntp -g ntp -d /var/lib/ntp
3.2 编译安装ntpd
[root@ntpserver ntp-4.2.8p9]# ./configure \
--prefix=/usr \
--bindir=/usr/sbin \
--sysconfdir=/etc \
--enable-linuxcaps \
--with-lineeditlibs=readline \
--docdir=/usr/share/doc/ntp-4.2.8p9 \
--enable-all-clocks \
--enable-parse-clocks \
--enable-clockctl
make && make install
注意: 如果ntp刚启动起来,在客户端同步的时候会出现 no server suitable for synchronization found 的错误提示,这是因为ntp server 刚启动起来,还没有和上级ntp取得同步,大概过5分钟左右再在客户端上同步,可得到正确信息。