IT评测·应用市场-qidao123.com技术社区

标题: CentOS 7内网服务器NTP时间校准与同步 [打印本页]

作者: 慢吞云雾缓吐愁    时间: 2024-6-15 02:09
标题: CentOS 7内网服务器NTP时间校准与同步
不就校准一个时间吗?有多难?

服务器时间差别步,最简朴粗暴的方法是:直接用命令手动改时间。
  1. timedatectl set-timezone "Asia/Shanghai"
  2. date -s "2024-2-6 21:00:00"
复制代码
先修改时区为上海,再修改时间,以是,so easy ,本文结束…
为什么?难受!这很不优雅,别问我为什么,总之无法接受
该死的完美主义,先明白需求吧!

还提要求,先看看自己有什么?

好,那就开干!

服务器1:172.0.0.1 计划作 当地 NTP 服务器
服务器2:172.0.0.2 NTP 客户端1
服务器3:172.0.0.3 NTP 客户端2
服务器4:172.0.0.4 NTP 客户端3
我的电脑:172.0.0.5 能上网,为什么,因为我有两张网卡,(192.168.1.1)
1.外网NTP服务器校准内网服务器时间

先安装个软件包,
  1. yum install ntp
复制代码
什么?装不上,都没网,代理上网不会吗?
  1. export http_proxy=http://172.0.0.5:1556
  2. export https_proxy=http://172.0.0.5:1556
  3. yum install ntp
复制代码
好,软件包装好了,开始校准时间。
  1. ntpdate ntp.aliyun.com
复制代码
用阿里的服务器校一校,很遗憾,服务器根本没配置DNS
  1. 6 Feb 21:09:08 ntpdate[249451]: Can't find host ntp.aliyun.com: Name or service not known (-2)
  2. 6 Feb 21:09:08 ntpdate[249451]: no servers can be used, exiting
复制代码
那我自己直接用IP,用自己的电脑先看看IP
  1. ping ntp.aliyun.com
  2. PING ntp.aliyun.com (203.107.6.88) 56(84) bytes of data.
复制代码
再校一校试试
  1. ntpdate 203.107.6.88
复制代码
照旧不可。
  1. 6 Feb 21:12:53 ntpdate[60842]: no server suitable for synchronization found
复制代码
没网?不可能,代理不了?果然,ntp服务利用的是UDP协议,端口为123。
那就在路由器上配个UDP端口转发,将123端口转发到203.107.6.88的123端口。
搞定!
这路由器不简朴,可以NAT到互联网,简化了许多操作。没有路由器可以用其他方法,核心就是将外网的NTP服务器UDP端口123映射内网中,这时间路由器(172.0.0.100)就相当于内网的NTP服务器了。
2.内网自建NTP服务器

以上全部服务器都能用路由器来校准时间,为什么还要内网自建NTP服务器呢?因为考虑到服务器之间的时间同步问题,表里网络情况稳固,外网波动大,路由器随时可以撤。某些场景下外网是完全隔离的。
编辑一下配置文件
  1. vim /etc/ntp.conf
复制代码
参加这两条,第一条是刚配置的转发路由器地址,第二条是它自己,当外部时间不可用时,利用当地时间。把其他不用的服务器注释即可。
  1. # server 0.centos.pool.ntp.org iburst
  2. # server 1.centos.pool.ntp.org iburst
  3. # server 2.centos.pool.ntp.org iburst
  4. # server 3.centos.pool.ntp.org iburst
  5. server 172.0.0.100 iburst
  6. server 127.127.1.0 iburst
复制代码
其他看着改,完整配置文件/etc/ntp.conf
  1. # For more information about this file, see the man pages
  2. # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
  3. driftfile /var/lib/ntp/drift
  4. # Permit time synchronization with our time source, but do not
  5. # permit the source to query or modify the service on this system.
  6. # restrict default nomodify notrap nopeer noquery
  7. # Permit all access over the loopback interface.  This could
  8. # be tightened as well, but to do so would effect some of
  9. # the administrative functions.
  10. # restrict 127.0.0.1
  11. # restrict ::1
  12. # Hosts on local network are less restricted.
  13. restrict 172.0.0.0 mask 255.255.255.0 nomodify notrap
  14. # Use public servers from the pool.ntp.org project.
  15. # Please consider joining the pool (http://www.pool.ntp.org/join.html).
  16. # server 0.centos.pool.ntp.org iburst
  17. # server 1.centos.pool.ntp.org iburst
  18. # server 2.centos.pool.ntp.org iburst
  19. # server 3.centos.pool.ntp.org iburst
  20. server 172.0.0.100 iburst
  21. server 127.127.1.0 iburst
  22. # fudge 127.127.1.0 stratum 8
  23. #broadcast 192.168.1.255 autokey        # broadcast server
  24. #broadcastclient                        # broadcast client
  25. #broadcast 224.0.1.1 autokey            # multicast server
  26. #multicastclient 224.0.1.1              # multicast client
  27. #manycastserver 239.255.254.254         # manycast server
  28. #manycastclient 239.255.254.254 autokey # manycast client
  29. # Enable public key cryptography.
  30. #crypto
  31. includefile /etc/ntp/crypto/pw
  32. # Key file containing the keys and key identifiers used when operating
  33. # with symmetric key cryptography.
  34. keys /etc/ntp/keys
  35. # Specify the key identifiers which are trusted.
  36. #trustedkey 4 8 42
  37. # Specify the key identifier to use with the ntpdc utility.
  38. #requestkey 8
  39. # Specify the key identifier to use with the ntpq utility.
  40. #controlkey 8
  41. # Enable writing of statistics records.
  42. #statistics clockstats cryptostats loopstats peerstats
  43. # Disable the monitoring facility to prevent amplification attacks using ntpdc
  44. # monlist command when default restrict does not include the noquery flag. See
  45. # CVE-2013-5211 for more details.
  46. # Note: Monitoring will not be disabled with the limited restriction flag.
  47. disable monitor
复制代码
启动NTP服务,并设置开机自启
  1. systemctl enable ntpd
  2. systemctl start ntpd
复制代码
查看一下状态
  1. [root@server root]# ntpq -p
  2.      remote           refid      st t when poll reach   delay   offset  jitter
  3. ==============================================================================
  4. *172.0.0.1       101.207.21.141   2 u   30  128  377   43.155    7.632   2.896
  5. LOCAL(0)        .LOCL.           5 l  72m   64    0    0.000    0.000   0.000
复制代码
3.配置NTP客户端

客户端比力简朴,全部服务器先跟表里NTP服务器同步一下时间。
  1. ntpdate -u 172.0.0.1
复制代码
把时间同步写入硬件中
  1. hwclock -w
复制代码
每台客户机设置一个定时任务,每小时同步一下时间。
  1. crontab -e
  2. */60 * * * * /usr/sbin/ntpdate 172.0.0.1 >>/tmp/ntp.log
复制代码
大功告成。
参考资料

   Linux系统主动校准时间 > https://blog.csdn.net/qq_45361058/article/details/101153863
centos7设置时区,时间+时间同步的三种方式 > https://blog.csdn.net/Liu__sir__/article/details/130635044
手把手教你在centos 7.4上搭建NTP服务器 > https://developer.aliyun.com/article/946051

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4