ntp源码安装

第一种:

https://blog.csdn.net/wzq756984/article/details/73866231

https://my.oschina.net/adailinux/blog/1861001

Centos7编译安装ntp-4.2.8p11

背景

因公司做等保评级,在进行安全漏洞检测时发现ntp需要升级到ntp-4.2.7p25以上版本,经过一番搜索,没有该版本及新版本ntp的yum安装包,所以只能编译安装了,网上搜到两篇文章,但是参考价值一般,所以自己摸索爬坑,在此记录一下。

环境

  • 系统环境:Centos 7.2
  • ntpd版本:4.2.8p11

安装

下载安装包

# 官方下载
$ wget http://www.eecis.udel.edu/~ntp/ntp_spool/ntp4/ntp-4.2/ntp-4.2.8p11.tar.gz

# 解压安装包
$ tar zxvf ntp-4.2.8p11.tar.gz

# 全局配置
$ cd ntp-4.2.8p11/
$ ./configure --prefix=/usr/local/ntp --bindir=/usr/local/ntp/sbin --sysconfdir=/etc --libexecdir=/usr/local/ntp/libexec --docdir=/usr/local/ntp/doc/ntp --enable-linuxcaps  --with-lineeditlibs=readline  --enable-all-clocks --enable-parse-clocks --enable-clockctl --enable-ntpdate-step --enable-libopts-install   # 配置(查看使用指南--help)

# 报错1
"/usr/bin/ld: cannot find -lcap"
# fix
$ find / -name "*libcap.so*""
$ ln -sv /usr/lib64/libcap.so.2 /usr/lib/libcap.so


# 报错2
“ntpd.c:120:29: 致命错误:sys/capability.h:没有那个文件或目录”
# fix
$ yum install -y libcap-devel

$ make && make install   # 编译 && 安装
$ echo $?   # 检测安装过程是否出错,0表示没错

注意: 因本机系统环境已完成初始化部署,所以一些依赖包的安装省略,如果遇到缺少某些lib*查到情况,直接yum安装即可。

配置

安装完成后并没有配置文件生成,需手动创建,官方指定其默认配置文件为:/etc/ntp.conf

$ vim /etc/ntp.conf

driftfile /var/lib/ntp/drift  # 指定时间漂移记录文件,作用:如果ntpd停止并重新启动,它将从该文件初始化频率,并避免可能的长时间间隔重新学习校正。

# 指定remote ntp服务器
server 202.120.2.101  prefer iburst minpoll 4 maxpoll 6  
## prefer:优先使用
## minpoll && maxpoll:
server 0.pool.ntp.org
server 1.pool.ntp.org
server 2.pool.ntp.org
server 3.pool.ntp.org

statistics loopstats peerstats clockstats

filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable


restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery
restrict 127.0.0.1
restrict 10.9.255.1
restrict 10.9.255.2
restrict ::1

启动ntpd

  • 将ntp相关命令加入系统环境变量:

    cp /usr/local/ntp/sbin/*  /usr/lcoal/sbin
    
  • 将ntpd服务加入系统管理:

    $ systemctl cat ntpd.service
    # /usr/lib/systemd/system/ntpd.service
    [Unit]
    Description=Network Time Service
    After=syslog.target
    
    [Service]
    Type=forking
    EnvironmentFile=-/etc/sysconfig/ntpd
    ExecStart=/usr/local/sbin/ntpd -u ntp:ntp $OPTIONS
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    
    
    $ systemctl enable ntpd  # 加入开机启动
    $ systemctl start/stop/status/restart ntpd
    

Finished(踩坑很多,本次最大的坑为--enable-ipv6相关,如果你在安装过程中也遇到了make无法通过的问题,直接pass该参数吧,具体原因还烦路过的大神多多指教)!!!

源码安装的ntp服务/etc/下是没有ntp.conf配置文件的,需要先yum安装ntp服务把/etc/ntp.conf文件备份后,在yum –y remove ntp*删除掉安装的ntp。

下面正式开始安装

1、首先创建一个ntp目录

mkdir /ntp

cd /ntp

2.下载ntp源码安装包

wget http://www.eecis.udel.edu/~ntp/ntp_spool/ntp4/ntp-4.2.6p5.tar.gz

3.解压

tar -xzvf ntp-4.2.6p5.tar.gz

4.编译并安装

cd ntp-4.2.6p5

./configure --prefix=/usr/local/ntp--enable-all-clocks --enable-parse-clocks

make &&make install

5.更改配置文件

1)允许任何IP的客户机都可以进行时间同步
将“restrictdefault kod nomodify notrap nopeer noquery”这行修改成:
restrict default nomodify
2)只允许192.168.1.*网段的客户机进行时间同步
在“restrictdefault nomodify notrap noquery(表示默认拒绝所有IP的时间同步)”之后增加一行:
restrict 192.168.1.0 mask 255.255.255.0nomodify

另外,配置文件默认通过网络同步时间:

1

server 0.centos.pool.ntp.org iburst

2

server 1.centos.pool.ntp.org iburst

 

3

server 2.centos.pool.ntp.org iburst

 

4

server 3.centos.pool.ntp.org iburst

(RedHat默认有类似上面4行内容,域名不一样而已)
如果需要使用本地时间,则把上面4行删掉或者用“#”号注释掉,然后添加以下两行:

1

server 127.127.1.0

2

fudge  127.127.1.0 stratum 10


6.启动ntp服务

/usr/local/ntp/bin/ntpd-c /etc/ntp.conf -p /tmp/ntpd.pid

7.开机自启动

直接把ntp启动命令添加到/etc/re.local就可以

8.查看ntp服务器工作情况

netstat –unl|grep 123   #查看123端口,结果如下

9.查看ntp服务是否正常启动

PS:ntpd启动后,客户机要等几分钟再与其进行时间同步,否则会提示“noserver suitable for synchronization found”错误。

配置时间同步客户机

[root@client ~]# date    #同步之前系统时间
2012年 10月 12日星期五 22:44:08 CST
[root@client ~]# ntpdate 192.168.1.41 #192.168.1.41是NTP服务器的IP
[root@client ~]# hwclock -w  #把时间写入BIOS
[root@client ~]# date    #同步之后系统时间
2012年 10月 11日星期四 21:45:30 CST  
[root@client ~]# crontab -e  #每天凌晨4点Linux系统自动进行网络时间校准
00 04 * * * /usr/sbin/ntpdate 192.168.1.41;/sbin/hwclock -w
[root@client ~]# service crond restart  #重启crond服务

第二种:

yum安装看看缺少那个包,少哪个包就下载哪个包,然后使用rpm安装:

例如:

 就去下载这两个包

下载地址:http://buildlogs-seed.centos.org/c7.1804.00.x86_64/ntp/20180413125044/4.2.6p5-28.el7.centos.x86_64/

http://rpm.pbone.net/index.php3/stat/4/idpl/32136138/dir/scientific_linux_7/com/autogen-libopts-5.18-5.el7.x86_64.rpm.html

安装命令:

rpm -ivh ntp-4.2.6p5-28.el7.centos.x86_64.rpm 

libcap-devel-2.22-9.el7.x86_64 .rpm

ntpdate-4.2.6p5-28.el7.centos.x86_64.rpm

ntp-4.2.6p5-28.el7.centos.x86_64.rpm

autogen-libopts-5.18-5.el7.x86_64.rpm 

rpm卸载命令:

例如:rpm -e  ntpdate-4.2.6p5-28.el7.centos.x86_64.rpm

原文地址:https://www.cnblogs.com/js1314/p/13864632.html