soul颠覆了执着

导航

NTP时间服务

时间服务


NTP:Network Time Protocol
作用:用来给其他主机提供时间同步服务

NTP的配置文件
/etc/ntp.conf

NTP相关的命令
date:显示/修改系统时间
hwclock:显示/修改硬件时间
ntpdate:客户端用于修改自己的时候ntp服务器同步
ntpd:ntp服务的主程序,也是客户端用于做时间同步的工具
ntpstat:查看ntp服务状态
ntpq -p:查看详细的时间同步状态信息


ntp.conf详解
1、restrict
作用:对ntp做权限控制
格式:restrict [ip] [mask] [par]
par:
ignore:忽略所有类型的NTP连接请求
nomodify:限制客户端不能使用命令ntpc和ntpq来修改服务器端的时间
noquery:不提供NTP网络校时服务
notrap:不接受远程登录请求
notrust:不接受没有经过认证的客户端的请求
【如果没有用任何参数,那么表示不做任何限制】


案例:只允许10.220.5.0/24进行网络时间同步
restrict 10.220.5.0 mask 255.255.255.0 nomodify

案例:只允许所有主机进行网络时间同步
restrict default nomodify

2. server
作用:指定ntp服务器的地址
格式:server [ip or hostname] [perfer]

3. fudge
作用:设置时间服务器的层级
格式:fudge ip [startnum int]
例子:fudge 10.225.5.1 startnum 10
注意:fudge必须和server一块用, 而且是在server的下一行
startnum
0~15
0:表示顶级
10:通常用于给局域网主机提供时间服务

 

案例:将当前主机作为时间服务器

server 127.127.1.0
fudge 127.127.1.0 startnum 10
restrict 127.0.0.1
restrict 10.220.5.0 netmask 255.255.255.0

安装NTP Server(服务端 主机:10.220.5.63)

一、安装ntp

[root@63 ~]# yum install ntp -y

二、修改ntp的配置文件

[root@63 ~]# cp /etc/ntp.conf{,.bak}
[root@63 ~]# vim /etc/ntp.conf
[root@63 ~]# vim /etc/ntp.conf
driftfile /var/lib/ntp/drift
logfile /var/log/ntp/ntp.log
server 127.127.1.0
fudge 127.127.1.0 startnum 10
restrict 127.0.0.1
restrict 10.220.5.0 netmask 255.255.255.0

 三,创建问文件

[root@63~]# mkdir /var/lib/ntp/
[root@63 ~]# touch /var/lib/ntp/ntp.log

四、启动服务(centos7)

[root@63 ~]# systemctl start ntpd
[root@63~]# systemctl enable ntpd  <<<开机自启

查看ntp的状态(第一种方法

[root@63 ~]# ntpstat
synchronised to local net at stratum 6 
   time correct to within 11 ms
   polling server every 64 s
注意:synchronised表示时间同步完成(ntp可以正常工作了 unsynchronised表示时间同步尚未完成 如果第一次启动查看状态是
unsynchronised,需要等几分钟,再次查看状态

 第二种方法

[root@63 ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*LOCAL(0)        .LOCL.           5 l   16   64  377    0.000    0.000   0.000

参数说明:

 

*:表示当前主机当做上游服务器工作
+表示当前主机当做备用服务器
.LOCL.表示基于当前主机做时间服务的
ip 表示上游服务器的地址
when:表示几秒钟前做过时间同步
poll表示下一次同步在几秒之后
reach表示向上游服务器发送请求的次数
delay表示传输报文时间延迟
offset 表示延迟补偿
jitter表示系统时间和软件时间差

六,关闭防火墙

 

[root@63 ~]# systemctl stop firewalld
[root@63~]# systemctl disable firewalld

 

 安装NTP client(10.220.5.64)

一、安装

[root@64~]# yum install ntp ntpdate -y

二、修改ntp的配置文件

[root@64 ~]# vim /etc/ntp.conf

server 10.220.5.63 <<<服务端ip
fudge 10.220.5.1 startnum 10
restrict 127.0.0.1
statsdir /var/log/ntp
logfile /var/log/ntp/ntp.log

三、创建日志文件

[root@64r ~]# mkdir /var/log/ntp
[root@64r ~]#touch /var/log/ntp/ntp.log

四、关闭防火墙

 同上

五、先执行一次ntpdate时间同步

[root@64 ~]# ntpdate 10.220.5.63
 1 May 23:25:16 ntpdate[1468]: the NTP socket is in use, exiting

六、启动ntpd

[root@64~]# systemctl start ntpd

七、检查状态

[root@64 ~]# ntpstat
synchronised to NTP server (10.220.5.63) at stratum 7 
   time correct to within 32 ms
   polling server every 256 s  表示同步完成

######################################

实现客户端的时间同步

基于ntpdate
方案:cron+ntpdate
优点:
实现简单
缺点:
会导致客户端的时间不连续,对于数据库业务影响是比较大,生产环境尽量不要用

基于ntpd
方案:
ntpdate+ntpd
优点:
可以实现顺滑的时间同步
缺点:
时间同步往往不会立刻完成
ntpd
要求该ntpd节点和上游服务器的时间相差不同太大

#############################################

 

 

 

posted on 2018-11-09 17:25  soul颠覆了执着  阅读(497)  评论(0编辑  收藏  举报