LinuxNTP服务器搭建部署与NTP时间同步配置Linux系统时区设置
timedatectl set-timezone Asia/Shanghai (这⾥是设置亚洲上海上海)
Linux NTP服务器部署安装及配置
⼀、linux NTP服务器部署安装
1、验证服务器端NTP服务是否已安装
[root@vm ~]# rpm -qa ntp*
ntpdate-4.2.s.2.x86_64
ntp-4.2.s.2.x86_64
2、若系统为安装ntp服务,可通过rpm或yum进⾏安装
[root@vm ~]# yum install ntp -y
[root@vm ~]# rpm -ql ntp
/
f    # ntp服务器的主配置⽂件
/etc/rc.d/init.d/ntpd    # 开机启动ntpd脚本⽂件
3、国内稳定NTP时间同步服务器域名
times.aliyun
ntp.aliyun
4、使⽤ntpdate同步时间服务器
[root@vm ~]# ntpdate times.aliyun
17 Oct 10:05:27 ntpdate[26878]: step time server 120.25.115.19 offset 4.115593 sec
⼆、linux NTP服务配置
1、NTP服务 f 参数详解
默认f内容如下
[root@vm ~]# vi /f
#系统时间与BIOS事件的偏差记录
driftfile /var/lib/ntp/drift
restrict default kod nomodify notrap nopeer noquery    # 拒绝所有IPv4的client连接此NTP服务器
restrict -6 default kod nomodify notrap nopeer noquery    # 拒绝所有IPv6的client连接此NTP服务器
restrict 127.0.0.1    # 放⾏本机localhost对NTP服务的访问
restrict -6 ::1
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap    # 放⾏192.168.1.0⽹段主机与NTP服务器进⾏时间同步
# Use public servers from p.org project.
# Please consider joining the pool (p.org/join.html).
p.org iburst    # 代表的同步时间服务器
p.org iburst
p.org iburst
p.org iburst
#broadcast 192.168.1.255 autokey        # broadcast server
#broadcastclient                        # broadcast client
#broadcast 224.0.1.1 autokey            # multicast server
#multicastclient 224.0.1.1              # multicast client
#manycastserver 239.255.254.254        # manycast server
#manycastclient 239.255.254.254 autokey # manycast client
# Enable public key cryptography.
#crypto
includefile /etc/ntp/crypto/pw
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys
----
各项参数详解
利⽤restrict 来管理权限控制
Restrict [IP] mask [netmask_IP] [parameter]
Parameter 的
ignore :拒绝所有类型的NTP联机。
nomodify: 客户端不能使⽤ntpc与ntpq这两个程序来修改服务器的时间参数,但客户端可透过这部主机来进⾏⽹络校时;
noquery:客户端不能够使⽤ntpc与ntpq等指令来查询时间服务器,不提供NTP的⽹络校时。
notrap:不提供trap 这个运程事件登⼊的功能。
notrust:拒绝没有认证的客户端。
Kod:kod技术可以阻⽌“Kiss of Death “包对服务器的破坏。
Nopeer:不与其他同⼀层的NTP服务器进⾏时间同步。
利⽤server 设定上层NTP服务器,格式如下:
server [IP or hostname] [prefer]
参数主要如下:
perfer:表⽰优先级最⾼
burst :当⼀个运程NTP服务器可⽤时,向它发送⼀系列的并发包进⾏检测。
iburst :当⼀个运程NTP服务器不可⽤时,向它发送⼀系列的并发包进⾏检测。
2、配置NTPserver
调整NTP配置⽂件\
[root@vm ~]# vi /f
Hosts on local network are less restricted.
允许局域⽹内设备与这台服务器进⾏同步时间.但是拒绝让他们修改服务器上的时间
restrict 192.168.23.0 mask 255.255.255.0 nomodify notrap
restrict 192.168.10.0 mask 255.255.255.0 nomodify notrap
restrict 192.168.20.0 mask 255.255.255.0 nomodify notrap
restrict 172.16.10.0 mask 255.255.255.0 nomodify notrap
restrict 192.168.100.0 mask 255.255.255.0 nomodify notrap
允许上层时间服务器修改本机时间
restrict times.aliyun nomodify
restrict ntp.aliyun nomodify
restrict p.org nomodify
定义要同步的时间服务器
server times.aliyun iburst prefer # prefer表⽰为优先,表⽰本机优先同步该服务器时间
server ntp.aliyun iburst
免费永久的linux服务器server p.org iburst
logfile /var/log/ntpstats/ntpd.log # 定义ntp⽇志⽬录
pidfile /var/run/ntp.pid # 定义pid路径
Key file containing the keys and key identifiers used when operating
with symmetric key cryptography.
keys /etc/ntp/keys
ntp服务启动
[root@vm ~]# chkconfig ntpd on
[root@vm ~]# service ntpd start
Starting ntpd: [ OK ]
[root@vm ~]# ss -tunlp | grep ntp
udp UNCONN 0 0 192.168.23.100:123 : users:(("ntpd",24250,19))
udp UNCONN 0 0 127.0.0.1:123 : users:(("ntpd",24250,18))
udp UNCONN 0 0 :123 : users:(("ntpd",24250,16))
udp UNCONN 0 0 fe80::92b1:1cff:fe10:3a44:123 ::: users:(("ntpd",24250,21))
udp UNCONN 0 0 ::1:123 ::  users:(("ntpd",24250,20))
udp UNCONN 0 0 :::123 ::  users:(("ntpd",24250,17))
3、实现clock时间与system时间同步,配置/etc/sysconfig/ntpd⽂件
ntp服务,默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd⽂件,在/etc/sysconfig/ntpd⽂件中,添加 SYNC_HWCLOCK=yes 这样,就可以让硬件时间与系统时间⼀起同步。[root@vm ~]# vi /etc/sysconfig/ntpd # 实现硬件时间与系统时间同步
Drop root to id 'ntp:ntp' by default.
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"
SYNC_HWCLOCK=yes
4、查看ntp服务器同步状态
[root@vm ~]# ntpstat # 确认本地NTP与上层NTP服务器是否联通
synchronised to NTP server (120.25.115.19) at stratum 10 #本NTP服务器层次为10,已向120.25.115.
19 NTP同步过
time correct to within 33 ms # 时间校正到相差33ms之内
polling server every 256 s # 每256秒会向上级NTP轮询更新⼀次时间
[root@vm ~]# ntpq -p # 查看本地NTP需进⾏同步的公⽹NTP服务器状态remote refid st t when poll reach delay offset jitter
*203.107.6.88 100.107.25.114 2 u 306 1024 377 15.067 -0.283 1.171
+120.25.115.19 10.137.53.7 2 u 979 1024 377 36.893 -0.671 1.219
+119.28.183.184 100.122.36.196 2 u 1036 1024 377 49.942 7.536 18.648
参数详解
remote :本地主机所连接的上层NTP服务器,最左边的符号如下:
如果有[*] 代表⽬前正在使⽤当中的上层NTP服务器。
如果有[+] 代表也有连上上层NTP服务器,可以作为提⾼时间更新的候选NTP服务器
如果有[-] 代表同步的该NTP服务器被认为是不合格的NTP Server
如果有[x] 代表同步的外⽹NTP服务器不可⽤
refid :指的是给上层NTP服务器提供时间校对的服务器。
St:上层NTP服务器的级别。
When: 上⼀次与上层NTP服务器进⾏时间校对的时间(单位:s)
Poll :本地主机与上层NTP服务器进⾏时间校对的周期(单位:s)
reach:已经向上层 NTP 服务器要求更新的次数
delay:⽹络传输过程当中延迟的时间,单位为 10^(-6) 秒
offset:时间补偿的结果,单位为10^(-6) 秒
jitter:Linux 系统时间与 BIOS 硬件时间的差异时间,单位为 10^(-6) 秒。
**NTP client 与NTP Server时间同步配置**
-
-----------------------------------------------
**⼀、linux 客户端与ntp服务器时间同步部署**
可通过两种⽅法实现,第⼀种配置周期性任务计划时间同步,第⼆种⽅法配置ntp服务
1、配置周期性时间同步任务
[root@zabbix ~]# yum install crontabs -y
[root@zabbix ~]# chkconfig crond on
[root@zabbix ~]# service crond start # 启动crond服务
[root@zabbix ~]# vi /etc/crontab # 配置周期性时间同步任务
Example of job definition:
.---------------- minute (0 - 59)
| .------------- hour (0 - 23)
| | .---------- day of month (1 - 31)
| | | .------- month (1 - 12) OR jan,feb,mar,apr ...
| | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat | | | | |
* * * * * user-name command to be executed
0 1 * * * root /usr/sbin/ntpdate 192.168.23.100 &> /dev/null ;/sbin/hwclock -w &> /dev/null
表⽰每天的1:00执⾏/usr/sbin/ntpdate 192.168.23.100 NTP服务器,同时同步硬件时钟,但不显⽰任何信息
2、linux客户端配置ntp服务
[root@zabbix ~]# yum install ntp ntpdate -y
[root@zabbix ~]# more /etc/sysconfig/clock
ZONE="Asia/Shanghai"
[root@zabbix ~]# date -R
Wed, 17 Oct 2018 13:53:34 +0800
[root@zabbix ~]# ntpdate 192.168.23.100
17 Oct 13:55:08 ntpdate[11772]: adjust time server 192.168.23.100 offset 0.463749 sec
[root@zabbix ~]# vi /f # 调整同步的时间服务器
server 192.168.23.100 prefer
restrict 192.168.23.100 nomodify notrap noqurey
[root@zabbix ~]# vi /etc/sysconfig/ntpd # 实现硬件时间与系统时间同步
Drop root to id 'ntp:ntp' by default.
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"
SYNC_HWCLOCK=yes
[root@zabbix ~]# service ntpd start
Starting ntpd: [ OK ]
[root@zabbix ~]# ntpq -p # 查看时间同步状态
remote refid st t when poll reach delay offset jitter
bogon 120.25.115.19 3 u 3 64 1 0.651 35.932 0.000
[root@zabbix ~]# ntpstat
synchronised to NTP server (192.168.23.100) at stratum 4 #本NTP服务器层次为4,已向203.107.6.88 NTP同步过
time correct to within 37 ms # 时间校正到相差37ms之内
polling server every 128 s # 每128秒会向上级NTP轮询更新⼀次时间
**⼆、Windows Server客户端与ntp服务器时间同步部署**
1、进⼊⽇期与时间,选择更改设置
!
[](img2020blogs/blog/1784001/202112/1784001-20211201140726624-2107018835.png)
2、输⼊ntp服务器地址,并点击“⽴即更新”,确定即可。
![](img2020blogs/blog/1784001/202112/1784001-20211201140748501-1866758013.png)
**注意:ntp与ntpdate**
ntpd、ntpdate的区别,下⾯是⽹上关于ntpd与ntpdate区别的相关资料。如下所⽰所⽰:
使⽤之前得弄清楚⼀个问题,ntpd与ntpdate在更新时间时有什么区别。ntpd不仅仅是时间同步服务器,它还可以做客户端与标准时间服务器进⾏同步时间,⽽且是平滑同步,并⾮ntpdate⽴即同步,在⽣产环境中慎⽤ntpdate,也正如此两者不可同时运⾏。
时钟的跃变,对于某些程序会导致很严重的问题。许多应⽤程序依赖连续的时钟,例如数据库事务。ntpdate调整时间的⽅式就是我们所说的”跃变“:在获得⼀个时间之
后,ntpdate使⽤settimeofday(2)设置系统时间,这有⼏个⾮常明显的问题:
第⼀,这样做不安全。ntpdate的设置依赖于ntp服务器的安全性,***者可以利⽤⼀些软件设计上的缺陷,拿下ntp服务器并令与其同步的服务器执⾏某些消耗性的任务。由于ntpdate采⽤的⽅式是跳变,跟随它的服务器⽆法知道是否发⽣了异常(时间不⼀样的时候,唯⼀的办法是以服务器为准)。
第⼆,这样做不精确。⼀旦ntp服务器宕机,跟随它的服务器也就会⽆法同步时间。与此不同,ntpd不仅能够校准计算机的时间,⽽且能够校准计算机的时钟。
第三,这样做不够优雅。由于是跳变,⽽不是使时间变快或变慢,依赖时序的程序会出错(例如,如果ntpdate发现你的时间快了,则可能会经历两个相同的时刻,对某些应⽤⽽⾔,这是致命的)。因⽽,唯⼀⼀个可以令时间发⽣跳变的点,是计算机刚刚启动,但还没有启动很多服务的那个时候。其余的时候,理想的做法是使⽤ntpd来校准时钟,⽽不是调整计算机时钟上的时间。
NTPD 在和时间服务器的同步过程中,会把 BIOS 计时器的振荡频率偏差——或者说 Local Clock 的⾃然漂移(drift)——记录下来。这样即使⽹络有问题,本机仍然能维持⼀个相当精确的⾛时。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。