Linux服务篇-NTP-网络时间协议

    科技2022-07-11  111

    一、NTP-网络时间协议

    1、简介

    NTP(Network Time Protocol),网络时间协议 是用来使计算机时间同步化的一种协议。它可以使计算机对其服务器或时钟源做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可介由加密确认的方式来防止恶毒的协议攻击 模式:c/s模式 端口:123

    2、安装

    服务端:ntp 客户端ntpdate 1、挂载安装

    [root@qianshuiluyu ~]# rpm -ivh /mnt/Packages/ntpdate-4.2.6p5-18.el7.x86_64.rpm

    或者 2、查找配置文件

    3、启动

    [root@qianshuiluyu ~]# systemctl start ntpd [root@qianshuiluyu ~]# systemctl enable ntpd ln -s '/usr/lib/systemd/system/ntpd.service' '/etc/systemd/system/multi-user.target.wants/ntpd.service'

    4、查看端口

    3、命令

    ntpdate

    ntpdate[NTP IP/hostname]-u防火墙有可能不让通过±u-ddubug模式

    tpq -p 列出我们的NTP服务器与上游服务器的连接状态 ntpstat 列出是否上游服务器连接,需要时间

    同步时,必须保证网络的畅通

    tpq -p

    remote:即remote - 本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先。 refid:参考的上一层NTP主机的地址 st:即stratum阶层 poll:下次更新在几秒之后 offset:时间补偿的结果 when:几秒前曾做过时间同步更新的操作 reach:已经向上层NTP服务器要求更新的次数 delay:网络传输过程钟延迟的时间 jitter:Linux系统时间与BIOS硬件时间的差异时间

    4、配置文件

    [root@qianshuiluyu ~]# vim /etc/ntp.conf restrict 127.0.0.1 restrict ::1 restrict 192.172.168.0 mask 255.255.255.0 # 允许哪些网段或者主机进行同步 server ntp1.aliyun.com # 阿里 server 210.72.145.44 # 中国国家授时中心 server :ntp.api.bz # NTP服务器(上海) server 192.172.168.1 server 0.rhel.pool.ntp.org iburst server 1.rhel.pool.ntp.org iburst server 2.rhel.pool.ntp.org iburst server 3.rhel.pool.ntp.org iburst # ntp自带的同步时间的域名,0.rhel.pool.ntp.org # server,指定本NTP服务器的上游NTP服务器为192.172.168.1 # server的优先级,从上到下,写的越靠上,优先级越高,上面的坏了就用下面的 server 127.127.1.0 # 上面的都没有同步就同步本机的 fudge 127.127.1.0 stratum 10 # 127.127.1.0 为第10层, # ntp层次阶级,同步完127.127.1.0之后为第十层,必能超过15,类似于ttl

    2、如果有问题就排下错 (1)

    [root@qianshuiliyu_client ~]# ntpdate 192.172.168.100 3 Oct 17:17:27 ntpdate[4099]: the NTP socket is in use, exiting

    已经运行了ntp服务的机器,执行同步命令会不成功

    [root@qianshuiliyu_client ~]# ntpdate 192.172.168.100 3 Oct 17:17:27 ntpdate[4099]: the NTP socket is in use, exiting

    出现该问题的原因是系统ntpd服务器正在运行中,通过 ps aux | grep ntpd 查看,会看到ntpd正在运行。

    解决:将服务关闭重启或者拿掉进程 (2)

    [root@qianshuiliyu_client ~]# ntpdate 192.172.168.100 3 Oct 18:32:34 ntpdate[4433]: no server suitable for synchronization found # 由于每次重启NTP服务器之后大约要3-5分钟客户端才能与server建立正常的通讯连接。当 # 此时用客户端连接服务端就会报这样的信息。一般等待几分钟就可以了。

    3、查看

    在启动NTP服务前,先对提供服务的这台主机手动的校正一次时间。 ntpd 有一个自我保护设置: 如果本机与上源时间相差太大, ntpd 不运行 所以新设置的时间服务器一定要先 ntpdate 从上源取得时间初值, 然后启动 ntpd服务。ntpd服务 运行后, 先是每64秒与上源服务器同步一次, 根据每次同步时测得的误差值经复杂计算逐步调整自己的时间, 随着误差减小, 逐步增加同步的间隔. 每次跳动, 都会重复这个调整的过程

    4、同步

    [root@qianshuiliyu ~]# ntpdate ntp1.aliyun.com 3 Oct 18:15:29 ntpdate[22326]: adjust time server 120.25.115.20 offset -0.000487 sec

    5、定期同步

    理想做法,在开机的时候,使用ntpdate强制同步时间,在其他时候使用ntpd服务来同步时间。

    [root@qianshuiluyu ~]# crontab -e 0 12 * * * /usr/sbin/ntpdate 192.172.168.100

    6、相关

    两个时间:系统时间与bios时间,现在通常都一样

    查看bios时间

    [root@qianshuiluyu ~]# hwclock -r 2020年10月03日 星期六 18时38分28秒 -0.959731 秒 [root@qianshuiluyu ~]# hwclock -w # -w,将当前系统时间写入BIOS中

    7、同步硬件时间

    默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpdate 文件。 在/etc/sysconfig/ntpdate 文件中,把SYNC_HWCLOCK=no 改成SYNC_HWCLOCK=yes 就可以让硬件时间与系统时间一起同步

    Processed: 0.043, SQL: 8