**环境配置** |服务器|IP地址| | :------------: | :------------: | |NTP服务器|10.100.200.11| |NTP客户端|10.100.200.10| ------------ ------------ # 一、为什么要使用ntpd而不是ntpdate? 原因很简单,ntpd是步进式的逐渐调整时间,而ntpdate是断点更新,比如现在服务器时间是9.18分,而标准时间是9.28分,ntpd会在一段时间内逐渐的把时间校准到与标准时间相同,而ntpdate会立刻把时间调整到9.28分,如果你往数据库内写入内容或在其他对时间有严格要求的生产环境下,产生的后果会是很严重的。(注:当本地时间与标准时间相差30分钟以上是ntpd会停止工作) # 二、NTP通信协议原理 1.首先主机启动NTP。 2.客户端会向NTP服务器发送调整时间的message。 3.然后NTP server会送出当前的标准时间给client 4.client接受来自server的时间后,会根据这个信息来调整自己的时间。这样就实现了网络对时。 NTP这个deamon采用了123端口。(UDP) 当我们要利用Tim server来进行实践的同步更新时,就需要使用NTP软件提供的ntpdate来连接端口123 **与时间相关的一些命令和配置文件** 1./etc/ntp.conf linux各版本虽然目录不同,但文件名相同。可以用which ntp.conf 或者locate ntp.conf来查找。这是NTP唯一的一个设置文件。 2./usr/share/zoneinfo/ 这个里面规定了这个主要时区的时间设置文件。 3./etc/localtime 本地端时间配置文件。 4./bin/date 这个是时间的修改命令,除了输出时间,还可以修改时间。 5./sbin/hwclock 因为linux系统上面BIOS时间与linux系统时间是分开的,所以使用date这个指令调整了时间之后,还需要使用hwclock才能将修改过的时间写入BIOS中。 6./usr/sbin/ntpd 这是NTP的daemon文件,需要启动它才能提供NTP服务,这个命令会读取/etc/ntp.conf里面的设置。 7./usr/sbin/ntpdate 这是client用来连接NTP Server的主要执行文件,如果您不想启用NTP,只想启用NTP Client功能的话,可以只应用此命令。 # 三、安装与配置 ## 1、安装ntpd服务 ```shell yum -y install ntp ``` ## 2、配置ntpd ```shell vim /etc/ntp.conf ``` 如下图修改配置,可以修改上层ntp,注释原先的server,新增自己的server ![修改ntp配置.png](https://www.xiaoleizhang.com/usr/uploads/2022/12/1770445267.png) ```shell vim /etc/sysconfig/ntpd ``` 在文件下新增 ```shell # Set to 'yes' to sync hw clock after successful ntpdate SYNC_HWCLOCK=yes #make no into yes; BIOS的时间也会跟着修改 ``` # 四、验证与状态检查 将ntpd重启和开机自启 ```shell systemctl enable ntpd.service systemctl restart ntpd.service ``` 查看ntp端口 可以看到udp的123端口监听 ```shell [root@localhost ~]# netstat -unlnp | grep 123 udp 0 0 10.100.200.11:123 0.0.0.0:* 2632/ntpd udp 0 0 127.0.0.1:123 0.0.0.0:* 2632/ntpd udp 0 0 0.0.0.0:123 0.0.0.0:* 2632/ntpd udp6 0 0 ::1:123 :::* 2632/ntpd udp6 0 0 fe80::98ae:39a7:972:123 :::* 2632/ntpd udp6 0 0 :::123 :::* 2632/ntpd [root@localhost ~]# ``` 查看ntp服务器有无和上层连通 ```shell [root@localhost ~]# ntpstat synchronised to NTP server (120.25.115.20) at stratum 3 time correct to within 965 ms polling server every 64 s [root@localhost ~]# ``` 如果出现下面的这种情况属于正常,ntp服务器配置完毕后,需要等待5-10分钟才能与/etc/ntp.conf中配置的标准时间进行同步。 ```shell unsynchronised time server re-starting polling server every 8 s ``` 等一段时间之后,再次使用ntpstat命令查看状态,就会变成上面的正常结果: 查看ntp服务器与上层ntp的状态 ```shell [root@localhost ~]# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== *120.25.115.20 10.137.53.7 2 u 70 64 172 36.120 1.326 4.244 [root@localhost ~]# ``` remote:本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先 refid:参考上一层ntp主机地址 st:stratum阶层 when:多少秒前曾经同步过时间 poll:下次更新在多少秒后 reach:已经向上层ntp服务器要求更新的次数 delay:网络延迟 offset:时间补偿 jitter:系统时间与bios时间差 如果所有远程服务器的jitter值是4000并且delay和reach的值是0,那么说明时间同步是有问题的 可能的原因是防火墙阻断了与server之间的通讯,即123端口是否正常开放; # 五、客户端配置 安装ntpd,指定时间server为上面配置的服务器地址 ## 1、安装ntpd服务 ```shell yum -y install ntp ``` ## 2、配置ntpd ```shell vim /etc/ntp.conf ``` 如下图修改配置,注释原先的server,新增自己搭建的NTP服务器(10.100.200.11) ![客户端配置ntp.png](https://www.xiaoleizhang.com/usr/uploads/2022/12/294891490.png) ## 3、验证ntpd 可以看到ntp服务器地址是10.100.200.11 ```shell [root@localhost ~]# ntpstat synchronised to NTP server (10.100.200.11) at stratum 4 time correct to within 483 ms polling server every 128 s [root@localhost ~]# [root@localhost ~]# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== *10.100.200.11 120.25.115.20 3 u 129 128 377 0.885 3.487 0.533 [root@localhost ~]# ``` 最后修改:2022 年 12 月 21 日 © 允许规范转载 赞 0 如果觉得我的文章对你有用,请随意赞赏