centos7搭建ntp服务器(centos76配置ntp)

【NTP】CentOS7.2配置NTP服务

操作系统:CentOS7.x 

ntp_sever:192.168.1.110

ntp_client:192.168.1.120

# yum -y install  ntp ntpdate

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

# firewall-cmd --reload

# vim  /etc/ntp.conf

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

driftfile /var/lib/ntp/drift

restrict default nomodify notrap nopeer noquery

restrict 127.0.0.1

restrict ::1

restrict 192.168.1.0 mask 255..255.255.0 nomodify notrap

server 192.168.1.110 iburst

server 127.127.1.0

fudge 127.127.1.0 stratum 10

interface ignore  wildcard

interface listen  192.168.1.110

interface listen  127.0.0.1

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

disable monitor

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

# systemctl start ntpd

# systemctl enable ntpd

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

# firewall-cmd --reload

# vim  /etc/ntp.conf

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

driftfile /var/lib/ntp/drift

restrict default nomodify notrap nopeer noquery

restrict 127.0.0.1

restrict ::1

server 192.168.1.110 iburst

restrict 192.168.1.110 nomodify notrap noquery

server 127.127.1.0

fudge 127.127.1.0 stratum 10

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

disable monitor

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

# ntpdate 192.168.1.110

# systemctl  start   ntpd

# systemctl enable ntpd

如何搭建自己的NTP服务器?

NTP服务器【Network Time Protocol(NTP)】是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可介由加密确认的方式来防止恶毒的协议攻击。时间按NTP服务器的等级传播。按照离外部UTC源的远近把所有服务器归入不同的Stratum(层)中。

                                                          

网络时间协议(NTP)用来同步网络上不同主机的系统时间。你管理的所有主机都可以和一个指定的被称为 NTP  服务器的时间服务器同步它们的时间。而另一方面,一个 NTP 服务器会将它的时间和任意公共 NTP 服务器,或者你选定的服务器同步。由 NTP  管理的所有系统时钟都会同步精确到毫秒级。

在公司环境中,如果他们不想为 NTP 传输打开防火墙,就有必要设置一个内部 NTP 服务器,然后让员工使用内部服务器而不是公共 NTP  服务器。在这个指南中,我们会介绍如何将一个 CentOS 系统配置为 NTP 服务器。在介绍详细内容之前,让我们先来简单了解一下 NTP 的概念。

为什么我们需要 NTP?

由于制造工艺多种多样,所有的(非原子)时钟并不按照完全一致的速度行走。有一些时钟走的比较快而有一些走的比较慢。因此经过很长一段时间以后,一个时钟的时间慢慢的和其它的发生偏移,这就是常说的  “时钟漂移” 或 “时间漂移”。为了将时钟漂移的影响最小化,使用 NTP 的主机应该周期性地和指定的 NTP 服务器交互以保持它们的时钟同步。

在不同的主机之间进行时间同步对于计划备份、入侵检测记录、分布式任务调度或者事务订单管理来说是很重要的事情。它甚至应该作为日常任务的一部分。

NTP 的层次结构

NTP 时钟以层次模型组织。层级中的每层被称为一个 stratum(阶层)。stratum 的概念说明了一台机器到授权的时间源有多少 NTP  跳。

Stratum 0 由没有时间漂移的时钟组成,例如原子时钟。这种时钟不能在网络上直接使用。Stratum N (N 1) 层服务器从  Stratum N-1 层服务器同步时间。Stratum N 时钟能通过网络和彼此互联。

NTP 支持多达 15 个 stratum 的层级。Stratum 16 被认为是未同步的,不能使用的。

准备 CentOS 服务器

现在让我们来开始在 CentOS 上设置 NTP 服务器。

首先,我们需要保证正确设置了服务器的时区。在 CentOS 7 中,我们可以使用 timedatectl  命令查看和更改服务器的时区(比如,\"Australia/Adelaide\",LCTT 译注:中国可设置为 Asia/Shanghai )

代码如下:

# timedatectl list-timezones | grep Australia

# timedatectl set-timezone Australia/Adelaide

# timedatectl

继续并使用 yum 安装需要的软件

代码如下:

# yum install ntp

然后我们会添加全球 NTP 服务器用于同步时间。

代码如下:

# vim /etc/ntp.conf

server 0.oceania.pool.ntp.org

server 1.oceania.pool.ntp.org

server 2.oceania.pool.ntp.org

server 3.oceania.pool.ntp.org

默认情况下,NTP 服务器的日志保存在 /var/log/messages。如果你希望使用自定义的日志文件,那也可以指定。

复制代码

代码如下:

logfile /var/log/ntpd.log

如果你选择自定义日志文件,确保更改了它的属主和 SELinux 环境。

复制代码

代码如下:

# chown ntp:ntp /var/log/ntpd.log

# chcon -t ntpd_log_t /var/log/ntpd.log

现在初始化 NTP 服务并确保把它添加到了开机启动。

代码如下:

# systemctl restart ntp

# systemctl enable ntp

验证 NTP Server 时钟

我们可以使用 ntpq 命令来检查本地服务器的时钟如何通过 NTP 同步。

下面的表格解释了输出列。

remote 源在 ntp.conf 中定义。‘*’ 表示当前使用的,也是最好的源;‘+’ 表示这些源可作为 NTP 源;‘-’  标记的源是不可用的。

refid 用于和本地时钟同步的远程服务器的 IP 地址。

st Stratum(阶层)

t 类型。 \'u\' 表示单播(unicast)。其它值包括本地(local)、多播(multicast)、广播(broadcast)。

when 自从上次和服务器交互后经过的时间(以秒数计)。

poll 和服务器的轮询间隔,以秒数计。

reach 表示和服务器交互是否有任何错误的八进制数。值 337 表示 100% 成功(即十进制的255)。

delay 服务器和远程服务器来回的时间。

offset 我们服务器和远程服务器的时间差异,以毫秒数计。

jitter 两次取样之间平均时差,以毫秒数计。

控制到 NTP 服务器的访问

默认情况下,NTP 服务器允许来自所有主机的查询。如果你想过滤进来的 NTP 同步连接,你可以在你的防火墙中添加规则过滤流量。

# iptables -A INPUT -s 192.168.1.0/24 -p udp --dport 123 -j ACCEPT

# iptables -A INPUT -p udp --dport 123 -j DROP

该规则允许从 192.168.1.0/24 来的 NTP 流量(端口 UDP/123),任何其它网络的流量会被丢弃。你可以根据需要更改规则。

配置 NTP 客户端

1. Linux

NTP 客户端主机需要 ntpupdate 软件包来和服务器同步时间。可以轻松地使用 yum 或 apt-get  安装这个软件包。安装完软件包之后,用服务器的 IP 地址运行下面的命令。

代码如下:

# ntpdate

基于 RHEL 和 Debian 的系统命令都相同。

2. Windows

如果你正在使用 Windows,在日期和时间设置(Date and Time settings)下查找网络时间(Internet Time)。

3. Cisco 设备

如果你想要同步 Cisco 设备的时间,你可以在全局配置模式下使用下面的命令。

代码如下:

# ntp server

来自其它厂家的支持 NTP 的设备有自己的用于网络时间的参数。如果你想将设备和 NTP服务器同步时间,请查看设备的说明文档。

结论

总而言之,NTP 是在你的所有主机上同步时钟的一个协议。我们已经介绍了如何设置 NTP 服务器并使支持 NTP 的设备和服务器同步时间。

【ntpdate】CentOS7.x上使用ntpdate同步ntp服务器

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

NTP时钟同步方式说明

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

直接同步

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

平滑同步

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

标准时钟同步服务

这个网站包含全球的标准时间同步服务,也包括对中国时间的同步,对应的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

NTP client           192.168.1.104

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

# yum  -y  install ntp

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

# timedatectl set-timezone Asia/Shanghai 

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

# systemctl startntpd

# 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 

# timedatectl

NTP synchronized: yes

启用ntpd后,服务器就开启了ntpd自动同步,无法使用 timedatectl set-time HH:MM:SS重新设置时间。

如果要使用timedatectl set-time HH:MM:SS 重新设置时间:

#  systemctl stop  ntpd 

# timedatectl set-ntp  false

# timedatectl set-time HH:MM:SS

# hwclock  -w

#  systemctl start  ntpd 

1.客户端时区需要和服务端保持一致,否则无法同步时间

# timedatectl set-timezone   

Asia/Shanghai 

2.安装同步软件包

# yum   -y install  ntpdate

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

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

#  vim  /etc/sysconfig/ntpdate

4.客户端定时任务配置

# crontab  -e

59  23  *  *  *   /sbin/ntpdate  192.168.1.102

设置为每天23:59分执行,重启crond服务

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

定时任务基本格式 : 

***** command 

分 时 日 月 周 命令 

第1列表示分钟1~59 每分钟用*或者 */1表示 

第2列表示小时1~23(0表示0点) 

第3列表示日期1~31 

第4列表示月份1~12 

第5列标识号星期0~6(0表示星期天) 

第6列要运行的命令

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

5.重启定时任务

# systemctl  restart crond.service

如何在centos 7中设置ntp 服务

一、搭建时间服务器

1、在一台linux服务器安装ntp server

tar zxvf ntp-4.2.6.tar.gz

cd ntp-4.2.6

./configure --prefix=/usr/local/ntp --enable-all-clocks --enable-parse-clocks

make make install

2、修改ntp.conf配置文件

vi /etc/ntp.conf

# 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 default nomodify

(允许任何IP的客户机都可以进行时间同步,如果是只允许某个网段的客户机进行时间同步可以这样写

restrict 10.58.26.0 mask 255.255.255.0 nomodify)

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

#server 0.rhel.pool.ntp.org(默认时间服务器)

#server 1.rhel.pool.ntp.org(默认时间服务器)

#server 2.rhel.pool.ntp.org(默认时间服务器)

server 10.128.14.25 (手工设置的时间服务器)

(如果是可以直连外网,可以使用LINUX默认提供的三组标准时间服务器,否则可以自己指定一个同步时间源)

#broadcast 192.168.1.255 key 42 # broadcast server

#broadcastclient # broadcast client

#broadcast 224.0.1.1 key 42 # multicast server

#multicastclient 224.0.1.1 # multicast client

#manycastserver 239.255.254.254 # manycast server

#manycastclient 239.255.254.254 key 42 # manycast client

# Undisciplined Local Clock. This is a fake driver intended for backup

# and when no outside source of synchronized time is available.

server 127.127.1.0 # local clock

fudge 127.127.1.0 stratum 10

3、以守护进程启动ntpd

#/etc/rc.d/init.d/ntpd -c /etc/ntp.conf -p /tmp/ntpd.pid

#/etc/rc.d/init.d/ntpd start

#ps -ef|grep ntpd

4、在ntp server上启动ntp服务后,ntp server自身或者与其server的同步的需要一个时间段,这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令进行同步时会产生no server suitable for synchronization found的错误。

下面命令可以知道何时ntp server完成了和自身同步的过程

在ntp server上使用命令:

# watch ntpq -p

注意LOCAL的这个就是与自身同步的ntp server。

注意reach这个值,在启动ntp server服务后,这个值就从0开始不断增加,当增加到17的时候,从0到17是5次的变更,每一次是poll的值的秒数,是64秒*5=320秒的时间。

二、配置时间同步客户机

vi /var/spool/cron/root(或crontab -e)

增加一行,在每天的1点10分、9点10分、17点10分与时间同步服务器进行同步并写入BIOS

10 1 ,9,17* * * root /usr/sbin/ntpdate 10.128.14.25; /sbin/hwclock -w

如果同步不正常,可以加输出日志或看系统日志

输出日志的方法:

10 1 ,9,17* * * root /usr/sbin/ntpdate 10.128.14.25/tmp/1.txt; /sbin/hwclock -w

在1.txt中可查看时间同步时的输出结果。

或者看/var/mail/root系统日志

Subject: Cron root@tyzssq8 /usr/sbin/ntpdate 10.128.14.25;/sbin/hwclock -w

X-Cron-Env: SHELL=/bin/sh

X-Cron-Env: HOME=/root

X-Cron-Env: PATH=/usr/bin:/bin

X-Cron-Env: LOGNAME=root

X-Cron-Env: USER=root

Message-Id: 20121127103001.076FF2090E@tyzssq8.site

Date: Tue, 27 Nov 2012 18:30:01 +0800 (CST)

27 Nov 18:29:59 ntpdate[6917]: step time server 10.128.14.25 offset -1.361968 sec

可以看到同步成功了,如果未成功会报出错误。

三、无法同步的问题

检查ntp server主机的防火墙。可能是ntp server的防火墙屏蔽了upd 123端口。

可以用命令

#service iptables stop

未经允许不得转载:便宜VPS网 » centos7搭建ntp服务器(centos76配置ntp)