设为首页 收藏本站
查看: 719|回复: 0

[经验分享] Linux下NTP服务器搭建

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-12-29 14:58:04 | 显示全部楼层 |阅读模式
一、搭建准备
1、确定自己的时区
#date命令可以查看当前系统时间,中国的时区为CST。使用tzselect命令进行配置时区
          spacer.jpg
配置文件的修改:/etc/sysconfig/clock
2、检查NTP服务程序
   #rpm -qa |grep ntp

    fontpackages-filesystem-1.41-1.1.el6.noarch
    ntpdate-4.2.4p8-3.el6.x86_64
如果没有,则需要安装ntp程序
#yum -y install ntp ntpdate        
需要配置好yum源
3、为了防止上层对时服务器故障,无法正确提供时间同步,一般建议配置两个或以上时间同步服务器。下面分别测试两个时钟服务是否正常可用
[iyunv@five yum.repos.d]# ntpdate -q 1.cn.pool.ntp.org
server 202.118.1.81, stratum 2, offset 1.100833, delay 0.04306
server 202.112.29.82, stratum 2, offset 1.140816, delay 0.12634
server 223.112.179.133, stratum 2, offset 1.105848, delay 0.06050
28 Dec 15:52:32 ntpdate[40722]: step time server 202.118.1.81 offset 1.100833 sec
[iyunv@five yum.repos.d]# ntpdate -q 2.cn.pool.ntp.org
server 223.112.179.133, stratum 2, offset 1.107213, delay 0.06606
server 202.118.1.81, stratum 2, offset 1.101250, delay 0.04173
server 202.112.29.82, stratum 2, offset 1.152465, delay 0.14330
server 202.112.31.197, stratum 2, offset 1.142942, delay 0.13098
28 Dec 15:52:47 ntpdate[40723]: step time server 202.118.1.81 offset 1.101250 sec
如上所示,就是正常的,如果上层服务器不可用,一般如下显示
1.server 10.32.196.60, stratum 0, offset 0.000000, delay 0.00000
2.26 Oct 15:01:17 ntpdate[23338]: no server suitable for synchronization found
二、配置NTP配置文件(配置文件:/etc/ntp.conf)
1、指定上层对时服务器
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 0.rhel.pool.ntp.org
server 1.rhel.pool.ntp.org
server 2.rhel.pool.ntp.org
将其改为刚才测试的国家对时服务器,即:
server    1.cn.pool.ntp.org    prefer
server    2.cn.pool.ntp.org
##prefer 表示优先使用

2、设置对内网主机提供NTP服务

# 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    ##NTP服务默认拒绝所有NTP连线
            ##nomodify 用户端不能更改NTP服务端的时间参数

            ##notrap 不提供trap 这个远程事件登录功能
            ##nopeer 避免其他人使用此ntpd作为time server
            ##noquery 用户端不能使用ntpq,ntpc等指令来查询时间
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   
        ##允许192.168.10.0/24 网段的主机来进行对时,但不允许客户端来修改,登录我的NTP服务器
&&允许任何IP都可以进行时间同步(此项不可与其默认选项重复)
restrict default nomodify
&&将内网地址添加进去
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
restrict 192.168.10.0 mask 255.255.255.0 nomodify notrap

3、设置同步更新本地hwclock

在Linux下系统时间在开机的时候会和硬件时间同步(synchronization),之后也就各自独立运行了那么既然两个时钟独自运行,那么时间久了必然就会产生误差了,而NTP默认又只更新系统时间,因此我们需要设置硬件时钟进行同步调整
1.# vim /etc/sysconfig/ntpd
2.# Drop root to id 'ntp:ntp' by default.
3.OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"
4.SYNC_HWCLOCK="yes"
添加SYNC_HWCLOCK="yes"
4、设置同步更新本地hwclock
在Linux下系统时间在开机的时候会和硬件时间同步(synchronization),之后也就各自独立运行了那么既然两个时钟独自运行,那么时间久了必然就会产生误差了,而NTP默认又只更新系统时间,因此我们需要设置硬件时钟进行同步调整
# vim /etc/sysconfig/ntpd
# Drop root to id 'ntp:ntp' by default.
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"
SYNC_HWCLOCK="yes"        ##添加SYNC_HWCLOCK="yes"
5、、启动服务
# chkconfig --level 345 ntpd on        ##添加为服务
# service ntpd start
Starting ntpd:                                             [  OK  ]
***6、对配置文件其他项进行说明:

driftfile /etc/ntp/drift        ##记录上次的NTP server与上层NTP server联接所花费的时间
restrict default ignore    ##设置默认策略为允许任何主机进行时间同步
fudge 127.127.1.1 stratum 10    ##指定阶层编号为10,降低其优先度。
statsdir /var/log/ntp/    ##设置ntp日志的path
logfile /var/log/ntp/ntp.log    ##设置ntp日志文件
restrict 192.168.10.2  ## 指定某台机器时间同步

restrict 192.168.0.0 mask 255.255.255.0     ##允许192.168.0.0/254子网内主机时间同步
restrict 0.0.0.0 mask 0.0.0.0 nomodify notrap     ##允许任何主机跟进行时间同步

三、维护相关
1、确认端口监听启动
ntpd启动 123 端口 通过UDP协议对外提供服务
# netstat -nlup |grep ntpd
udp        0      0 192.168.1.114:123           0.0.0.0:*                               40967/ntpd         
udp        0      0 127.0.0.1:123               0.0.0.0:*                               40967/ntpd         
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               40967/ntpd         
udp        0      0 ::1:123                     :::*                                    40967/ntpd         
udp        0      0 fe80::20c:29ff:fef0:4168:123 :::*                                    40967/ntpd         
udp        0      0 :::123                      :::*                                    40967/ntpd
2、确认已进行时间同步(&&另注意防火墙问题)
# ntpstat
    synchronised to NTP server (202.112.29.82) at stratum 3
    time correct to within 1030 ms
    polling server every 64 s
3、查看与上层服务器连接情况
# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
===========================================================
223.112.179.133 61.160.246.234   2 u   11   64   47   46.430   12.675  22.285
*dns1.synet.edu. 202.118.1.47     2 u    5   64   77  106.024   53.636  32.323
    remote 远程主机的主机名或IP
        ##*目前正在使用的上层NTP
        ##+已连线,可提供时间更新的候补服务器
    refid 上级NTP的IP
    st 就是stratum 上层NTP的层级
    when 几秒钟前曾做过时间同步更新
    poll 下一次更新在几秒后
    reach 已经向上层服务器要求更新的次数
    delay 网络传输过程中的延迟时间
    jitter linux 系统时间与bios硬件时钟之间的差异
4、管理命令
ntpq –p    #查看本机和上层服务器的时间同步结果
ntptrace    #可以用來追踪某台时间服务器的时间对应关系
ntpdate IP  #客户端要和NTP server进行时钟同步。
/var/log/ntp/ntp.log  #查看ntp日志
四、Linux配置客户端
1、测试同步:
    #ntpdate ip地址
2、设置自动同步:
    #echo “00 */1 * * * root /usr/sbin/ntpdate 192.168.0.240;/sbin/hwclock -w”>>/etc/crontab
        ##每小时同NTP server进行一次时钟同步,并写入本机BIOS



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-321112-1-1.html 上篇帖子: Linux基础之vim编辑器 下篇帖子: Linux手动添加用户 服务器 Linux
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表