centos配置NTP服务器

时间服务器:

NTP(Network Time Protocol,网络时间协议)是用来使用网络中的各个计算机时间同步的一种协议,NTP服务器就是利用NTP协议提供时间同步服务的。

 

一、环境准备:

1、配置计算机IP:

Centos 7系统上配置NTP时钟服务器:

时钟服务器:192.168.13.131

客户端同步服务器:192.168.13.132

 

2、查看两台机器是否安装NTP包:

[root@ntp-server ~]# rpm -qa | grep ntp

ntpdate-4.2.6p5-25.el7.centos.x86_64

ntp-4.2.6p5-25.el7.centos.x86_64

 

这样输出表明已经安装好,如果没有可以使用“yum -y install ntp ntpdate”进行安装

 

3、查看当前服务器时区&列出时区并设置时区:

[root@ntp-server ~]# timedatectl

[root@ntp-server ~]# timedatectl list-timezones

[root@ntp-server ~]# timedatectl set-timezone Asia/Shanghai

 

4、时间时区概念理解:

GMT   UTC    CST    DST

UTC:整个地球分为二十四个时区,每个时区都有自己的本地时间,在国际无线电通信场合,为了统一起见,使用一个统一的时间,称为通用协调时。

 

GMT:格林威治标准时间:指位于英国伦敦郊区的皇家格林尼治天文台的标准时间,因为本初子午线被定义在通过那里的经线(UTC与GMT时间基本相同)

 

CST:中国标准时间

GMT+8=UTC+8=CST

 

DST:夏令时指在夏天太阳升起的比较早时,将时间拨快一小时,以提早日光时间(中国不适用)

 

5、使用NTP公共服务器池同步的服务器时间(http://www.pool.ntp.org

 

6、NTP配置文件

修改NTP配置文件:

vim /etc/ntp.conf

 

添加NTP服务的日志目录:

  4 driftfile /var/lib/ntp/drift

  5

  6 logfile /var/log/ntpd.log

 

 

设置授权198.168.13.0/24网段上的所有机器都可以从这台机器上查询和同步时间。

 15 restrict 127.0.0.1

 16 restrict ::1

 17 restrict 192.168.13.0 mask 255.255.255.0 nomodify notrap

 

 

设置时间服务器列表:

 24 server 0.centos.pool.ntp.org iburst(系统默认的,可以注释)

 25 server 1.centos.pool.ntp.org iburst

 26 server 2.centos.pool.ntp.org iburst

 27 server 3.centos.pool.ntp.org iburst

 

 

当外部时间不可用时,使用本地时间:

 29 server 192.168.13.131 iburst(无上层时间时配置为:server 127.127.1.0

 30 fudge 127.127.1.0 stratum 11

 

设置允许上层时间服务器主动修改本机时间:

 37 #manycastclient 239.255.254.254 autokey # manycast client

 38

 39 restrict 0.centos.pool.ntp.org nomodify notrap noquery

 40 restrict 1.centos.pool.ntp.org nomodify notrap noquery

 41 restrict 2.centos.pool.ntp.org nomodify notrap noquery

 42 restrict 3.centos.pool.ntp.org nomodify notrap noquery

 

7、设置开启自启动:

[root@ntp-server ~]# systemctl enable ntpd

[root@ntp-server ~]# systemctl enable ntpdate

[root@ntp-server ~]# systemctl is-enabled ntpd

 

手动修改服务器时间时:

[root@ntp-server ~]# timedatectl set-ntp no

[root@ntp-server ~]# timedatectl set-time 10:00:00

 

8、查看服务器硬件时间:

[root@ntp-server ~]# hwclock

 

硬件时钟与系统时钟同步:

# hwclock --hctosys 或者 # clock --hctosys  hc代表硬件时间,sys代表系统时间,即用硬件时钟同步系统时钟

系统时钟和硬件时钟同步:

# hwclock --systohc或者# clock --systohc  即用系统时钟同步硬件时钟

 

9、服务端重启NTP服务:

[root@ntp-server ~]# systemctl restart ntpdate

 

二、客户端配置ntp:

1、通过ntpd方式同步时间

vim /etc/ntp.conf

 

server 192.168.13.131

fudge 127.127.1.0 stratum 11         #这里必须是11,因为不是顶级的

 

 

[root@ntp-client ~]# ntpq -np                  #通过配置ntp配置文件,完成自动时间同步

     remote           refid      st t when poll reach   delay   offset  jitter

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

 192.168.13.131  LOCAL(0)        12 u   12   64    1    0.432    2.574   0.000

 

2、通过ntpdate方式同步时间

[root@ntp-client ~]# ntpdate 192.168.13.131          #手动NTP同步时间

[root@ntp-client ~]# crontab -e                                    #通过定时任务完成同步

0 0 * * * /usr/sbin/sntp -P no -r 192.168.13.131;hwclock -w

 

 

四、命令解析:

一、ntpq

1、简介:

ntpq是查询ntpd工作状态的命令,一般使用“ntpq -np”参数

 

 

二、ntpdate

1、简介:

ntpdate通过轮询指定为服务器参数的网络时间协议(NTP)服务器来设置本地日期和时间,从而确定正确的时间,在使用ntpdate同步时间时,需要保证ntpd服务处于关闭状态。

 

2、使用条件:

root身份

ntpd未运行(ntpd运行时占用udp123端口)

3、常用方法:

ntpdate -d ip

debug模式,查询与服务器的时差,状态。ip是指时间服务器的IP地址

ntpdate ip

与服务器同步本地系统时间

 

三、hwclock

1、简介:

是一个硬件时钟访问工具,它可以显示当前时间、设置硬件时钟的时间和设置硬件时钟为系统时间,也可设置系统时间为硬件时钟的时间。

 

在Linux中有硬件时钟与系统时钟等两种时钟。硬件时钟是指主机板上的时钟设备,也就是通常可在BIOS画面设定的时钟。系统时钟则是指kernel中的时钟。当linux启动时,系统时钟会去读取硬件时钟的设定,之后系统时钟即独立运作。所有linux相关指令与函数都是读取系统时钟的设定。

 

2、选项:

--adjust:hwclock每次更改硬件时钟时,都会记录在/etc/adjtime文件中。使用--adjust参数,可使hwclock根据先前的记录来估算硬件时钟的偏差,并用来校正目前的硬件时钟;
--debug:显示hwclock执行时详细的信息;
--directisa:hwclock预设从/dev/rtc设备来存取硬件时钟。若无法存取时,可用此参数直接以I/O指令来存取硬件时钟;
--hctosys:将系统时钟调整为与目前的硬件时钟一致;
--set --date=<日期与时间>:设定硬件时钟;
--show:显示硬件时钟的时间与日期;
--systohc:将硬件时钟调整为与目前的系统时钟一致;
--test:仅测试程序,而不会实际更改硬件时钟;
--utc:若要使用格林威治时间,请加入此参数,hwclock会执行转换的工作;
--version:显示版本信息。

 

posted @ 2019-01-24 11:31  Day__Day_Up  阅读(4837)  评论(0编辑  收藏  举报