NTP时钟同步方式说明
NTP在linux下有两种时钟同步方式,分别为直接同步和平滑同步:
直接同步
使用ntpdate命令进行同步,直接进行时间变更。如果服务器上存在一个12点运行的任务,当前服务器时间是13点,但标准时间时11点,使用此命令可能会造成任务重复执行。因此使用ntpdate同步可能会引发风险,因此该命令也多用于配置时钟同步服务时第一次同步时间时使用。
平滑同步
使用ntpd进行时钟同步,可以保证一个时间不经历两次,它每次同步时间的偏移量不会太陡,是慢慢来的,这正因为这样,ntpd平滑同步可能耗费的时间比较长。
标准时钟同步服务
>
Linux 系统安装配置NTP时间服务器。
一、关于NTP时间服务器:
NTP(The Network Time Protocol) 是网络时间协议,用以同步网络内计算机的时间。 它通过udp包交换,用特定算法进行协商,从而把计算机上的时间与时间服务器上的时间保持一致。通过互联网它支持的误差是10毫秒,局域网则可以达到200微秒。
NTP时间服务器分为多层,从0层到4层,每层依次与上一次服务器同步,最高层的服务器则直接连接一个高精度的时钟设备,比如原子钟、GPS或者电波时等。
怎样安装NTP Server:
Linux系统内核提供了对NTP的支持,因此只需要再安装一个NTP Server的守护进程即可。常用的ntpd由ntporg提供。
二、怎么确认linux系统有没有开启ntp服务?
service ntpd status 查看此服务有没有运行,设置:图形化界面--左上角时间---点击就可以知道。
ntpd服务运行时需要开启双向(服务器端和客户端) udp/123端口。
# yum install iptables iptables-services
# iptables -A INPUT -p udp -m udp --dport 123 -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT
# iptables -A OUTPUT -p udp -m udp --dport 123 -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT
# systemctl restart iptablesservice
# yum -y install firewalld
# firewall-cmd --zone=public --add-port=123/udp --permanent
# firewall-cmd --reload
What are the iptables rules to permit ntp
>
麒麟服务器离线下载下载ntp服务命令。
下载ntp的安装包,网址在官网搜索ntp:可以为其配置ntpf将其设置为自动同步某服务器时钟。我们继续执行安装命令,方式一用rpm64rpm45进行安装或者使用rpmivh和nodepsforce执行安装命令。安装之后试运行。
务端配置
1首先安装检查服务器是否安装了ntp、ntpdate
# rpm -qa | grep ntp
ntpdate-426p5-29el7centosx86_64
ntp-426p5-29el7centosx86_64
2如果没有,需要使用安装
# yum -y install ntp ntpdate
3修改ntp配置文件/etc/ntpconf
1)注释以下配置
# Use public servers from the poolntporg project
# Please consider joining the pool (>
#server 0centospoolntporg iburst
#server 1centospoolntporg iburst
#server 2centospoolntporg iburst
#server 3centospoolntporg iburst
2)新增如下配置
#日志文件
logfile /var/log/ntpdlog
#授权1721610网段上所有机器可以从这台机器上查询和时间同步
restrict 1721610 mask 2252252250 nomotify notrap
一,用ntpdate从时间服务器更新时间如果你的linux系统根本没有ntpdate这个命令
yum install ntp
安装完了之后,你不要做什么配置,也不需要,直接测试一下
[root@localhost ~]# ntpdate timenistgov
22 Oct 21:11:43 ntpdate[5014]: adjust time server 20720081113 offset -0018788 sec
如果出去上面的内容说明,同步成功了。然后在crontab里面加上以下内容。
/10 ntpdate timenistgov #域名或IP
每隔十分钟同步一次。推荐几个时间服务器。
timenistgov
timenurinet
asiapoolntporg
asiapoolntporg
asiapoolntporg
asiapoolntporg
二,用ntp搭建自己的时间服务器
上面我们是利用别人的时间服务器来同步时间,这些时间服务器都是比较权威的。当我们自己搭建时间服务器就不用crontab来定时去跑。
1,安装时间服务器ntp
yum install ntp
2,配置ntp
查看复制打印
[root@localhost ~]# cat /etc/ntpconf |awk '{if($0 !~ /^$/ && $0 !~ /^#/) {print $0}}'
restrict default ignore //默认不允许修改或者查询ntp,并且不接收特殊封包
restrict 127001 //给于本机所有权限
restrict 19216810 mask 2552552550 notrap nomodify //给于局域网机的机器有同步时间的权限
server timenistgov prefer //设置时间服务器,加prefer表示优先
server 0asiapoolntporg
server 1asiapoolntporg
server 2asiapoolntporg
server 12712710 # local clock
fudge 12712710 stratum 10
driftfile /var/lib/ntp/drift
keys /etc/ntp/keys
3,启动 ntp
[root@localhost ~]# /etc/initd/ntpd start
4,查看并测试
[root@localhost ~]# netstat -upnl |grep ntpd //查看时程
[root@localhost ~]# ntpq -pn //查看同步的服务器IP
remote refid st t when poll reach delay offset jitter
==============================================================================
5077217185 INIT 16 u - 64 0 0000 0000 0000
202901584 INIT 16 u - 64 0 0000 0000 0000
2027110089 INIT 16 u - 64 0 0000 0000 0000
202134110 INIT 16 u - 64 0 0000 0000 0000
12712710 LOCL 10 l 18 64 377 0000 0000 0001
[root@localhost ~]# ntpstat //同步的结果
synchronised to local net at stratum 11
time correct to within 12 ms
polling server every 512 s
remote:即NTP主机的IP或主机名称。注意最左边的符号,如果由“+”则代表目前正在作用钟的上层NTP,如果是“”则表示也有连上线,不过是作为次要联机的NTP主机。
refid:参考的上一层NTP主机的地址
st:即stratum阶层
when:几秒前曾做过时间同步更新的 *** 作
poll:下次更新在几秒之后
reach:已经向上层NTP服务器要求更新的次数
delay:网络传输过程钟延迟的时间
offset:时间补偿的结果
jitter:Linux系统时间与BIOS硬件时间的差异时间
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)