欢迎来到李洋的博客

回首境界: 众里寻他千百度,蓦然回首,那人却在灯火阑珊处。

31、NTP时间服务器

1.NTP简介

NTP服务器顾名思义就是时间同步服务器(Network Time Protocol),Linux下的ntp服务器配置相对来说都比较容易,但在Linux下有一个弊端,不同时区或者说是时间相差太大的无法同步,所以在配置ntp服务器之前需要把时间配置成相同的。

 

NTP时钟同步方式说明

NTP在linux下有两种时钟同步方式,分别为直接同步和平滑同步: 

1.1直接同步

使用ntpdate命令进行同步,直接进行时间变更。如果服务器上存在一个12点运行的任务,当前服务器时间是13点,但标准时间时11点,使用此命令可能会造成任务重复执行。因此使用ntpdate同步可能会引发风险,因此该命令也多用于配置时钟同步服务时第一次同步时间时使用。

1.2平滑同步

使用ntpd进行时钟同步,可以保证一个时间不经历两次,它每次同步时间的偏移量不会太陡,是慢慢来的,这正因为这样,ntpd平滑同步可能耗费的时间比较长。

1.3标准时钟同步服务

http://www.pool.ntp.org/zone/cn

这个网站包含全球的标准时间同步服务,也包括对中国时间的同步,对应的URL为:cn.pool.ntp.org

在其中也描述了ntp配置文件中的建议写法:

server  1.cn.pool.ntp.org
server  2.asia.pool.ntp.org
server  3.asia.pool.ntp.org

实验室集群没有联网,我们需要搭建ntp服务器并进行时间同步。

现使用的系统为centos7.2,机器使用情况如下表所示,这里以192.168.1.102为ntp server,192.168.1.104为client对时间进行同步。

NTP server   192.168.1.102

client       192.168.1.104

2.方法一:在所有节点安装 NTP,然后一台设置为server,其他为client,和server同步

1.在集群中所有节点上安装ntp

yum  -y  install ntp 

 

2.所有节点设置时区,这里设置为中国所用时间

timedatectl set-timezone Asia/Shanghai 

 

3.在server节点上启动ntp服务

systemctl start ntpd
systemctl enable ntpd

4.在server节点上设置现在的准确时间

timedatectl set-time HH:MM:SS 

5.在server节点上设置其ntp服务器为其自身,同时设置可以接受连接服务的客户端,是通过更改/etc/ntp.conf文件来实现,其中server设置127.127.1.0为其自身,新增加一个 restrict 段为可以接受服务的网段

vim  /etc/ntp.conf

 

6.重启ntpd服务

systemctl restart ntpd

7.在client节点上设置ntp服务器为server节点

vim  /etc/ntp.conf 

 

关于防火墙设置

关闭NTP服务器上的防火墙并非万全之策,毕竟存在即合理。

最好的解决方案一定不会是关闭防火墙。接下来,我们尝试把ntpd服务添加到防火墙策略里面。

由于CentOS7默认使用的防火墙是firewalld,所以只需要配置firewalld开放ntpd使用的123端口即可。

由于此NTP服务器需要放在dmz区域,所以我们需要把dmz区设置为默认区:

#  firewall-cmd --set-default-zone=dmz
# firewall-cmd  --permanent --zone=dmz  --add-interface=ens33

ntpd服务使用的端口是123,协议是udp协议,添加到防火墙策略:

# firewall-cmd --permanent --zone=dmz --add-port=123/udp
# firewall-cmd --reload

8.在client节点上同步server的时间

#  ntpdate 10.107.18.35 

9.client节点启动ntpd服务

# systemctl start ntpd
# systemctl enable ntpd

10.所有节点启动时间同步

#  timedatectl set-ntp yes

11.使用ntpq -p 查看网络中的NTP服务器,同时显示客户端和每个服务器的关系

 

12.使用ntpstat 命令查看时间同步状态,这个一般需要5-10分钟后才能成功连接和同步

刚启动时:

#  ntpstat 
unsynchronised
  time server re-starting
polling server every 8 s

连接并同步后:
synchronised to NTP server (202.112.10.36) at stratum 3
   time correct to within 275 ms
   polling server every 256 s

3.方法二:只在NTP server节点安装安装 NTP,其他为client,通过定时任务和NTP server时间同步

服务端设置同方法一

客户端设置

修改/etc/sysconfig/ntpdate,让ntpdate每次同步时间之后把时间写入hwclock,相当于命令hwclock -w

将最后一行SYNC_HWCLOCK=no修改为:SYNC_HWCLOCK=yes

vim  /etc/sysconfig/ntpdate

 

客户端定时任务配置

# crontab -e
59 23 * * *  /sbin/ntpdate 192.168.1.102
设置为每天23:59分执行,重启crond服务
# systemctl  restart crond.service

至此,NTP服务器构建完成!

posted on 2019-08-27 14:26  miss_xxy  阅读(757)  评论(0编辑  收藏  举报

导航

levels of contents