本地NTP 时间服务器

NTP简介

NTPNetworkTime Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议。它的用途是把计算机的时钟同步到世界协调时UTC,其精度在局域网内可达0.1ms,在互联网上绝大多数的地方其精度可以达到1-50ms

NTP服务器就是利用NTP协议提供时间同步服务的。

NTP服务器安装

查看系统自带ntp

[root@syaving ~]# rpm -qa ntpntp-4.2.6p5-5.el6.centos.x86_64

如果没有就安装

[root@syaving ~]# yum -y install ntp

配置NTP服务

[root@syaving ~]# vim /etc/ntp.conf# restrict default kod nomodify notrap nopeernoqueryrestrict default nomodify# nomodify客户端可以同步# 将默认时间同步源注释改用可用源# 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 iburstserver ntp1.aliyun.comserver time.nist.gov

启动NTP服务器

如果计划任务有时间同步,先注释,两种用法会冲突。

[root@syaving ~]# crontab -e# time sync by syaving at 2010-8-8#*/5 * * * * /usr/sbin/ntpdate time.nist.gov>/dev/null 2>&1
[root@syaving ~]# /etc/init.d/ntpd startStarting ntpd:                                            [  OK  ][root@syaving ~]# ntpq -p    remote           refid      st t when poll reach   delay  offset  jitter==============================================================================*ntp1.aliyun.com 10.137.38.86     2 u  22   64    1 525.885  -42.367   0.000[root@syaving ~]# ntpstatsynchronised to NTP server (110.75.186.247) atstratum 3   timecorrect to within 4257 ms   pollingserver every 64 s[root@syaving ~]# ntpdate 10.0.0.130 7 Dec18:43:07 ntpdate[26950]: the NTP socket is in use, exiting

客户机时间同步

客户机要等几分钟再与新启动的ntp服务器进行时间同步,否则会提示no server suitable for synchronizationfound错误。

[root@m01 ~]# # ntpdate 10.0.0.130 7 Dec18:40:16 ntpdate[1453]: step time server 10.0.0.130 offset 40.880807 sec

将命令放入计划任务即可。

注意:需要把之前的定时任务注释掉

[root@m01 ~]# crontab -e# time sync by syaving at 2010-8-8*/5 * * * * ntpdate 10.0.0.130 >/dev/null2>&1


本文出自 “宋某人c” 博客,请务必保留此出处http://syaving.blog.51cto.com/5614476/1869685

在 CentOS 7 系统中配置本地 NTP 时间服务器,可以通过安装和配置 `ntpd`(Network Time Protocol daemon)服务来实现。以下是详细的配置步骤: ### 安装 NTP 软件包 首先,确保系统中已安装 `ntp` 软件包。如果尚未安装,可以通过以下命令进行安装: ```bash yum install -y ntp ``` ### 配置 NTP 服务器 NTP 服务器的主要配置文件为 `/etc/ntp.conf`。需要根据本地网络环境和时间同步需求修改该文件。以下是一个典型的配置示例: ```bash # 允许的客户端网络范围 restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap # 使用本地时钟作为时间源(当外部时间源不可用时) server 127.127.1.0 fudge 127.127.1.0 stratum 10 # 外部 NTP 服务器(可选) 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 ``` - `restrict` 行用于定义允许哪些客户端访问该 NTP 服务器,并设置相应的访问控制权限。 - `server 127.127.1.0` 和 `fudge` 行表示如果外部 NTP 服务器不可用,本地服务器将使用其硬件时钟作为时间源,并设置其层级为 10(stratum 10)。 - `server` 行用于指定外部 NTP 服务器,以便与全球时间服务器保持同步。 ### 启动并启用 NTP 服务 配置完成后,需要启动 `ntpd` 服务,并将其设置为开机自启动: ```bash systemctl start ntpd systemctl enable ntpd ``` 如果需要重启服务以应用配置更改,可以使用以下命令: ```bash systemctl restart ntpd ``` ### 验证 NTP 服务状态 可以使用以下命令检查 NTP 服务的状态,确保其正常运行: ```bash systemctl status ntpd ``` 此外,可以通过 `ntpq -p` 命令查看 NTP 服务器与客户端之间的连接状态: ```bash ntpq -p ``` 该命令将显示所有与本地 NTP 服务器同步的客户端及其状态信息。 ### 客户端配置 在客户端上,同样需要安装 `ntp` 软件包,并修改 `/etc/ntp.conf` 文件,将 NTP 服务器指向本地配置的 NTP 服务器 IP 地址: ```bash server <NTP_SERVER_IP> iburst ``` 然后重启客户端的 `ntpd` 服务以应用更改: ```bash systemctl restart ntpd ``` ### 防火墙配置 确保服务器的防火墙允许 NTP 流量通过(默认使用 UDP 端口 123): ```bash firewall-cmd --permanent --add-service=ntp firewall-cmd --reload ``` ### 时间同步测试 在客户端上,可以使用 `ntpdate` 命令手动同步时间以测试 NTP 服务器是否正常工作: ```bash ntpdate <NTP_SERVER_IP> ``` ### 总结 通过上述步骤,可以在 CentOS 7 系统中成功配置一个本地 NTP 时间服务器,确保局域网内的设备能够保持时间同步,从而提高系统的稳定性和可靠性[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值