zoukankan      html  css  js  c++  java
  • 浅谈Linux(Centos7.4)环境下NTP服务器的构建

    

    一、软件环境

    1、操作系统版本

    [root@Geeklp201 etc]# cat /etc/redhat-release
    CentOS Linux release 7.4.1708 (Core)

    2、实践环境IP规划

    服务端:192.168.237.201

    客户端:192.168.237.202        192.168.237.203

    二、安装服务端及客户端相关软件

    服务端(192.168.237.201)需安装ntpd服务,ntpdate会被作为依赖包被安装。当然,也可以直接找到ntpdrpm包单独安装。强烈不建议使用源码安装。

    [root@Geeklp201 ~]# yum -y installntp

    安装完成之后我们会看到以下提示。

    已安装:

    ntp.x86_64 0:4.2.6p5-25.el7.centos.2                                                                                                                                

    作为依赖被安装:

    ntpdate.x86_64 0:4.2.6p5-25.el7.centos.2

    查看一下当前系统时间:

    [root@Geeklp201 ~]# date

    2017年 10月 26日 星期四 18:51:45 CST

    查看一下刚刚安装的ntpd服务:

    [root@Geeklp201 ~]# systemctl status ntpd

    ● ntpd.service - Network Time Service

      Loaded:loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset:disabled)

      Active:inactive (dead) since 四 2017-10-26 16:21:00 CST; 2h 30min ago

     Main PID: 1578(code=exited, status=0/SUCCESS)

    … ….

    我们可以看到,服务并未运行,也没有开机启动。今天应该是1027日,但是实际显示的时间是26日。这是由于我自己的系统时间设置就是26日。

    启动ntpd服务:

    [root@Geeklp201 ~]# systemctl start ntpd

    设置ntpd服务开机启动:

    [root@Geeklp201 ~]# systemctl enable ntpd

    Created symlink from/etc/systemd/system/multi-user.target.wants/ntpd.service to/usr/lib/systemd/system/ntpd.service.

    稍等片刻之后,再次执行date命令,时间已经正确显示:

    [root@Geeklp201 ~]# date

    2017年 10月 27日 星期五 09:56:24 CST

    接下来分别在2台客户机上安装ntpdate

    [root@Geeklp202 ~]# yum -y install ntpdate

    安装完毕之后执行命令:

    [root@Geeklp202 ~]# ntpdate 192.168.237.201

    27 Oct 10:32:36ntpdate[7318]: no server suitable for synchronization found

    时间同步并没有成功,原因是在服务端我们开启了防火墙,关闭防火墙:

    [root@Geeklp201 ~]# systemctl stop firewalld

    如果使用的是iptables防火墙,那么也是需要关闭的:

    [root@Geeklp201 ~]# systemctl stop iptables

    [root@Geeklp201 ~]# systemctl stop ip6tables

    再次在客户端执行:

    [root@Geeklp202 ~]# ntpdate 192.168.237.201

    27 Oct 10:36:35ntpdate[7320]: adjust time server 192.168.237.201 offset 0.017485 sec

    时间同步成功。如果没有什么其他要求,NTP服务器至此完全搭建完成。

    三、个性化配置

    1、关于防火墙设置

    关闭NTP服务器上的防火墙并非万全之策,毕竟存在即合理。最好的解决方案一定不会是关闭防火墙。接下来,我们尝试把ntpd服务添加到防火墙策略里面。由于CentOS7默认使用的防火墙是firewalld,所以只需要配置firewalld开放ntpd使用的123端口即可。

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

    [root@Geeklp201 ~]# firewall-cmd --set-default-zone=dmz

    把接口添加到dmz区:

    [root@Geeklp201 ~]# firewall-cmd --permanent --zone=dmz--add-interface=ens33

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

    [root@Geeklp201 ~]# firewall-cmd --permanent --zone=dmz--add-port=123/udp

    重载防火墙:

    [root@Geeklp201 ~]# firewall-cmd --reload

    在服客户端行ntpdate命令:

    [root@Geeklp202 ~]# ntpdate 192.168.237.201

    27 Oct 11:23:24ntpdate[7436]: adjust time server 192.168.237.201 offset 0.023007 sec

    如果使用的是iptables防火墙,也需要作相关配置,在此不作赘述。

    2ntpd.conf的个性化配置

    在ntp.conf配置文件内可以利用“restrict”来控管权限,这个参数的设定方式为:

    restrict [你的IP] mask[netmask_IP] [parameter]

    由于公司内网网段划分比较复杂,在此并未作限制。使用原有默认设置:

    restrict default nomodify notrap nopeer noquery

    restrict参数作用如下如下,可根据时间情况进行添加:

    ignore:拒绝所有类型的 NTP 联机;

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

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

    notrap:不提供 trap 这个远程事件登录(remote event logging) 的功能;

    notrust:要求客户端必须通过认证,否则拒绝没有认证的客户端;

    nopeer:用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟;

    kod:访问违规时发送 KoD 包;

    restrict-6:表示IPV6地址的权限设置。

    在本配置文件中以serve打头这一行,就是为我们提供ntp服务的上层服务器域名或IP。我使用了如下几个提供ntp服务的域名:

    server cn.ntp.org.cnperfer

    server edu.ntp.org.cniburst

    server0.centos.pool.ntp.org iburst

    server1.centos.pool.ntp.org iburst

    server2.centos.pool.ntp.org iburst

    server3.centos.pool.ntp.org iburst

    配置文件修改完毕,保存之后重启ntp服务:

    [root@Geeklp201 ~]# systemctl restart ntpd

    看一下当前NTP服务器对上层服务的联机情况:

    [root@Geeklp201 ~]# ntpq -p

    ntpq -p可以列出目前我们的 NTP与相关的上层 NTP的状态,表头的的几个字段的意义为:

    remote NTP主机的 IP或主机域名,注意最左边的符号

    如果有『 *』代表目前正在使用中的上层 NTP

    如果是『 +』代表也有连上线,而且可作为下一个提供时间更新的候选者

    refid:参考的上一层 NTP主机的地址

    st:就是 stratum级别,正在响应请求的NTP服务器的级别;

    when:几秒钟前曾经做过时间同步化更新的动作

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

    reach:已经向上层 NTP服务器要求更新的次数

    delay:网络传输过程当中延迟的时间,单位为 10^(-6)

    offset:时间补偿的结果,单位与 10^(-3)

    jitterLinux系统时间与 BIOS 硬件时间的差异时间,单位为 10^(-6)秒。

    我们还可使用ntpstat查看一下当前NTP服务器的同步情况:

    [root@Geeklp201 ~]# ntpstat

    synchronisedto NTP server (202.112.31.197) at stratum 3

      time correct to within 41 ms

      polling server every 64 s

    3、客户端设置

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

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

    4、客户端定时任务配置

    [root@Geeklp202 ntp]# crontab -e

    5923 * * */sbin/ntpdate 192.168.237.201

    设置为每天23:59分执行,重启crond服务。至此,NTP服务器构建完成。

    [root@Geeklp202 ntp]# systemctl status crond

    在CentOS7中,系统已经集成了另一款更好用的NTP服务软件chrony,在后续的文章中我们将进一步讨论。

  • 相关阅读:
    隐语义模型LFM
    基于内容的推荐、协同过滤
    评定标准
    函数式模型示例
    函数式模型概述
    序贯模型
    seq2seq
    链队列
    顺序栈
    线性表的静态链表存储结构
  • 原文地址:https://www.cnblogs.com/aceboy/p/7820198.html
Copyright © 2011-2022 走看看