升级ntpd到4.2.8p17,ntp编译报recompile with -fPIC错误解决

该文描述了如何检查和升级NTP服务到4.2.8p17以解决安全问题。首先,检查当前版本,然后下载安装包并安装依赖。接着,备份配置文件,移除旧版NTP,解压并编译新版本,处理编译时的-fPIC选项问题。最后,生成systemd配置文件,还原配置,启动服务,并验证升级后的版本。
摘要由CSDN通过智能技术生成

背景

低版本的ntp会有漏洞,需要升级到最新版本4.2.8p17

检查ntpd版本

/usr/sbin/ntpd --version

下载部署包

https://archive.ntp.org/ntp4/ntp-4.2/ntp-4.2.8p17.tar.gz

安装依赖包

每个文档的都不一样,这里索性都安装

yum install gcc gcc-c++ openssl-devel libstdc++* libcap* m4 autoconf libtool gcc gcc-c++ ncurses-devel cmake make gcc libcap libcap-devel glic-devel automake libevent*

备份原有配置文件

scp -r /etc/ntp /etc/ntp.bak
scp -r /etc/ntp.conf /etc/ntp.conf.bak
scp -r /etc/init.d/ntpd /etc/init.d/ntpd.bak
scp -r /etc/sysconfig/ntpd /etc/sysconfig/ntpd.bak
scp -r /etc/sysconfig/ntpdate /etc/sysconfig/ntpdate.bak
scp -r /usr/sbin/ntpd /usr/sbin/ntpd.bak

移除ntp

yum remove ntp ntpdate -y

解压安装包

tar -zxvf ntp-4.2.8p17.tar.gz

cd ntp-4.2.8p17/
install -v -m710 -o ntp -g ntp -d /var/lib/ntp

./configure --prefix=/usr --bindir=/usr/sbin --enable-all-clocks --enable-parse-clocks --docdir=/usr/share/doc/ntp-4.2.8p17 --enable-all-clocks --enable-parse-clocks --enable-clockctl CFLAGS="-fPIC"


make && make install

#注意此处CFLAGS=“-fPIC”,p15不需要这个参数,但是p17要有
否则会报下面错误

/usr/bin/ld: ./libevent/.libs/libevent_core.a(evutil.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: ./libevent/.libs/libevent_core.a(evutil_rand.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: ./libevent/.libs/libevent_core.a(log.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: ./libevent/.libs/libevent_core.a(select.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: ./libevent/.libs/libevent_core.a(poll.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: ./libevent/.libs/libevent_core.a(epoll.o): relocation R_X86_64_32S against `.rodata.epollops_changelist' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: ./libevent/.libs/libevent_core.a(signal.o): relocation R_X86_64_32 against `.bss.signals.6714' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: ./libevent/.libs/libevent_pthreads.a(evthread_pthread.o): relocation R_X86_64_PC32 against symbol `pthread_self@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status
make[4]: *** [sntp] Error 1
make[4]: Leaving directory `/root/ntp-4.2.8p17/sntp'
make[3]: *** [all-recursive] Error 1
make[3]: Leaving directory `/root/ntp-4.2.8p17/sntp'
make[2]: *** [all] Error 2
make[2]: Leaving directory `/root/ntp-4.2.8p17/sntp'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/root/ntp-4.2.8p17'

生成systemd管理配置文件

cat  > /usr/lib/systemd/system/ntpd.service << EOF
[Unit]
Description=Network Time Service
After=syslog.target ntpdate.service sntp.service

[Service]
Type=forking
EnvironmentFile=-/etc/sysconfig/ntpd
ExecStart=/usr/sbin/ntpd -u root:root \$OPTIONS
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

还原配置文件

scp  -r /etc/sysconfig/ntpd.bak /etc/sysconfig/ntpd
scp  -r /etc/sysconfig/ntpdate.bak /etc/sysconfig/ntpdate
scp  -r /etc/ntp.bak /etc/ntp
scp  -r /etc/ntp.conf.bak /etc/ntp.conf

启动服务

systemctl daemon-reload
systemctl restart ntpd
systemctl status ntpd

检查ntpd版本

/usr/sbin/ntpd --version
date
`ntpd` 是一个用于网络时间协议(Network Time Protocol,NTP)的标准开源时间服务器客户端程序。版本 4.2.8 中,如果你想让 `ntpd` 同步时间成功后在日志中记录相关信息,你可以通过修改它的配置文件 `ntp.conf` 来设置。 1. 首先,打开 `ntp.conf` 文件,通常位于 `/etc/ntp.conf` 或类似路径下。 2. 添加或修改 `logfile` 和 `logfile-format` 行,指定日志文件的位置和内容格式。例如: ``` logfile /var/log/ntpd.log logfile-format "%t %I %f %l %v %s %z %e %u %D" ``` 其中: - `%t`:日期时间戳 - `%I`:主IP地址 - `%f`:服务名(通常是 "ntpd") - `%l`:线程ID - `%v`:事件级别(比如 "stratum 2") - `%s`:系统时钟偏移量(秒) - `%z`:时区 - `%e`:错误码(如果有的话) - `%u`:用户ID - `%D`:日志描述信息 3. 对于同步前后的具体时间和server IP,`logfile-format` 可能不会直接提供这些信息,你需要在其他地方或者自定义脚本中捕获这些数据。当同步完成时,可以创建一个自定义的服务脚本(如 `/etc/service/ntpd` 或 `/usr/local/bin/ntpd-sync-check.sh`),这个脚本会在同步后运行并提取必要的信息,然后将其写入日志或发送到监控系统。 4. 确保 `ntpd` 有适当的权限去写入日志文件,并启动或重启 `ntpd` 以便应用新的配置。 如果你需要实时的日志记录,可能还需要设置 `ntpd` 的日志级别,比如将它设置为 `debug` 或更高,这会生成更多的详细信息。但是请注意过多的日志可能会对性能造成影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值