Linux【工具 01】NTP时间服务器搭建及Linux+Windows客户端利用(一篇学会使 ...

打印 上一主题 下一主题

主题 536|帖子 536|积分 1608

1.default表现对所有的盘算机进行控制

restrict default nomodify notrap nopeer noquery
局域网可放行可以指定网段或列出单独IP

restrict xxx.xxx.xxx.x mask 255.255.255.0 nomodify

restrict xxx.xxx.xxx.x nomodify

1.end

restrict 127.0.0.1
restrict ::1
2.注释掉公网NTP服务器

server 0.centos.pool.ntp.org iburst

server 1.centos.pool.ntp.org iburst

server 2.centos.pool.ntp.org iburst

server 3.centos.pool.ntp.org iburst

2.end

3.开启自身同步

server 127.127.1.0
Fudge 127.127.1.0 stratum 8
3.end

includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
  1. ### 2.3 服务器端测试
复制代码
重启服务

systemctl restart ntpd
测试

[root@aliyun ~]# ntpstat
synchronised to local net (127.127.1.0) at stratum 6
time correct to within 949 ms
polling server every 64 s
  1. ## 3.Linux客户端配置
  2. ### 3.1 安装
  3. 安装过程跟服务器端一致,这里不再赘述。
  4. ### 3.2 客户端配置
  5. `ntp.conf`修改前不再赘述,修改后:
复制代码
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1
1.注释掉公网NTP服务器

server 0.centos.pool.ntp.org iburst

server 1.centos.pool.ntp.org iburst

server 2.centos.pool.ntp.org iburst

server 3.centos.pool.ntp.org iburst

1.end

2.开启服务器同步

server aliyun
Fudge aliyun stratum 8
2.end

includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
  1. ### 3.3 客户端测试
复制代码
查看NTP服务是否开启

[root@tcloud ~]# timedatectl
Local time: Fri 2022-03-25 15:29:25 CST
Universal time: Fri 2022-03-25 07:29:25 UTC
RTC time: Fri 2022-03-25 07:29:24
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
开启NTP服务

[root@tcloud ~]# timedatectl set-ntp yes
[root@tcloud ~]# timedatectl
Local time: Fri 2022-03-25 15:52:30 CST
Universal time: Fri 2022-03-25 07:52:30 UTC
RTC time: Fri 2022-03-25 07:52:29
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
  1. [/code] [size=5]重启服务[/size]
  2. systemctl restart ntpd
  3. [size=5]测试[/size]
  4. [root@tcloud ~]# ntpstat
  5. unsynchronised
  6. time server re-starting
  7. polling server every 8 s
  8. [size=5]配置后一般要等候几分钟才能与/etc/ntp.conf中的服务器端完成同步[/size]
  9. [root@tcloud ~]# ntpstat
  10. synchronised to NTP server (101.201.69.99) at stratum 7
  11. time correct to within 968 ms
  12. polling server every 64 s
  13. [code]
  14. ### 3.4 另一种方式
  15. 使用`ntpdate`命令结合定时任务实现时间同步。此时要停止`ntpd`服务器,测试`ntpdate aliyun`报错,查看报错信息:
复制代码
[root@tcloud ~]# ntpdate -d aliyun
28 Mar 15:33:22 ntpdate[17145]: ntpdate 4.2.6p5@1.2349-o Tue Jun 23 15:38:19 UTC 2020 (1)
Looking for host aliyun and service ntp
host found : aliyun
transmit(101.201.69.99)
101.201.69.99: Server dropped: no data
server 101.201.69.99, port 123
28 Mar 15:33:30 ntpdate[17145]: no server suitable for synchronization found
  1. 解决:关闭防火墙,配置`aliyun`服务器的安全组放开端口`123`。
复制代码
端口开放后

[root@tcloud ~]# ntpdate aliyun
28 Mar 16:06:57 ntpdate[2330]: adjust time server 101.201.69.99 offset 0.000112 sec
  1. [/code] [size=5]添加定时任务[/size]
  2. vim /etc/crontab
  3. SHELL=/bin/bash
  4. PATH=/sbin:/bin:/usr/sbin:/usr/bin
  5. MAILTO=root
  6. [size=5]/etc/crontab 内容如下[/size]
  7. [size=5]For details see man 4 crontabs[/size]
  8. [size=5]Example of job definition:[/size]
  9. [size=5].---------------- minute (0 - 59)[/size]
  10. [size=5]| .------------- hour (0 - 23)[/size]
  11. [size=5]| | .---------- day of month (1 - 31)[/size]
  12. [size=5]| | | .------- month (1 - 12) OR jan,feb,mar,apr …[/size]
  13. [size=5]| | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat[/size]
  14. [size=5]| | | | |[/size]
  15. [size=5]* * * * * user-name command to be executed[/size]
  16. [size=5]每分钟同步一次[/size]
  17. */1 * * * * root ntpdate aliyun
  18. [code]
  19. ## 4.Windows客户端配置
  20. ### 4.1 配置并启用客户端
  21. `Win键+r`输入`gpedit.msc`打开本地组策略编辑器,找到`配置 Windows NTP 客户端`(计算间配置-管理模板-系统-Windows 时间服务-时间提供程序)进行客户端配置:
  22. ![在这里插入图片描述](https://img-blog.csdnimg.cn/33a4eddcca1144139b311f3850cf4d86.png#pic_center)
  23. 配置页面:
  24. ![在这里插入图片描述](https://img-blog.csdnimg.cn/4cc63eea37fa4da8b198a40fb580919e.png#pic_center)
  25. 启用客户端配置:
  26. ![在这里插入图片描述](https://img-blog.csdnimg.cn/596d8588882a44d9a24b0ceb7e80a09a.png#pic_center)
  27. ### 4.2 使用客户端配置
  28. ![在这里插入图片描述](https://img-blog.csdnimg.cn/94dd2b447cc045aaac71af2c4d822eb8.png#pic_center)
  29. ## 5.ntp服务其他参数配置
  30. ### 5.1 查看ntp服务器有无和上层ntp连通
复制代码
[root@aliyun ~]# ntpstat
synchronised to NTP server (139.199.214.202) at stratum 3
time correct to within 1010 ms
polling server every 64 s
  1. ### 5.2 查看ntp服务器与上层ntp的状态
复制代码
[root@aliyun ~]# ntpq -p
remote refid st t when poll reach delay offset jitter


+skitty.itu.ch 84.16.67.12 2 u 49 64 17 174.891 20.582 5.712
+139.199.214.202 100.122.36.196 2 u 47 64 37 46.355 43.764 9.636
-time.cloudflare 10.28.12.207 3 u 49 64 37 216.628 31.101 12.642
*tick.ntp.infoma .GPS. 1 u 49 64 37 156.665 12.054 11.269
或者利用以下下令查看实时状态

[root@aliyun ~]# watch “ntpq -p”
  1. * remote:列出源的 IP 地址或主机名。第一列中的字符指示源的质量。星号 ( \* ) 表示该源是当前引用。
  2. * refid:参考上一层ntp主机地址。
  3. * st:stratum阶层。
  4. * when:多少秒前曾经同步过时间。指出从轮询源开始已过去的时间(秒)。
  5. * poll:下次更新在多少秒后。指出轮询间隔时间。该值会根据本地时钟的精度相应增加。
  6. * reach:已经向上层ntp服务器要求更新的次数。是一个八进制数字,指出源的可存取性。值 377 表示源已应答了前八个连续轮询。
  7. * delay:网络延迟。
  8. * offset:时间补偿。是源时钟与本地时钟的时间差(毫秒)。
  9. * jitter:系统时间与bios时间差。
  10. ### 5.3 从其他博客收集的信息
  11. #### 5.3.1 /etc/ntp.conf 配置内容
复制代码
driftfile /var/lib/ntp/drift
1. 权限设置 包括放行上层服务器以及开放局域网用户来源

restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1
放行局域网用户来源 或者列出单独IP

restrict xxx.xxx.xxx.x mask 255.255.255.0 nomodify
2. 设定主机来源 请先将原本的 [0|1|2|3].centos.pool.ntp.org 注掉

#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
以这部主机为最优先的server

server xxx.xxx.xxx.xx prefer
3. 默认的一个内部时钟数据 用在没有外部NTP服务器时 利用它为局域网用户提供服务

server 127.127.1.0
Fudge 127.127.1.0 stratum 8
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
  1. #### 5.3.2 restrict选项格式
  2. restrict [ 客户端IP ] mask [ IP掩码 ] [参数]
  3. `客户端IP`和`IP掩码`指定了对网络中哪些范围的计算机进行控制,如果使用default关键字,则表示对所有的计算机进行控制,参数指定了具体的限制内容,常见的参数如下:
  4. * ignore:拒绝连接到NTP服务器。
  5. * nomodiy: 客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
  6. * noquery: 不提供客户端的时间查询。
  7. * notrap: 不提供trap远程登录功能,trap服务是一种远程时间日志服务。
  8. * notrust: 客户端除非通过认证,否则该客户端来源将被视为不信任子网 。
  9. * nopeer: 提供时间服务,但不作为对等体。
  10. * kod: 向不安全的访问者发送Kiss-Of-Death报文。
  11. #### 5.3.3 server选项格式
  12. server host [ key n ] [ version n ] [ prefer ] [ mode n ] [ minpoll n ] [ maxpoll n ] [ iburst ]
  13. 其中host是上层NTP服务器的IP地址或域名,随后所跟的参数解释如下所示:
  14. * key: 表示所有发往服务器的报文包含有秘钥加密的认证信息,n是32位的整数,表示秘钥号。
  15. * version: 表示发往上层服务器的报文使用的版本号,n默认是3,可以是1或者2。
  16. * prefer: 如果有多个server选项,具有该参数的服务器有限使用。
  17. * mode: 指定数据报文mode字段的值。
  18. * minpoll: 指定与查询该服务器的最小时间间隔为2的n次方秒,n默认为6,范围为4-14。
  19. * maxpoll: 指定与查询该服务器的最大时间间隔为2的n次方秒,n默认为10,范围为4-14。
  20. * iburst: 当初始同步请求时,采用突发方式接连发送8个报文,时间间隔为2秒。
  21. #### 5.3.4 层次(stratum)
  22. stratum根据上层server的层次而设定(+1)。对于提供network time service provider的主机来说,stratum的设定要尽可能准确。而作为局域网的time service provider,通常将stratum设置为10。
  23. 0层的服务器采用的是原子钟、GPS钟等物理设备,stratum 1与stratum 0 是直接相连的,往后的stratum与上一层stratum通过网络相连,同一层的server也可以交互。ntpd对下层client来说是service server,对于上层server来说它是client。ntpd根据配置文件的参数决定是要为其他服务器提供时钟服务或者是从其他服务器同步时钟。所有的配置都在/etc/ntp.conf文件中。
  24. **先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前在阿里**
  25. **深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**
  26. **因此收集整理了一份《2024年最新Linux运维全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。**
  27. ![img](https://img-blog.csdnimg.cn/img_convert/4953d6c3753d7a8dd9a1c40bb6eb50e2.png)
  28. ![img](https://img-blog.csdnimg.cn/img_convert/79d2e93e9a61f91e1533c0bad7fa7009.png)
  29. ![img](https://img-blog.csdnimg.cn/img_convert/f5635b5fc27b81d68a0976b7c82e5277.png)
  30. ![img](https://img-blog.csdnimg.cn/img_convert/0a56ba64f3fb672812d2ed9a8321df18.png)
  31. ![img](https://img-blog.csdnimg.cn/img_convert/d59fa94ba723e45720c24065a424b6c1.png)
  32. **既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!**
  33. **由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
  34. **[需要这份系统化的资料的朋友,可以点击这里获取!](https://bbs.csdn.net/topics/618542503)**
  35. 795684)]
  36. [外链图片转存中...(img-766MGa6P-1714273795684)]
  37. [外链图片转存中...(img-mMMB97bf-1714273795684)]
  38. [外链图片转存中...(img-Px8HjsfD-1714273795685)]
  39. **既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!**
  40. **由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
  41. **[需要这份系统化的资料的朋友,可以点击这里获取!](https://bbs.csdn.net/topics/618542503)**
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

盛世宏图

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表