一、先介绍几个与时间相关常见的概念

GMT:Greenwich Mean Time    格林威治时间

理论上来说,格林尼治标准时间的正午是指当太阳横穿格林尼治子午线时(也就是在格林尼治上空最高点时)的时间。由于地球在它的椭圆轨道里的运动速度不均匀,这个时刻可能和实际的太阳时相差16分钟。

UTC:Coordinated Universal Time  协调世界时

世界标准时间

CST:(Chinese Standard Time)

CST却同时可以代表如下 4 个不同的时区:

Central Standard Time (USA)       UT-6:00
        Central Standard Time (Australia) UT+9:30
        China Standard Time               UT+8:00
        Cuba Standard Time UT-4:00

二、操作系统上的两种时间


软件时钟:也叫系统时间,根据1970/01/01开始计算的总秒数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
查看系统时间:
    [root@node1 ~]# date            #查看当前时间
    Wed Jun 28 19:57:53 CST 2017
    [root@node1 ~]#  date +"%Y-%m-%d" #设置当前时间格式
    2017-06-28
     
    [root@node1 ~]# date +"%Y-%m-%d %H:%M:%S"    #设置当前时间格式
    2017-06-28 19:58:42
     
    [root@node1 ~]# date +%s         #从1970/01/01到现在的总秒数
    1498651211
设置时间:
date  -s  "20120523 01:01:01"   #设置当前系统时间,一般不建议直接在生产主机上执行。
date  -s "dd/mm/yyyy hh:mm:ss"

硬件时钟:主机硬件系统上的时钟。主板上BIOS中的时间,由主板电池供电来维持运行,系统开机时要读取这个时间,并根据它来设定系统时间.

1
2
查看硬件时间: hwclock 或clock
设置: hwclock --set --date="06/18/14 14:55"

硬件时间与系统时间相互转换:

1
2
clock --hctosys      #将系统时间调整为目前的硬件时间一致;当Linux启动时,系统时钟会去读取硬件时钟的设定,之后系统时钟即独立运作.
clock --systohc      #将硬件时钟调整为与目前的系统时钟一致;将系统时间存回硬件时钟内

三、NTP服务器安装与设置


1.所需要的软件

ntp      主要软件

tzdata   提供各时区对应的显示格式

2.相关文件

/etc/ntp.conf     主配置文件

/usr/share/zoneinfo/    由tzdata提供,为各时区的时间格式对应文件。

/etc/sysconfig/clock    设置时区是否使用UTC时钟的配置文件。

1
2
3
 [root@node1 zoneinfo]# cat /etc/sysconfig/clock 
  ZONE="Asia/Shanghai"
 #表明我们的时间配置文件要使用/usr/share/zoneinfo/Asia/Shanghai那个文件

/etc/localtime     本地端的时间配置文件

1
2
3
4
5
6
简单的一个例子:
目前在上海,假如到了美国,怎么调整时间?
首先    vim /etc/sysconfig/clock  
        ZONE="America/New_York"
然后    cp  /usr/share/zoneinfo/America/New_York    /etc/localtime 
最后    date

3.主配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
[root@node1 ~]# cat /etc/ntp.conf 
# 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 kod nomodify notrap nopeer noquery
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
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#server 0.rhel.pool.ntp.org iburst
#server 1.rhel.pool.ntp.org iburst
#server 2.rhel.pool.ntp.org iburst
#server 3.rhel.pool.ntp.org iburst
server 1.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

参数解释:

(1)driftfile  记录时间差异

(2)restrict   管理权限控制

1
2
3
4
5
6
7
8
9
restrict   [ip]  mask [netmask_ip]  [parameter]
    parameter主要有:
       ignore:拒绝所有类型的NTP连接
       nomodify:客户端不能使用ntpc/ntpq来修改服务器的时间参数,但客户端可以进行网络校时
       noquery:客户端不能用ntpq/ntpc来查询时间服务器,等于不提供NTP的网络校时
       notrap:不提供trap这个远程事件登陆
       notrust:拒绝没有认证的客户端
        
如果在[parameter] 没有设置,则表示该网段不受任何限制。

(3)server:设置上层的NTP服务器

1
server [ip或主机名]  [prefer]

四、服务器NTP服务的启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#/etc/init.d/ntpd start    #启动服务
#chkconfig ntpd on         #开机自启
#tail /var/log/messages    #查看日志
查看端口:
[root@node1 ~]# netstat -tulnp | grep ntp
udp        0      0 192.168.209.128:123         0.0.0.0:*                               2766/ntpd           
udp        0      0 127.0.0.1:123               0.0.0.0:*                               2766/ntpd           
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               2766/ntpd           
udp        0      0 fe80::20c:29ff:fe67:be62:123 :::*                                    2766/ntpd           
udp        0      0 ::1:123                     :::*                                    2766/ntpd           
udp        0      0 :::123                      :::*                                    2766/ntpd
通常启动NTP15分钟后才会和上层NTP服务器顺利连接上。
#ntpstat 查看ntp服务器是否与上层连接。
synchronised to NTP server (xx.xx.xx.xx) at stratum 3 
   time correct to within 538 ms
   polling server every 128 s
    
       
[root@node1 ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 85.199.214.101  .GPS.            1 u   55   64    1  245.097  179.415   0.000
  
remote: NTP主机的IP或者主机名
    * 代表目前正在作用当中的上层NTP
    + 代表已经连接成功,可以作为下一个提供更新时间的候选者
refid:NTP服务器使用的更高一级服务器的名称
st: stratum阶层
when:几秒前曾经做过时间同步化更新的操作.上一次成功请求之后到现在的秒数
poll:下一次更新在几秒钟之后.本地和远程服务器多少时间进行一次同步,单位秒,在一开始运行NTP的时候这个poll值会比较小,服务器同步的频率大,可以尽快调整到正确的时间范围,之后poll值会逐渐增大,同步的频率也就会相应减小
reach:已经向上层NTP服务器要求更新的次数.用来测试能否和服务器连接,是一个八进制值,每成功连接一次它的值就会增加
delay:网络传输过程当中延迟的时间.从本地机发送同步要求到ntp服务器的往返时间
offset:时间补偿的结果.主机通过NTP时钟同步与所同步时间源的时间偏移量,单位为毫秒,offset越接近于0,主机和ntp服务器的时间越接近
jitter:Linux系统时间与硬件时间的差异时间。这个数值的绝对值越小,主机的时间就越精确

五、客户端同步NTP服务器的时间

1
2
3
4
5
6
7
8
9
10
11
方法1:
    ntpdate 192.168.209.128
    编辑定时任务crontab,每隔30分钟自动同步,并写入硬件时钟。
    */30  * * * * ( /usr/sbin/ntpdate 192.168.209.128 && hwclock -w ) &>/dev/null
     
方法2:
    修改/etc/ntp.conf配置文件,开启ntpd服务,使其自动去同步上层ntp服务器。
     添加server 1.cn.pool.ntp.org    
      
     /etc/init.d/ntpd start
     chkconfig ntpd on
本文转自   a8757906   51CTO博客,原文链接:http://blog.51cto.com/nxyboy/1944855

9.linux ntp服务器搭建相关推荐

  1. LINUX NTP 服务器搭建

    LINUX NTP 服务器搭建 ╮(╯▽╰)╭ , 这一生让我感到悲剧,关于LINUX NTP 服务器搭建,网上的资料太多了,但是有几个能详细的说明一下ntp.conf 文件呢,几乎都是千篇一律,同样 ...

  2. linux NTP服务器搭建

    1. NTP简介 NTP(Network Time Protocol 网络时间协议)是一个用于同步计算机时钟的网络协议.它可以使计算机与其他服务器或时钟源进行时间同步,进行高精度的时间校正. 简而言之 ...

  3. 虚拟化--051 vsphere linux搭建NTP服务器搭建

    051 vsphere linux搭建NTP服务器搭建 参考链接 http://url.cn/g96DNG 转载于:https://blog.51cto.com/williamliuwen/16865 ...

  4. 离线NTP服务器搭建(Linux)

    离线NTP服务器搭建(Linux) 如果没能帮你解决问题,请在下方留言 实例: 服务端-192.168.111.143 客户端-192.168.111.170 1. 获取rpm包 解压操作系统的镜像文 ...

  5. linux 建立 ntp 服务器,NTP服务器搭建

    1.NTP(Network Time Protocol) 在计算机世界中,NTP(Network Time Protocol,网络时间协议)被广泛用于对时间的统一性和准确性要求非常高的场景,是用来使网 ...

  6. ntp服务器搭建及客户端配置-使用阿里云

    1. NTP服务器搭建 安装ntp yum install -y ntp 2. 外网使用阿里云ntp服务器作为基准 阿里云ntp服务器列表: time1.aliyun.com time2.aliyun ...

  7. Linux邮件服务器搭建实验

    Linux邮件服务器搭建实验 前言 本文重点在于后面的搭建过程,前面会简单介绍邮件服务原理与工作流程,如果有什么错误或遗漏的地方,欢迎大家指正批评,谢谢. 基本理论说明 电子邮件服务器是处理邮件交换的 ...

  8. 腾讯云Linux云服务器搭建网站

    腾讯云Linux云服务器搭建网站 基本准备及操作 这里的安装过程使用系统自带的yum工具进行安装 安装jdk和java运行环境 安装mysql 安装tomcat 安装httpd服务 部署web项目到服 ...

  9. centos7 NTP服务器搭建

    NTP服务器搭建 验证NTP软件包是否安装 [root@localhost ~]# rpm -qa ntp* ntpdate-4.2.6p5-29.el7.centos.2.x86_64 ntp-4. ...

最新文章

  1. ASP.NET中序列化与反序列化-以显示上一次登录的信息为例
  2. BZOJ 4278 [ONTAK2015]Tasowanie (后缀数组)
  3. java 创建uri_使用UriBuilder快速创建URI
  4. Hadoop2.6.0完全分布式安装
  5. iOS CGRectGetMaxX/Y 使用
  6. Inception-v4,Inception-ResNet论文笔记
  7. [转]很经典的http协议详解
  8. mysql存储过程in_在MySQL存储过程中使用WHERE IN()
  9. kali linux卸载lnmp,kali linux 如何安装lnmp 1.2
  10. Android支付宝刷步数,支付宝运动怎么刷步数? 支付宝如何快速刷步数?
  11. 微信小程序-wxParse插件的使用
  12. 了解new一个对象具体过程
  13. golang读取pdf
  14. 什么是路由守卫?vue-router有哪几种路由守卫?
  15. USB2.0系列(锆石科技FPGA)
  16. 每日一面 - Spring Boot 中的 ApplicationContext 的分层是什么意思?
  17. 双击.bat文件闪退
  18. MacBook Pro连接移动硬盘未找到怎么办
  19. 「 神器 」绝不简单的截图神器
  20. 我现在也晕菜了(一)

热门文章

  1. fiddler重定向
  2. 从外行到外包,从手工测试到知名互联大厂测开 我经历了我这个年龄段不该经历的事情...
  3. 通过 YAPI + Python Flask 实现接口自动化测试
  4. oracle 错误码1438,一次ora-01438错误的处理
  5. python sys模块 argv用法_python中sys模块的argv
  6. pythondifflib详解_python difflib模块示例讲解
  7. java poi无法读取word_poi,word_POI读取word内容的疑问,poi,word,java - phpStudy
  8. vue获取url中ip_Kubernetes 集群中这样获取客户端真实 IP
  9. matlab中cuda编程中分配grid和block dimension的时候的注意事项
  10. activiti mysql_基于MySQL的Activiti6引擎创建