中易网

如何在CentOS中搭建NTP服务器

答案:3  悬赏:0  
解决时间 2021-03-10 03:07
如何在CentOS中搭建NTP服务器
最佳答案
一、搭建时间服务器
1、在一台linux服务器安装ntp server

tar zxvf ntp-4.2.6.tar.gz
cd ntp-4.2.6
./configure --prefix=/usr/local/ntp --enable-all-clocks --enable-parse-clocks
make && make install
2、修改ntp.conf配置文件
vi /etc/ntp.conf
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
#restrict default kod nomodify notrap nopeer noquery
restrict default nomodify
(允许任何IP的客户机都可以进行时间同步,如果是只允许某个网段的客户机进行时间同步可以这样写
restrict 10.58.26.0 mask 255.255.255.0 nomodify)

restrict -6 default kod nomodify notrap nopeer noquery
# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict -6 ::1
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool
#server 0.rhel.pool.ntp.org(默认时间服务器)
#server 1.rhel.pool.ntp.org(默认时间服务器)
#server 2.rhel.pool.ntp.org(默认时间服务器)
server 10.128.14.25 (手工设置的时间服务器)
(如果是可以直连外网,可以使用LINUX默认提供的三组标准时间服务器,否则可以自己指定一个同步时间源)

#broadcast 192.168.1.255 key 42 # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 key 42 # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 key 42 # manycast client
# Undisciplined Local Clock. This is a fake driver intended for backup
# and when no outside source of synchronized time is available.
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10

3、以守护进程启动ntpd
#/etc/rc.d/init.d/ntpd -c /etc/ntp.conf -p /tmp/ntpd.pid
#/etc/rc.d/init.d/ntpd start
#ps -ef|grep ntpd
4、在ntp server上启动ntp服务后,ntp server自身或者与其server的同步的需要一个时间段,这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令进行同步时会产生no server suitable for synchronization found的错误。
那么如何知道何时ntp server完成了和自身同步的过程呢?
在ntp server上使用命令:
# watch ntpq -p
出现如下画面:

注意LOCAL的这个就是与自身同步的ntp server。
注意reach这个值,在启动ntp server服务后,这个值就从0开始不断增加,当增加到17的时候,从0到17是5次的变更,每一次是poll的值的秒数,是64秒*5=320秒的时间。

二、配置时间同步客户机
vi /var/spool/cron/root(或crontab -e)
增加一行,在每天的1点10分、9点10分、17点10分与时间同步服务器进行同步并写入BIOS

10 1 ,9,17* * * root /usr/sbin/ntpdate 10.128.14.25; /sbin/hwclock -w
如果同步不正常,可以加输出日志或看系统日志
输出日志的方法:
10 1 ,9,17* * * root /usr/sbin/ntpdate 10.128.14.25>>/tmp/1.txt; /sbin/hwclock -w
在1.txt中可查看时间同步时的输出结果。
或者看/var/mail/root系统日志
Subject: Cron /usr/sbin/ntpdate 10.128.14.25;/sbin/hwclock -w
X-Cron-Env:
X-Cron-Env:
X-Cron-Env:
X-Cron-Env:
X-Cron-Env:
Message-Id: <[email protected]>
Date: Tue, 27 Nov 2012 18:30:01 +0800 (CST)
27 Nov 18:29:59 ntpdate[6917]: step time server 10.128.14.25 offset -1.361968 sec
可以看到同步成功了,如果未成功会报出错误。

三、无法同步的问题
检查ntp server主机的防火墙。可能是ntp server的防火墙屏蔽了upd 123端口。
可以用命令
#service iptables stop
全部回答
在CentOS 7中,我们可以使用timedatectl命令来查看和更改服务器的时区(比如“Australia/Adelaide”)。
# timedatectl list-timezones | grep Australia
# timedatectl set-timezone Australia/Adelaide
# timedatectl
如何在CentOS中搭建NTP服务器?
接下来使用yum,安装必要的软件。
# yum install ntp
然后,我们将添加用来同步时间的全局NTP服务器。
# vim /etc/ntp.conf
server 0.oceania.pool.ntp.org
server 1.oceania.pool.ntp.org
server 2.oceania.pool.ntp.org
server 3.oceania.pool.ntp.org
默认情况下,NTP服务器日志保存在/var/log/messages中。如果你想使用自定义的日志文件,也可以指定该文件的位置。
logfile /var/log/ntpd.log
如果你果真选择使用自定义的日志文件,确保更改其所有权和SELinux上下文。
# chown ntp:ntp /var/log/ntpd.log
# chcon -t ntpd_log_t /var/log/ntpd.log
现在启动NTP服务,确保它已添加到启动项。
# systemctl restart ntp
# systemctl enable ntp
验证NTP服务器时钟
我们可以使用ntpq命令,检查本地服务器的时钟如何通过NTP实现同步。
ntp服务器的问题可以咨询泰福特
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
关于论坛后台的管理员查ip的问题
javaweb项目绑定域名后,使用域名+项目名, 可
两个公司同时录了我,都是同一个行业的公司,
啄木鸟健康漆地址在什么地方,想过去办事
繁殖母羊小尾寒羊一天干草和精料和喂多少!发
下雨车停在低处会不会发动机进水
潍坊哪里的瑜伽会所比较好?费用大概是多少?
小新700 i5-6300hq 8g 500g+128固态 4g w10 1
2003年12月23日,重庆川东北某天然气井发生井
金蓓蕾幼儿园(乌兰察布商都县)怎么去啊,我要
什么是单反钻和足反钻
和老公那个的时候,我觉得很羞,脸热,脸红的
劳动就业大厦地址有知道的么?有点事想过去
100.0Mpts的网速有多快,看不懂
女主叫宋暖的exo橙光游戏
推荐资讯
分析一下600570 连续好几天尾盘几分钟都会拉
网上有很多《张天师符咒大全》不知什么样的《
十代思域停车熄火后需要手动拉起电子手刹吗?
MG牌的车子怎么样?
红米note5系统更新总是显示从新启动
南榆木家具比老榆木家具差在哪里?南榆木家具
东北林业大学会计二专业容易拿到证吗
造价新手,菜鸟求教,清单项怎么确定套什么定
公务员不到年限辞职可以报考事业编制吗
长岭冰箱BCD218,温度补偿问题。
小红萝卜是什么萝卜
ios8延时拍照前摄像头的连拍怎么关
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?