欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > Centos 7离线安装ntpd服务

Centos 7离线安装ntpd服务

2025/2/23 0:39:58 来源:https://blog.csdn.net/subfate/article/details/143576751  浏览:    关键词:Centos 7离线安装ntpd服务

本文涉及一次Centos 7系统中离线安装ntpd对时服务的过程,其目的是为了在服务器运行过程中能够实时同步时间。

问题提出

某服务器需部署业务程序,这些程序的部署脚本是我初创的,后因其它事转交给其他人,后再因其它事又兜兜转转到了我头上。是自己的锅总是自己背。部署后,发现程序有些异常,看日期是前几天的。询问之下得知,服务器没有安装ntp,逐安装之,于是有了本文。

实践过程

下载包

下载ntp的离线包,地址:

https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/

服务器系统版本为7.6,但该版本在阿里云上没有包,而最早版本的包是7.9的,因此选择该版本。对于很多年前的系统版本,现在的源越来越少了,应珍惜当前还能下载的机会。

下载的文件有3个,具体为:

ntp-4.2.6p5-29.el7.centos.2.x86_64.rpm
ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm
autogen-libopts-5.18-5.el7.x86_64.rpm

安装过程

下载后,上传服务器,切换root权限,执行以下命令安装:

# rpm -ivh ntp-4.2.6p5-29.el7.centos.2.x86_64.rpm ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm autogen-libopts-5.18-5.el7.x86_64.rpm

安装后,会自动设置/etc/passwd/etc/group,添加ntp用户和组。执行如下命令确认:

# cat /etc/group | grep ntp
ntp:x:38:
# cat /etc/passwd | grep ntp
ntp:x:38:38::/etc/ntp:/sbin/nologin

另外,也会自动安装配置文件/etc/ntp.conf和ntp服务配置文件/usr/lib/systemd/system/ntpd.service

由于服务器只能在内网运行,因此设置内网ntp服务器。修改/etc/ntp.conf文件,将原来的server 0.centos.pool.ntp.org iburst等注释掉,改为server <ntp服务IP>。如果服务器连接互联网,则无需修改。

启动ntp服务:

# systemctl start ntpd

查看服务状态:

# systemctl status ntpd
● ntpd.service - Network Time ServiceLoaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)Active: active (running) since Wed 2024-11-03 04:27:31 EST; 1s agoProcess: 22144 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)Main PID: 22145 (ntpd)Tasks: 1Memory: 876.0KCGroup: /system.slice/ntpd.service└─22145 /usr/sbin/ntpd -u ntp:ntp -g

设置开机自启动:

# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.

测试验证

查看当前时间:

# date
Wed Nov  3 04:36:30 EST 2024

时间不是当前的,查看时区文件:

# ls /etc/localtime  -hl
lrwxrwxrwx. 1 root root 38 Nov  4 22:32 /etc/localtime -> ../usr/share/zoneinfo/America/New_York

发现时区是New_York的,不知道厂商在安装系统时不选时区还是出现其它什么原因,总之要修改成我国时间,命令如下:

# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime  -f

修改后,时间是正常的了,如下:

# date
Wed Nov  3 17:37:08 CST 2024

小结

系统是厂商安装的,ntpd没有安装,对业务程序有很大影响,咨询了现场同事,回复说没有在合同时规定要安装ntpd,也没规定要改时区。个人认为,对于此类事,应加入到服务器检查列表中。不过限于业务范围和水平,此事也只是建议而已,该救火时还得救火。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词