NTP(Network Time Protocol)是一种用于在计算机网络中同步各个节点时间的协议。NTP服务器是运行NTP协议的服务器,用于向客户端提供时间同步服务。
Linux操作系统计时方式主要是从 1970年1月1日开始计算总秒数的,因此,如果还记得date目录的话,会发现它有个+%s的参数,可以取得总秒数,这个就是软件时钟。但是,如同前面说的,计算机硬件主要是以BIOS内部的实际为主要的时间依据(硬件时钟),而偏偏这个时间可能因为BIOS内部的本身问题,而导致BIOS时间与标准时间(UTC)存在有一点点差异。所以,为了避免主机时间因为长期运行而导致时间偏差,进行时间同步的工作就显得很重要了
那么怎么让时间同步化呢?如果我们选择几部主要主机调校时间,让这些主要的主机时间同步之后,再开网络服务让Client端连接,并且允许Client端调制自己的时间,就可以实现全部计算机的时间同步了。NTP就可以实现这些功能。NTP是如何让Server与Client同步时间的呢?
(1)首先,主机当然需要启动这个daemon
(2)之后,Client会向NTP Server发送出校对时间的 message
(3)然后 NTP Server 会送出当前的标准时间给Client
(4)Client 接收了来自 Server 的时间后,会据以调整自己的时间,这样就实现了网络校时
NTP服务器是一种很容易安装的软件,但在不同的操作系统上可能有不同的名称。你只需要安装它并定义一个上层NTP服务器来同步你的时间就可以了。如果你只想同步你自己电脑的时间,不需要架设NTP服务器,直接使用NTP客户端软件即可。
若没有安装NTP服务,可以通过 yum install -y ntp 安装即可,不过还需要与时区相关的数据文件,所以需要下面的软件
与时间及NTP服务器设置相关的配置文件与重要数据文件有下面几个
常用时间服务器与修改时间命令,主要有下面这几个
/bin/date:用于Linux时间(软件时钟)的修改与显示的命令
/sbin/hwclock:用于BIOS时钟(硬件时钟)的修改与显示的命令
/usr/sbin/ntpd:主要提供NTP服务的程序。配置文件为:/etc/ntp.conf
/usr/sbin/ntpdate:用于客户端的时间校正,如果不要启用NTP而仅想要使用NTP Client功能的化,那么会用到这个命令。
在ntp.conf 文件内可以利用 restrict 来控制权限,这个参数的设置方式为
restrict [你的IP] mask [netmask_IP] [parameter]
其中parameter的参数主要有以下这些
如果没有在parameter的地方加上任何参数的话,这表示"该IP或网段不受任何限制"的意思。
上层NTP服务器设置方式为:
server [IP or hostname] [prefer]
# server后端可以接IP或主机名,一般来说设置IP
# perfer表示优先使用的服务器
设置的方式如下:
driftfile [可以被 ntpd 写入的目录与文件]
driftfile后面接的文件会被ntpd自动更新,所以它的权限一定要能够让ntpd写入才行
根据上面的说明我们最终可以取得这样的配置文件内容
[root@ntp-server ~]# cat /etc/ntp.conf
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
driftfile /var/lib/ntp/drift <==默认时间差异分析文件,不用改变他
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
# restrict default nomodify notrap nopeer noquery
restrict 192.168.200.0 mask 255.255.255.0 nomodify <== 放行局域网来源
# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
# 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 ntp.aliyun.com prefer <== ntp主机来源,阿里云的NTP服务器
.....(省略下面的内容).....
设置完ntp.conf 之后就可以启动 NTP服务器了。启动与查看的方式如下
[root@ntp-server ~]# systemctl start ntpd
[root@ntp-server ~]# systemctl enable ntpd
[root@ntp-server ~]# netstat -tulnp |grep ntp
udp 0 0 192.168.200.40:123 0.0.0.0:* 43270/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 43270/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 43270/ntpd
udp6 0 0 fe80::433b:8c3e:d01:123 :::* 43270/ntpd
udp6 0 0 ::1:123 :::* 43270/ntpd
udp6 0 0 :::123 :::* 43270/ntpd
# 主要是 UDP数据包,且在port 123 这个端口
这就表示NTP服务器已经启动了,不过要与上层NTP服务器连接则还需要一些时间,通常启动NTP后约3-4分钟才会和NTP服务器顺利连接上。那要如何确认NTP服务器已经顺利连接呢?
[root@ntp-server ~]# ntpstat
synchronised to NTP server (203.107.6.88) at stratum 3
time correct to within 474 ms
polling server every 64 s
# 这个命令可以列出我们NTP服务器是否已经与上层连接
# 由上述输出结果可以知道 时间已经校正约 474ms 且每隔 64秒会主动去更新时间
[root@ntp-server ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*203.107.6.88 100.107.25.114 2 u 34 64 377 28.679 18.169 7.925
这个ntpq -p 命令可以列出当前我们NTP与相关上层NTP的状态
上面几个字段意义如下
[root@ntp-server ~]# firewall-cmd --add-service=ntp --permanent
success
[root@ntp-server ~]# firewall-cmd --reload
success
在软件时钟方面,可以通过 date这个命令来进行手动修改,如果修改BIOS记录的时间,就要使用hwclock这个命令来写入才行
修改时间为1小时后
[root@ntp-client ~]# date -s "2023-06-19 14:15"
2023年 06月 19日 星期一 14:15:00 CST
hwclock [-rw]
选项:
-r:就是read,读出目前BIOS内的时间参数
-w:也就是write,将目前的系统时间写入 BIOS中
查阅BIOS时间,并写入更改过的时间
[root@ntp-client ~]# date; hwclock -r
2023年 06月 19日 星期一 14:15:10 CST
2023年06月19日 星期一 13时15分42秒 -0.115410 秒
# 刚刚相差1个小时,就是BIOS时间
[root@ntp-client ~]# hwclock -w;hwclock -r;date
2023年06月19日 星期一 14时18分19秒 -0.423185 秒
2023年 06月 19日 星期一 14:18:18 CST
# 这样就写入了,软件时钟和硬件时钟就同步了
在Linux环境中利用NTP的客户端程序,即ntpdate这个程序就能够进行时间同步。不过需要注意的是,因为NTP服务器本来就会与上层时间服务器进行实际的同步化,所以默认的情况下,NTP服务器不可以使用ntpdate,也就是说ntpdate与ntpd不可同时启用
,所以不要在NTP Server执行这条命令
ntpdate [-dv] [NTP IP/hostname]
选项:
-d:进入排错模式,可以显示出更对的有效信息
-v:显示更详细的信息
[root@ntp-client ~]# ntpdate 192.168.200.40
19 Jun 13:43:15 ntpdate[118643]: step time server 192.168.200.40 offset 66987584.766316 sec
# 最后面会显示微调的时间有多少
# 使用hwclock同步BIOS时间
[root@ntp-client ~]# date;hwclock -w
2023年 06月 19日 星期一 13:45:01 CST
[root@ntp-client ~]# vi /etc/crontab
# 加入这一行
*/60 * * * * root (/usr/sbin/ntpdate 192.168.200.40 && /sbin/hwclock -w) >> /tmp/localntp.log
使用crontab之后,每60分钟Linux系统就会自动进行校时了。不过这种方式仅适合不启动NTP的情况。
如果机器数量很多,那么客户端也最好启动一下NTP服务器,通过NTP去主动地更新时间
[root@ntp-client ~]# ntpdate 192.168.200.40
# 由于ntpd的Server/Client 之间的时间误差不允许超过 1000 秒
# 因此需要先手动进行时间同步,然后再设置与启动时间服务器
[root@ntp-client ~]# vi /etc/ntp.conf
#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 192.168.200.40
# 就是将原本的Server注释掉,加入我们要的服务器即可
[root@ntp-client ~]# systemctl start ntpd
[root@ntp-client ~]# systemctl enable ntpd
[root@ntp-client ~]# ntpstat
synchronised to NTP server (192.168.200.40) at stratum 4
time correct to within 1043 ms
polling server every 64 s
[root@ntp-client ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*192.168.200.40 203.107.6.88 3 u 28 64 17 0.974 0.336 0.059
然后取消掉crontab的更新程序,这样Client 计算机会主动到NTP服务器上去更新了。