Linux运维知识之Linux下NTP服务器搭建
小标 2018-12-07 来源 : 阅读 1240 评论 0

摘要:本文主要向大家介绍了Linux运维知识之Linux下NTP服务器搭建,通过具体的内容向大家展现,希望对大家学习Linux运维知识有所帮助。

本文主要向大家介绍了Linux运维知识之Linux下NTP服务器搭建,通过具体的内容向大家展现,希望对大家学习Linux运维知识有所帮助。

一、搭建准备

1、确定自己的时区

#date命令可以查看当前系统时间,中国的时区为CST。使用tzselect命令进行配置时区

         

配置文件的修改:/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、为了防止上层对时服务器故障,无法正确提供时间同步,一般建议配置两个或以上时间同步服务器。下面分别测试两个时钟服务是否正常可用

[root@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

[root@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 (//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

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注系统运维Linux频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程