GHOST系统之家 - Windows系统光盘下载网站!
当前位置:GHOST系统之家>系统问题 > Linux系统下NTP协议的超级配置攻略

Linux系统下NTP协议的超级配置攻略

来源:Ghost系统之家浏览:时间:2023-04-05 11:16:05
在Linux系统中,为了避免主机时间因为在长时间运行下所导致的时间偏差,进行时间同步(synchronize)的工作是非常必要的。Linux系统下,一般使用ntp服务来同步不同机器的时间。NTP 是网络时间协议(Network Time Protocol)的简称,干嘛用的呢?就是通过网络协议使计算机之间的时间同步化。

安装NTP包

检查是否安装了ntp相关包。如果没有安装ntp相关包,使用rpm或yum安装,安装也非常简单方便。
 

复制代码代码如下:
[root@localhost ~]#rpm -qa | grep
ntpntpdate-4.2.6p5-1.el6.x86_64
fontpackages-filesystem-1.41-1.1.el6.noarch
ntp-4.2.6p5-1.el6.x86_64
 

NTP的配置


A: 配置/etc/ntp.conf

NTP Server的主要配置文件为/etc/ntp.conf ,没有修改过的ntp.conf文件内容如下所示,配置选项都有相关注释信息(Linux 版本为Red Hat Enterprise Linux Server release 6.6 )

复制代码代码如下:
[root@localhost ~]# more /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 kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery

# Permit all access over the loopback interface.This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict -6 ::1

# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
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


#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

# Specify the key identifiers which are trusted.
#trustedkey 4 8 42

# Specify the key identifier to use with the ntpdc utility.
#requestkey 8

# Specify the key identifier to use with the ntpq utility.
#controlkey 8

# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats
[root@localhost ~]# more /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 kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery

# Permit all access over the loopback interface.This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict -6 ::1

# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
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


#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

# Specify the key identifiers which are trusted.
#trustedkey 4 8 42

# Specify the key identifier to use with the ntpdc utility.
#requestkey 8

# Specify the key identifier to use with the ntpq utility.
#controlkey 8

# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats
各个选项信息:
#系统时间与BIOS事件的偏差记录
driftfile /etc/ntp/drift

 

restrict 控制相关权限。

语法为: restrict IP地址 mask 子网掩码 参数

其中IP地址也可以是default ,default 就是指所有的IP

参数有以下几个:

ignore  :关闭所有的 NTP 联机服务

nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。

notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网

noquery :不提供客户端的时间查询:用户端不能使用ntpq,ntpc等命令来查询ntp服务器

notrap :不提供trap远端登陆:拒绝为匹配的主机提供模式 6 控制消息陷阱服务。陷阱服务是 ntpdq 控制消息协议的子系统,用于远程事件日志记录程序。

nopeer :用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟

kod : 访问违规时发送 KoD 包。

restrict -6 表示IPV6地址的权限设置。

1:设定NTP主机来源(其中prefer表示优先主机),192.168.7.49是本地的NTP服务器,所以优先指定从该主机同步时间。
 

复制代码代码如下:
server 192.168.7.49 prefer
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


2:限制你允许的这些服务器的访问类型,在这个例子中的服务器是不容许修改运行时配置或查询您的Linux NTP服务器
 

复制代码代码如下:
restrict 192.168.0.0 mask 255.255.255.0 notrust nomodify notrap

在上例中,掩码地址扩展为255,因此从192.168.0.1-192.168.0.254的服务器都可以使用我们的NTP服务器来同步时间

 

复制代码代码如下:
#此时表示限制向从192.168.0.1-192.168.0.254这些IP段的服务器提供NTP服务。
restrict 192.168.0.0 mask 255.255.255.0 notrust nomodify notrap noquery

#设置默认策略为允许任何主机进行时间同步
restrict default ignore

 

3:确保localhost(这个常用的IP地址用来指Linux服务器本身)有足够权限.使用没有任何限制关键词的语法:

复制代码代码如下:
restrict 127.0.0.1
restrict -6 ::1

 

B:配置/etc/ntp/stpe-tickers文件

修改/etc/ntp/stpe-tickers文件,内容如下(当ntpd服务启动时,会自动与该文件中记录的上层NTP服务进行时间校对)

复制代码代码如下:
[root@localhost ntp]# more /etc/ntp/step-tickers
# List of servers used for initial synchronization.
[root@localhost ntp]# vi /etc/ntp/step-tickers
# List of servers used for initial synchronization.
server 192.168.7.49 prefer
server 0.rhel.pool.ntp.org
server 1.rhel.pool.ntp.org
server 2.rhel.pool.ntp.org
server 3.rhel.pool.ntp.org



C:配置/etc/sysconfig/ntpd文件

ntp服务,默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd文件,在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes 这样,就可以让硬件时间与系统时间一起同步。

复制代码代码如下:
#允许BIOS与系统时间同步,也可以通过hwclock -w 命令
SYNC_HWCLOCK=yes

 

IPTABLES 配置

由于NTP服务需要使用到UDP端口号123,所以当系统的防火墙(Iptables)启动的情况下,必须开放UDP端口号123。

复制代码代码如下:
[root@localhost ~]#/etc/init.d/iptables status
Table: filter
Chain INPUT (policy ACCEPT)
numtarget prot opt source destination
1ACCEPT all--0.0.0.0/00.0.0.0/0 state RELATED,ESTABLISHED
2ACCEPT icmp --0.0.0.0/00.0.0.0/0
3ACCEPT all--0.0.0.0/00.0.0.0/0
4ACCEPT tcp--0.0.0.0/00.0.0.0/0 state NEW tcp dpt:22
5REJECT all--0.0.0.0/00.0.0.0/0 reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
numtarget prot opt source destination
1REJECT all--0.0.0.0/00.0.0.0/0 reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
numtarget prot opt source destination

[root@localhost ~]# /sbin/iptables -I INPUT -p udp --dport 123 -j ACCEPT
[root@localhost ~]#/etc/init.d/iptables status
Table: filter
Chain INPUT (policy ACCEPT)
numtarget prot opt source destination
1ACCEPT udp--0.0.0.0/00.0.0.0/0 udp dpt:123
2ACCEPT all--0.0.0.0/00.0.0.0/0 state RELATED,ESTABLISHED
3ACCEPT icmp --0.0.0.0/00.0.0.0/0
4ACCEPT all--0.0.0.0/00.0.0.0/0
5ACCEPT tcp--0.0.0.0/00.0.0.0/0 state NEW tcp dpt:22
6REJECT all--0.0.0.0/00.0.0.0/0 reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
numtarget prot opt source destination
1REJECT all--0.0.0.0/00.0.0.0/0 reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
numtarget prot opt source destination

[root@localhost ~]#


如果防火墙没有开放UDP端口号123,有可能出现下面情况。

复制代码代码如下:
[root@localhost ~]# /usr/sbin/ntpq -c rv | grep stratum
stratum=16, precision=-24, rootdelay=0.000, rootdisp=3.525, refid=INIT,
[root@localhost~]#
A stratum level of 16 indicates that NTP is not synchronizing correctly.If a stratum level of 16 is detected, wait 15 minutes and issue the command again. It may take this long for the NTP server to stabilize.If NTP continues to detect a stratum level of 16, verify that the NTP port (UDP Port 123) is open on all firewalls between the cluster and the remote machine you are attempting to synchronize to.

 

启动NTP服务

复制代码代码如下:
[root@localhost ~]# service ntpd status
ntpd is stopped
[root@localhost ~]# service ntpd start
Starting ntpd: [OK]
[root@localhost ~]#

service ntpd status#查看ntpd服务状态
service ntpd start #启动ntpd服务
service ntpd stop#停止ntpd服务
service ntpd restart #重启ntpd服务
检查ntp服务是否开机启动,将其设置为开机启动。
[root@localhost ~]# chkconfig --list ntpd
ntpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@localhost ~]# runlevel
N 3
[root@localhost ~]# chkconfig ntpd on #在运行级别2、3、4、5上设置为自动运行
[root@localhost ~]# chkconfig --list ntpd
ntpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@localhost ~]#

如果要设置在运行级别上自动运行,可以使用下面命令

复制代码代码如下:
chkconfig --level 345 ntpd on

可以用下面命令检测NTP服务是否运行

复制代码代码如下:
[root@localhost ~]# pgrep ntpd
2639
2641
[root@localhost ~]# netstat -tlunp | grep ntp #如果看到123端口,说明ntp服务成功启动。
udp00 192.168.7.224:123 0.0.0.0:* 2639/ntpd
udp00 127.0.0.1:123 0.0.0.0:* 2639/ntpd
udp00 0.0.0.0:123 0.0.0.0:* 2639/ntpd
udp00 fe80::250:56ff:feb3:b5:123:::*2639/ntpd
udp00 ::1:123 :::*2639/ntpd
udp00 :::123:::*2639/ntpd
[root@localhost ~]#


查看ntp服务器有无和上层ntp连通

复制代码代码如下:
[root@localhost ~]# ntpstat
synchronised to NTP server (192.168.7.49) at stratum 6
time correct to within 440 ms
polling server every 128 s
[root@localhost ~]#

查看ntp服务器与上层ntp的状态

remote   - 本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先

refid    - 参考上一层ntp主机地址

st       - stratum阶层

when     - 多少秒前曾经同步过时间

poll     - 下次更新在多少秒后

reach    - 已经向上层ntp服务器要求更新的次数

delay    - 网络延迟

offset   - 时间补偿

jitter   - 系统时间与bios时间差

要查看 ntpd 进程的状态,请运行以下命令,按 Ctrl+C 停止查看进程。

remote   - 本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先

refid    - 参考上一层ntp主机地址

st       - stratum阶层

when     - 多少秒前曾经同步过时间

poll     - 下次更新在多少秒后

reach    - 已经向上层ntp服务器要求更新的次数

delay    - 网络延迟

offset   - 时间补偿

jitter   - 系统时间与bios时间差

要查看 ntpd 进程的状态,请运行以下命令,按 Ctrl+C 停止查看进程。
标签:

推荐系统

  • 微软Win11原版22H2下载_Win11GHOST 免 激活密钥 22H2正式版64位免费下载

    微软Win11原版22H2下载_Win11GHOST 免 激活密钥 22H2正式版64位免费下载

    语言:中文版系统大小:5.13GB系统类型:Win11

    微软Win11原版22H2下载_Win11GHOST 免 激活密钥 22H2正式版64位免费下载系统在家用办公上跑分表现都是非常优秀,完美的兼容各种硬件和软件,运行环境安全可靠稳定。Win11 64位 Office办公版(免费)优化  1、保留 Edge浏览器。  2、隐藏“操作中心”托盘图标。  3、保留常用组件(微软商店,计算器,图片查看器等)。  5、关闭天气资讯。 

  • Win11 21H2 官方正式版下载_Win11 21H2最新系统免激活下载

    Win11 21H2 官方正式版下载_Win11 21H2最新系统免激活下载

    语言:中文版系统大小:4.75GB系统类型:Win11

    Ghost Win11 21H2是微软在系统方面技术积累雄厚深耕多年,Ghost Win11 21H2系统在家用办公上跑分表现都是非常优秀,完美的兼容各种硬件和软件,运行环境安全可靠稳定。Ghost Win11 21H2是微软最新发布的KB5019961补丁升级而来的最新版的21H2系统,以Windows 11 21H2 22000 1219 专业版为基础进行优化,保持原汁原味,系统流畅稳定,保留常用组件

  • windows11中文版镜像 微软win11正式版简体中文GHOST ISO镜像64位系统下载

    windows11中文版镜像 微软win11正式版简体中文GHOST ISO镜像64位系统下载

    语言:中文版系统大小:5.31GB系统类型:Win11

    windows11中文版镜像 微软win11正式版简体中文GHOST ISO镜像64位系统下载,微软win11发布快大半年了,其中做了很多次补丁和修复一些BUG,比之前的版本有一些功能上的调整,目前已经升级到最新版本的镜像系统,并且优化了自动激活,永久使用。windows11中文版镜像国内镜像下载地址微软windows11正式版镜像 介绍:1、对函数算法进行了一定程度的简化和优化

  • 微软windows11正式版GHOST ISO镜像 win11下载 国内最新版渠道下载

    微软windows11正式版GHOST ISO镜像 win11下载 国内最新版渠道下载

    语言:中文版系统大小:5.31GB系统类型:Win11

    微软windows11正式版GHOST ISO镜像 win11下载 国内最新版渠道下载,微软2022年正式推出了win11系统,很多人迫不及待的要体验,本站提供了最新版的微软Windows11正式版系统下载,微软windows11正式版镜像 是一款功能超级强大的装机系统,是微软方面全新推出的装机系统,这款系统可以通过pe直接的完成安装,对此系统感兴趣,想要使用的用户们就快来下载

  • 微软windows11系统下载 微软原版 Ghost win11 X64 正式版ISO镜像文件

    微软windows11系统下载 微软原版 Ghost win11 X64 正式版ISO镜像文件

    语言:中文版系统大小:0MB系统类型:Win11

    微软Ghost win11 正式版镜像文件是一款由微软方面推出的优秀全新装机系统,这款系统的新功能非常多,用户们能够在这里体验到最富有人性化的设计等,且全新的柔软界面,看起来非常的舒服~微软Ghost win11 正式版镜像文件介绍:1、与各种硬件设备兼容。 更好地完成用户安装并有效地使用。2、稳定使用蓝屏,系统不再兼容,更能享受无缝的系统服务。3、为

  • 雨林木风Windows11专业版 Ghost Win11官方正式版 (22H2) 系统下载

    雨林木风Windows11专业版 Ghost Win11官方正式版 (22H2) 系统下载

    语言:中文版系统大小:4.75GB系统类型:

    雨林木风Windows11专业版 Ghost Win11官方正式版 (22H2) 系统下载在系统方面技术积累雄厚深耕多年,打造了国内重装系统行业的雨林木风品牌,其系统口碑得到许多人认可,积累了广大的用户群体,雨林木风是一款稳定流畅的系统,一直以来都以用户为中心,是由雨林木风团队推出的Windows11国内镜像版,基于国内用户的习惯,做了系统性能的优化,采用了新的系统

  • 雨林木风win7旗舰版系统下载 win7 32位旗舰版 GHOST 免激活镜像ISO

    雨林木风win7旗舰版系统下载 win7 32位旗舰版 GHOST 免激活镜像ISO

    语言:中文版系统大小:5.91GB系统类型:Win7

    雨林木风win7旗舰版系统下载 win7 32位旗舰版 GHOST 免激活镜像ISO在系统方面技术积累雄厚深耕多年,加固了系统安全策略,雨林木风win7旗舰版系统在家用办公上跑分表现都是非常优秀,完美的兼容各种硬件和软件,运行环境安全可靠稳定。win7 32位旗舰装机版 v2019 05能够帮助用户们进行系统的一键安装、快速装机等,系统中的内容全面,能够为广大用户

  • 番茄花园Ghost Win7 x64 SP1稳定装机版2022年7月(64位) 高速下载

    番茄花园Ghost Win7 x64 SP1稳定装机版2022年7月(64位) 高速下载

    语言:中文版系统大小:3.91GB系统类型:Win7

    欢迎使用 番茄花园 Ghost Win7 x64 SP1 2022.07 极速装机版 专业装机版具有更安全、更稳定、更人性化等特点。集成最常用的装机软件,集成最全面的硬件驱动,精心挑选的系统维护工具,加上独有人性化的设计。是电脑城、个人、公司快速装机之首选!拥有此系统