centos 7 ntp

程序员近来摊上个自动化运维的项目。

服务数量上,NTP客户端有两个,服务端只有一个。客户端分别是ntpd和ntpdate,服务端只有ntpd。

  • ntpd的配置文件/etc/ntp.conf
  • ntpdate的配置文件/etc/ntp/step-tickers

作为客户端:
ntpdate {{server}}又被称为手动同步,与cron结合可以实现自动同步,缺点是容易造成断秒,并且与ntpd占用相同端口(反亲和)。
ntpd方式为持续跟踪,所以又被称为自动同步,缺点是当时间差距过大(据说10分钟),无法跟踪。

内网和网关同步、网关和时钟同步,这种情况下网关只能用ntpd。
/etc/ntp.conf配置,主要就两条:
(作为客户端)我要和谁同步:server {{ server }}
(作为服务端)允许谁和我同步: restrict {{ ip }} mask {{ netmask }} {{ flags }}
(同时客户服务端)额外还有个,我的层级:fudge {{ server }} stratum {{ level }}
fudge对于纯客户端来说没有意义,只有在同时作为客户端和服务端的时候才有意义。

例子:
网关同时客户服务端的配置:
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1
restrict 172.28.15.0 mask 255.255.255.0 nomodify notrap
server 172.28.11.10
fudge 172.28.11.10 stratum 8
includefile /etc/ntp/crypto/pw
disable monitor

纯客户端的配置:
driftfile /var/lib/ntp/drift # 频飘,ppm单位
restrict default nomodify notrap nopeer noquery # 写不写这行都一样。所有主机不能变更我的时间、不能peer我、也不能查询
restrict 127.0.0.1 # lo接口可以随便
restrict ::1 # lo接口随便
server 172.28.15.15
fudge 172.28.15.15 stratum 10 # 必须大于NTP服务器的层级。0为最高级
restrict 172.28.15.15 nomodify notrap noquery # server不能变更和查询我的时间。
includefile /etc/ntp/crypto/pw # 包括配置文件
disable monitor # 与安全有关

flag之一的notrap不知道啥意思,不能陷阱我?

配置文件搞好后,以内网纯客户端为例:
ntpdate {{ server }} # 手动修改本地时间与服务器同步
service ntpd restart # 启动本地ntpd

> $ ntpq -p
>      remote           refid      st t when poll reach   delay   offset  jitter
> ==============================================================================
>  172.28.15.15    172.28.11.10     4 u   29   64    1    0.964   -0.617   0.000

remote一栏前面是空格,表示没有同步上。同步需要一分钟左右。

> $ ntpq -p
>      remote           refid      st t when poll reach   delay   offset  jitter
> ==============================================================================
> *172.28.15.15    172.28.11.10     4 u   17   64   77    0.864   -1.800   0.643

remote一栏前面是*表示同步上了。
还可以用netstat看:

> $ ntpstat 
> synchronised to NTP server (172.28.15.15) at stratum 5   
> time correct to within 64 ms    polling server every 64 s

只有当{{ server }}开启了ntpd服务时,才能实现ntpdate {{ server }},如果{{ server }}没开ntpd:

 ntpdate[19479]: no server suitable for synchronization found
### 安装和配置NTP时间同步服务 #### 服务器端设置 为了确保NTP服务器正常工作,在CentOS 7上部署NTP服务器时,需先确认防火墙允许UDP 123端口通过。接着按照如下操作: 安装ntp软件包并启用相应服务: ```bash yum -y install ntp systemctl start ntpd systemctl enable ntpd ``` 编辑`/etc/ntp.conf`文件来指定上游时间源以及允许哪些网络内的设备连接到此NTP服务器[^1]。 重启ntpd服务使更改生效: ```bash systemctl restart ntpd ``` 验证NTP服务器运行状况,可以利用命令`ntpq -p`查看当前已知的时间源及其状态;如果显示为`unsynchronised`开头,则表示尚未成功获取外部时间源的数据,等待数分钟后重试即可。 #### 客户端设置 对于希望与上述搭建好的NTP服务器保持时间一致性的其他机器而言,只需完成以下几步: 同样地,也需要安装ntp工具集但是不立即开启其守护进程: ```bash yum -y install ntp* ``` 手动向NTP服务器请求最新时间戳并将之应用至本地硬件时钟: ```bash /usr/sbin/ntpdate 192.168.220.138 && hwclock --systohc ``` 为了让这一过程周期性自动发生,可以通过crontab设定定时任务,比如每隔十分钟执行一次这样的更新动作: ```bash */10 * * * * /usr/sbin/ntpdate 192.168.220.138 && hwclock --systohc ``` 值得注意的是,这里使用的IP地址应当替换为客户实际环境中所指派给NTP服务器的那个具体数值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值