【Linux】NTP(网络时间协议)

计算机内部所记录的时钟是记载于BIOS(CMOS)内的,但如果计算机上面的CMOS电池没电了,或者是某些特殊因素导致BIOS数据被清除,此时计算机的时间就会不准。对于处于在网络当中的主机,如何保证主机随时保持正确的时间信息呢?这就需要用到NTP服务器。

NTP是网络时间协议(Network Time Protocol),它是用来同步网络中各个计算机的时间的协议。

NTP要提供准确的时间,就必须有准确的时间来源,那可以用格林尼治时间(GMT)吗?答案是否定的。因为格林尼治时间是以地球自转为基础的时间计量系统,但是地球每天的自转是有些不规则的,因此,格林尼治时间已经不再被作为标准时间使用。

新的标准时间,在计算时间的时候,最准确的是使用原子震荡周期所计算的物理时钟(atomic clock,也称原子钟),这也被定义为标准时间。UTC(Universal Time Coordinated,世界协调时)就是利用这种原子钟所定义出来的时间。

为什么linux的发行版里面定义时区没有北京时间?因为在1949年之前,我国一共分了5个时区,以哈尔滨、上海、重庆、乌鲁木齐和喀什为代表分别是长白时区,中原标准时区,陇蜀时区,新藏时区和昆仑时区。/usr/share/zoneinfo/Asia

Asia/Harbin

Asia/Shanghai

Asia/Chongqing

Asia/Urumqi

Asia/Kashgar

这五个时区的时间现在均为北京时间,即中国标准时间。

北京时间是中国采用国际时区东八时区作为标准时间。北京时间并不是北京的地方时间,而是东经120°的地方时间,所以北京时间=UTC+8。由于中国地域辽阔,东西相跨5个时区(东五区、东六区、东七区、东八区、东九区5个时区),授时台必须建立在地理中心地带,所以授时台不在北京,而在陕西蒲成。


查看当前的时间

[root@localhost ~]# date -R

Fri, 29 Mar 2019 09:41:17 +0800

查看所有时区:

[root@localhost ~]# ls /usr/share/zoneinfo/

查看其他时区的当前时间

[root@web ~]# zdump Hongkong

更改时区:

方法一、

1、使用tzselect命令查询需要的时区,按照提示操作

2、添加变量到~/.bash_profile文件中,即追加类似的内容:

TZ='America/Mexico_City'; export TZ

3、重新连接一个新会话,使用date -R查看

方法二、使用timedatectl命令更改时区

[root@localhost ~]# timedatectl   查看当前时区信息

[root@localhost ~]# timedatectl list-timezones  列出所有的时区

设置时区

[root@localhost ~]# timedatectl set-timezone Asia/Chongqing

[root@localhost ~]# ll /etc/localtime

lrwxrwxrwx. 1 root root 36 Mar 31 14:15 /etc/localtime -> ../usr/share/zoneinfo/Asia/Chongqing

[root@localhost ~]# timedatectl

设置时间

[root@localhost ~]# teamdctl set-time '2019-03-03 10:41:00'


 NTP服务器的设置

如何让时间同步化,可以先选几部主要的主机校准时间,让这些主要的主机时间同步后,再开放网络服务来让client端连接,并且允许client端调整自己的时间,就可以实现全部计算机的时间同步了。

ntp服务使用的是udp的123端口

安装软件:

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

/etc/ntp.conf配置文件

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

driftfile /var/lib/ntp/drift    使用driftfile记录时间差异。由于默认的NTP服务器本身的时间计算是依据BIOS的芯片振荡周期频率来计算的,但是这个数值与时间服务器不一定一样,所以该主机会自动计算自己主机的频率和上层时间服务器的频率差值,并将该值记录在/var/lib/ntp/drift这个文件里面。

注:driftfile后面指定的文件需要使用完整路径文件名;该文件不能是链接文件;该文件记录的数值单位为百万分之一秒。


restrict default nomodify notrap nopeer noquery

使用restrict来控制权限,参数设置方式为:

restrict

 [你的ip地址]

 mask

[255.255.255.0]

parameter 

parameter的参数如下:

ignore:拒绝所有类型的连接

nomodify:客户端不能使用ntpc与ntpq这两个程序来修改服务器的时间参数,但客户端可通过该主机来进行网络校时

noquery:客户端不能够使用ntpc、ntpq等命令来查询时间服务器,等于不提供NTP的网络校时

notrap:不提供trap这个远程事件登录的功能

notrust:拒绝没有认证的客户端

如果在parameter的地方没有加上任何参数的话,这表示该ip或网段不受任何限制。


server 0.rhel.pool.ntp.org iburst   互联网上的ntp服务器

利用server设置上层NTP服务器,设置方式为:

server host  [ key n ] [ version n ] [ prefer ] [ mode n ] [ minpoll n ] [ maxpoll n ] [ iburst ]

其中host是上层NTP服务器的IP地址或域名,随后所跟的参数解释如下所示:

key:表示所有发往服务器的报文包含有秘钥加密的认证信息,n是32位的整数,表示秘钥号。

version:表示发往上层服务器的报文使用的版本号,n默认是3,可以是1或者2。

prefer:如果有多个server选项,具有该参数的服务器优先使用。

mode:指定数据报文mode字段的值。

minpoll:指定与查询该服务器的最小时间间隔为2的n次方秒,n默认为6,范围为4-14。

maxpoll:指定与查询该服务器的最大时间间隔为2的n次方秒,n默认为10,范围为4-14。

 iburst:当初始同步请求时,采用突发方式接连发送8个报文,时间间隔为2秒。

重启服务

[root@localhost ~]# systemctl restart ntpd

可使用如下命令来查看NTP服务器是否已经顺利更新了自己的时间

[root@localhost ~]# ntpstat                                    //该命令可列出我们的NTP服务器是否已经与上层连接。

[root@localhost ~]# ntpstat 

unsynchronised

   polling server every 64 s

等一会儿再查看

[root@localhost ~]# ntpstat

synchronised to NTP server (203.107.6.88) at stratum 3

   time correct to within 111 ms

   polling server every 128 s

NTP服务器已经成功与上层建立连接。时间已经校正约111ms,每隔128s会主动去更新时间。

[root@localhost ~]# ntpq -p                                    //可以列出当前我们的NTP与相关的上层NTP的状态

     remote           refid      st t when poll reach   delay   offset  jitter

=========================================================================

+electrode.felix 89.231.96.83     2 u   24   64  377  386.224    7.593  58.888

+ntp5.flashdance 192.36.143.130   2 u   26   64  377  417.799   38.981 207.158

*203.107.6.88    10.165.84.13     2 u   22   64  377   73.291   33.280   6.919

-static.107.100. 192.53.103.108   2 u   91   64   12  466.187  -76.949  77.603

remote  远程主机的主机名或IP

*代表目前正在使用的上层NTP

+连接成功,可提供时间更新的候补服务器

-该NTP服务器被认为不合格

refid    上级NTP服务器的地址

st     上级NTP服务器的层级,0-15

t :The type of the peer (local, unicast, multicast or broadcast) when the last packet was received

l    local (such as a GPS clock) 

u   unicast (this is the common type) (单播,即NTP client向NTP server发送NTP请求,NTP server回复的模式)

m  multicast (多播,可跨子网。)

b   broadcast (广播,不可跨子网。NTP server定时向广播地址发送NTP包,NTP client通过广播地址获取NTP包,同步本地时钟)

when 几秒钟前曾做过时间同步化

poll    下一次更新在几秒钟之后

reach   八进制数,已经向上层服务器要求更新的次数

delay   网络传输过程中的延迟时间

offset  本地和服务器之间的时间差别,越接近0,说明和服务器的时间约接近

jitter  系统时间与硬件时间的差异

服务端(192.168.206.141):

[root@localhost ~]# systemctl stop firewalld

[root@localhost ~]# setenforce 0

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

restrict 192.168.206.0 mask 255.255.255.0                         //限制当前网段的主机可以同步时间

server 127.127.1.0                                                               //设置上层NTP服务器为自己

fudge 127.127.1.0 stratum 8                                                //虚拟自己在第8层

[root@localhost ~]# systemctl restart ntpd

客户端(192.168.206.138):

[root@localhost ~]# ntpdate -q 192.168.206.141                                     //查看上层服务器的状态

server 192.168.206.141, stratum 9, offset -0.002161, delay 0.02641

29 Mar 19:02:53 ntpdate[43576]: adjust time server 192.168.206.141 offset -0.002161 sec

[root@localhost ~]# ntpdate 192.168.206.141                                       //更新时间

29 Mar 19:02:08 ntpdate[43567]: step time server 192.168.206.141 offset 32331.572864 sec

使用计划任务自动更新客户端的时间

编辑/etc/crontab文件

[root@localhost ~]# vim /etc/crontab

* * * * * root /usr/sbin/ntpdate 192.168.206.141 > /dev/null 2>&1

标准输出(stdout):代码为1,使用>或>>;

标准错误输出(stderr):代码为2,使用2>或2>>。

 2>&1 意思是把标准错误输出重定向到标准输出

发布了43 篇原创文章 · 获赞 50 · 访问量 7943

猜你喜欢

转载自blog.csdn.net/weixin_43997530/article/details/104130971
今日推荐