zl程序教程

您现在的位置是:首页 >  其他

当前栏目

NTP服务升级

2023-03-07 09:15:02 时间

一、背景

使用 `yum install ntpdate ntp` 安装的ntp版本应该是:ntpd 4.2.6p5,该版本存在【拒绝服务攻击、信息泄露漏洞】问题,攻击者可通过此漏洞获取服务器版本信息,可发起拒绝服务攻击。可通过以下命令查看:

nmap -Pn -sU -p123 --script ntp-info -n $ip

返回信息:

按安全团队要求,需关闭ntp服务,或升级到最新版本。详情可以看 https://doc.ntp.org/support/securitynotice/#426 以上版本解决的 security issue。

如果直接使用 `yum update ntpdate ntp`升级,会提示没有可用软件包。因此,需要手动离线安装。可以下载源文件,进行编译安装,或者下载rpm包安装。由于使用rpm包安装时,遇到依赖依赖某些.so的报错问题,本文选择使用源码安装。

二、准备

下载安装包

wget  http://www.eecis.udel.edu/~ntp/ntp_spool/ntp4/ntp-4.2/ntp-4.2.8p15.tar.gz
tar -zxvf ntp-4.2.8p15.tar.gz
cd ntp-4.2.8p15/

三、安装

1. 安装依赖包

yum install gcc gcc-c++ openssl-devel libstdc++* libcap*

2. 备份旧版本的配置文件

 cp -ar /etc/ntp /etc/ntp.bak
 cp /etc/ntp.conf /etc/ntp.conf.bak
 cp /etc/init.d/ntpd /etc/init.d/ntpd.bak  
 cp /etc/sysconfig/ntpd /etc/sysconfig/ntpd.bak
 cp /etc/sysconfig/ntpdate /etc/sysconfig/ntpdate.bak

/etc/init.d/ntpd 可能会报不存在,忽略即可。

3. 卸载旧的ntp

yum erase ntp ntpdate

4. 创建/var/lib/ntp目录

install -v -m710 -o ntp -g ntp -d /var/lib/ntp

5. 配置(非必须要)

./configure \
--prefix=/usr \
--bindir=/usr/sbin \
--sysconfdir=/etc \
--enable-linuxcaps \
--with-lineeditlibs=readline \
--docdir=/usr/share/doc/ntp-4.2.8p15 \
--enable-all-clocks \
--enable-parse-clocks \
--enable-clockctl

6. 安装

make install && install -v -o ntp -g ntp -d /var/lib/ntp

7. 查看是否安装成功

ntpd相关可执行文件

8. 还原配置文件

cp /etc/init.d/ntpd.bak /etc/init.d/ntpd
cp /etc/sysconfig/ntpd.bak /etc/sysconfig/ntpd
cp /etc/sysconfig/ntpdate.bak /etc/sysconfig/ntpdate
mv /etc/ntp.bak /etc/ntp
cp /etc/ntp.conf.bak /etc/ntp.conf

9. 将ntpd加入systemctl服务

vim /usr/lib/systemd/system/ntpd.service

[Unit]
Description=Network Time Service
After=syslog.target ntpdate.service sntp.service
[Service]
Type=forking
EnvironmentFile=-/etc/sysconfig/ntpd
ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS
PrivateTmp=true
[Install]
WantedBy=multi-user.target

注意下,ExecStart=/usr/sbin/ntpd 文件路径。

10. 启动ntpd服务

重载系统服务:systemctl daemon-reload 设置开机启动:systemctl enable ntpd.service 启动服务:systemctl start ntpd.service

四、验证

systemctl status ntpd

● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled)
   Active: active (running) since 三 2023-02-15 20:37:57 CST; 43min ago
  Process: 177992 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 177994 (ntpd)
    Tasks: 2
   Memory: 1.0M
   CGroup: /system.slice/ntpd.service
           └─177994 /usr/sbin/ntpd -u ntp:ntp -g

ntpd --version

ntpd 4.2.8p15@1.3728-o Wed Feb 15 11:27:36 UTC 2023 (1)