简单搭建本地ntp时间服务器

标签(空格分隔): Linux ntp

《简单搭建本地ntp时间服务器》 ntp阶梯式架构图

NTP(Network Time Protocol):

同步网络中各个计算机时间的协议.ntp服务器监听端口为UDP的123.

本地ntp时间服务器:

在本地的一台可连接互联网的主机Server上安装实现NTP协议的应用,其它本地局域网的各主机都定期来这台时间服务器获取(同步)时间,以保证各计算机的时间一致.

开始实验

❶准备若干台虚拟机(我这里用3台CentOS7作演示)

10.0.0.111: 当作ntp Server,可以与互联网进行通讯(Server)
10.0.0.112: 与Server主机在同一网段,但不能与互联网通信(Client1)
10.0.0.113: 同10.0.0.112(Client2)

❷设置Server

ln -sv /usr/share/zoneinfo/posix/Asia/Shanghai /etc/localtime # 设为上海时区
yum install ntp              # 安装ntp,ntpdate作为依赖也会被安装,如下图

《简单搭建本地ntp时间服务器》 发现ntpdate作为依赖也被安装

ntpdate 202.120.2.101      # 手动同步时间(上交大的授时服务),ntpd服务启动后这个命令不能执行生效(socket占用)

配置ntp配置文件
vim /etc/ntp.conf
    restrict 10.0.0.0 mask 255.255.255.0 nomodify notrap # nomodify表示客户端不能更改服务器端的时间参数,参数说明在下面
    server 127.127.1.0                
    fudge 127.126.1.0 stratum 10
    
    server asia.pool.ntp.org prefer # prefer表示优先使用该项
    server 0.asia.pool.ntp.org
    server 1.asia.pool.ntp.org
    server 2.asia.pool.ntp.org
    server time.nist.gov

systemctl restart ntpd              # 重启ntpd服务
systemctl enable ntpd               # 将ntpd服务加入开机自启动

ntpstat                             # 查看ntp服务
ntpq -p                             # 查看ntp Server状态

iptables -A INTPUT -m state --state NEW -m udp -p udp --dport 123 -j ACCEPT      # 防火墙放行udp 123端口的报文
restrict格式
restrict IPADDR mask MASK 参数
参数:
    ignore: 关闭所有NTP联机服务
    nomodify: 客户端不能更改服务器端的时间参数,但是可以通过服务端进行网络校时。
    notrust: 客户端除非通过认证,否则客户端来源将视为不信任子网
    noquery:不提供客户端的时间查询

❸设置Client

yum -y install ntpdate              # 默认应该都有安装
crontab -e
    # sync time with local ntp server 10.0.0.111 every day
    30 06 * * * /usr/sbin/ntpdate 10.0.0.111 &> /dev/null;/sbin/hwclock -w &> /dev/null
systemctl restart crond
systemclt enable crond

我们这里在Client端设置计划任务: 每天早上6:30与本地ntp时间服务器10.0.0.111同步时间.ntp默认只同步系统时间,为了与硬件时间保持一致,所以在同步的时候我们又设置了其与系统时间保持一致.

Ok,到这里简单的ntp服务的搭建便完成了。

    原文作者:uangianlap
    原文地址: https://www.jianshu.com/p/325b63bffe11
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞