NTP 网络时间服务

NTP 网络时间服务

//常见问题:
服务搭建可用后重启设备,即使服务可以正常运行,但是同步会报错。
没有好的解决办法,只能重启服务/防火墙 就可以正确同步。

1. NTP时钟同步方式说明

NTP在linux下有两种时钟同步方式,分别为直接同步和平滑同步:

直接同步

使用ntpdate命令进行同步,直接进行时间变更。如果服务器上存在一个12点运行的任务,当前服务器时间是13点,但标准时间时11点,使用此命令可能会造成任务重复执行。因此使用ntpdate同步可能会引发风险,因此该命令也多用于配置时钟同步服务时第一次同步时间时使用。

平滑同步

使用ntpd进行时钟同步,可以保证一个时间不经历两次,它每次同步时间的偏移量不会太陡,是慢慢来的,这正因为这样,ntpd平滑同步可能耗费的时间比较长。

NTP默认使用的端口号
B UDP 123


标准时钟同步服务

http://www.pool.ntp.org/zone/cn网站包含全球的标准时间同步服务,也包括对中国时间的同步,对应的URL为cn.pool.ntp.org,在其中也描述了ntp配置文件中的建议写法:
server 1.cn.pool.ntp.org
server 3.asia.pool.ntp.org
server 2.asia.pool.ntp.o

配置

配置前先使用命令:
ntpdate -u cn.pool.ntp.org,同步服务器

修改/etc/ntp.conf文件
restrict //约束

restrict 172.16.248.0 mask 255.255.255.0 nomodify notrap
//允许172.16.248.0网段的设备进行时间同步。


 Restrict default nomodify
权限的设定主要以restrict这个参数来设定,主要的语法为:
restrict  IP地址  mask 子网掩码 参数
其中IP可以是IP地址也可以是default,default是指所有的IP
----------------
参数主要有以下几个:
ignore :关闭所有的 NTP 联机服务
nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
notrust:客户端除非通过认证,否则该客户端来源将被视为不信任子网
noquery:不提供客户端的时间查询
----------------

开机启动服务
systemctl enable ntpd

启动服务
systemctl start ntpd

启动后,查看同步情况

# ntpq -p

    [root@localhost ~]# ntpq -p
         remote refid st t when poll reach delay offset jitter
    ==============================================================================
     dns1.synet.edu. .INIT. 16 u - 1024 0 0.000 0.000 0.000
    *time.ume.tubita .PPS. 1 u 107 128 377 397.503 -24.042 0.538
    -211.233.84.186 204.123.2.5 2 u 100 128 377 124.228 24.973 0.524
    +dns.sjtu.edu.cn 79.213.241.147 3 u 94 128 377 20.878 3.339 0.512
    +Hshh.org 216.218.192.202 2 u 45 128 377 31.205 10.731 1.278
     dns2.synet.edu. .INIT. 16 u - 1024 0 0.000 0.000 0.000

说明:
*表示目前使用的ntp server,这里选择的本机;
st:即stratum阶层,值越小表示ntp serve的精准度越高;
when:几秒前曾做过时间同步更新的操作;
Poll表示,每隔多少毫秒与ntp server同步一次;
reach:已经向上层NTP服务器要求更新的次数;
delay:网络传输过程钟延迟的时间;
offset:时间补偿的结果;
jitter:Linux系统时间与BIOS硬件时间的差异时间
注意:NTP服务端重启后,客户机要等5分钟再与其进行时间同步,否则会提示“no server suitable for synchronization found”错误。等待的时间可以通过命令 watch ntpq -p来监控。

# ntpstat

ntpstat 命令查看时间同步状态,这个一般需要5-10分钟后才能成功连接和同步。所以,服务器启动后需要稍等下。

刚启动的时候,一般是:

/# ntpstat
unsynchronised
time server re-starting
polling server every 64 s
连接并同步后:
synchronised to NTP server (202.120.2.101) at stratum 4
time correct to within 1192 ms
polling server every 64 s

客户端
修改配置文件/etc/ntp.conf
server 172.16.248.1 //指定ntp时间服务器。
配置NTP开机启动

时间同步的命令

直接修改系统时间
date -s “2017/8/23 22:22:00”
将当前时间和日期写入BIOS,避免重启后失效
命令 : “hwclock -w”
ntpdate命令
ntpdate -u 10.10.106.156 进行时间同步-强制更新(会出现时间断层或重复)
ntpdate -d 10.10.106.156 查看进行时间同步会产生的影响(不生效)

服务端配置文件


# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

driftfile /var/lib/ntp/drift

# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default nomodify //给所有网段的设备分配地址

# 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 ::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 (http://www.pool.ntp.org/join.html).
server cn.pool.ntp.org //指定时间服务器的地址

#broadcast 192.168.1.255 autokey        # broadcast server
#broadcastclient                        # broadcast client
#broadcast 224.0.1.1 autokey            # multicast server
#multicastclient 224.0.1.1              # multicast client
#manycastserver 239.255.254.254         # manycast server
#manycastclient 239.255.254.254 autokey # manycast client

# Enable public key cryptography.
#crypto

includefile /etc/ntp/crypto/pw

# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography. 
keys /etc/ntp/keys

# Specify the key identifiers which are trusted.
#trustedkey 4 8 42

# Specify the key identifier to use with the ntpdc utility.
#requestkey 8

# Specify the key identifier to use with the ntpq utility.
#controlkey 8

# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats

# Disable the monitoring facility to prevent amplification attacks using ntpdc
# monlist command when default restrict does not include the noquery flag. See
# CVE-2013-5211 for more details.
# Note: Monitoring will not be disabled with the limited restriction flag.
disable monitor
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值