广告
返回顶部
首页 > 资讯 > 操作系统 >Linux系统下NTP协议配置方式
  • 405
分享到

Linux系统下NTP协议配置方式

2023-06-12 23:06:12 405人浏览 泡泡鱼
摘要

本篇内容主要讲解“linux系统下NTP协议配置方式”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Linux系统下NTP协议配置方式”吧!在Linux系统中,为了避免主机时间因为在长时间运行下所

本篇内容主要讲解“linux系统下NTP协议配置方式”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Linux系统下NTP协议配置方式”吧!

在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


Linux系统下NTP协议配置方式
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)
num  target     prot opt source               destination        
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
2    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0          
3    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0          
4    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22
5    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited

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

Chain OUTPUT (policy ACCEPT)
num  target     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)
num  target     prot opt source               destination        
1    ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:123
2    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
3    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0          
4    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0          
5    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22
6    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited

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

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination        

[root@localhost ~]#


Linux系统下NTP协议配置方式
如果防火墙没有开放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服务成功启动。
udp        0      0 192.168.7.224:123           0.0.0.0:*                               2639/ntpd          
udp        0      0 127.0.0.1:123               0.0.0.0:*                               2639/ntpd          
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               2639/ntpd          
udp        0      0 fe80::250:56ff:feb3:b5:123  :::*                                    2639/ntpd          
udp        0      0 ::1:123                     :::*                                    2639/ntpd          
udp        0      0 :::123                      :::*                                    2639/ntpd          
[root@localhost ~]#


Linux系统下NTP协议配置方式
查看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的状态
Linux系统下NTP协议配置方式
remote   - 本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先

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

st       - stratum阶层

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

poll     - 下次更新在多少秒后

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

delay    - 网络延迟

offset   - 时间补偿

jitter   - 系统时间与bios时间差

要查看 ntpd 进程的状态,请运行以下命令,按 Ctrl+C 停止查看进程。
Linux系统下NTP协议配置方式
remote   - 本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先

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

st       - stratum阶层

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

poll     - 下次更新在多少秒后

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

delay    - 网络延迟

offset   - 时间补偿

jitter   - 系统时间与bios时间差

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

到此,相信大家对“Linux系统下NTP协议配置方式”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

--结束END--

本文标题: Linux系统下NTP协议配置方式

本文链接: https://www.lsjlt.com/news/263405.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Linux系统下NTP协议配置方式
    本篇内容主要讲解“Linux系统下NTP协议配置方式”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Linux系统下NTP协议配置方式”吧!在Linux系统中,为了避免主机时间因为在长时间运行下所...
    99+
    2023-06-12
  • xp系统下用netsh配置nat协议方法
    WindowsXP 中文版+SP1 单网卡,具有CERNET内独立IP   Step 0 确认ICS即网络连接共享处于关闭状态   Step 1 按Win+R 输入并运行services.msc,在服...
    99+
    2023-06-05
    netsh ant 协议 系统 nat xp
  • Linux系统如何配置ntp系统
    这篇文章将为大家详细讲解有关Linux系统如何配置ntp系统,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。数据产生与处理系统是各种计算设备集群的,计算设备将统一、同步的标准时间用于记录各种事件发生时序,如...
    99+
    2023-06-28
  • Linux系统如何配置NTP服务
    小编给大家分享一下Linux系统如何配置NTP服务,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!NTP简介:NTP是用来使计算机时间同步化的一种协议,它可以使计算...
    99+
    2023-06-28
  • xp系统下怎么用netsh配置nat协议
    这篇文章主要讲解了“xp系统下怎么用netsh配置nat协议”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“xp系统下怎么用netsh配置nat协议”吧!WindowsXP 中文版+SP1 单...
    99+
    2023-06-13
  • Linux服务器snmp协议v2/v3配置方法
    Snmp V2 配置方法 1、确保本机已经安装了snmp服务 [root@idc ~]# rpm -qa |grep snmp net-snmp-libs-5.1.2-11.EL4.7 net-snmp-5.1.2-11.EL4...
    99+
    2023-09-14
    网络协议 Powered by 金山文档
  • Linux系统下Git的基本配置和使用方式
    本篇内容主要讲解“Linux系统下Git的基本配置和使用方式”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Linux系统下Git的基本配置和使用方式”吧!git config配置文件一.Git已...
    99+
    2023-06-12
  • Linux系统下SystemC环境配置方法
    以下为centos7下配置方法 下载systemc源码包:SystemC (accellera.org) 将压缩包放置到用户目录下,并解压 tar -zxvf systemc-2.3.3.tar.gz 进入到s...
    99+
    2022-06-04
    Linux系统SystemC环境 SystemC环境搭建
  • Linux系统中Tomcat环境配置方式
    目录1.下载tomcat 安装包2.上传tomcat安装包并解压3.配置环境变量4.输入 I 键5.复制配置信息6. 按 Esc 键7.验证tomcat是否安装成功 (默认是8080端口)8.如图tomcat启动成功总结...
    99+
    2023-04-07
    Linux Tomcat环境 Linux配置Tomcat环境 Linux配置Tomcat
  • Linux系统中jdk环境配置方式
    目录1.下载jdk安装包 3.上传jdk安装包到linux4.解压jdk安装包5.6.验证是否配置jdk成功总结如下操作步骤为linux系统中部署jdk环境 1.下载jdk安装包  jdk官网下载地址...
    99+
    2023-04-07
    Linux配置jdk环境 jdk环境配置 Linux jdk环境
  • Linux系统下如何配置SSH
    本篇内容介绍了“Linux系统下如何配置SSH”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!SSH是一个用来替代TELNET、FTP以及R命...
    99+
    2023-06-10
  • linux系统下DHCP服务器的配置方法
    这篇文章主要讲解了“linux系统下DHCP服务器的配置方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“linux系统下DHCP服务器的配置方法”吧! 安装dhcp服务器软件Rh...
    99+
    2023-06-10
  • Linux系统下Nginx支持ipv6配置的方法
    一、查看现有nginx是否支持ipv6 需要执行以下命令,查看现有nginx是否支持ipv6,如果参数中具有--with-ipv6,则支持,如 果没有,则不支持,需要重新编译nginx。 # 此处的nginx路径以...
    99+
    2022-06-04
    Nginx支持ipv6 Nginx ipv6配置
  • linux系统下vnc的配置及使用方法
    这篇文章主要讲解了“linux系统下vnc的配置及使用方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“linux系统下vnc的配置及使用方法”吧!VNC基本上是由两部分组成:一部分是客户端...
    99+
    2023-06-10
  • windows 7系统中正确配置ipv6协议地址的方法介绍
        &nbs   事实上,与以前的那些操作系统相比,Windows 7操作系统已经在支持IPV6协议上做出了很好了;但是,仍然有一些瑕疵。   首先想到的一个问题是在Windo...
    99+
    2023-05-29
    windows 7 系统 正确配置 ipv6协议 协议 ipv6 地址 7系统 方法
  • 云服务器如何配置路由协议信息系统
    云服务器可以配置路由协议来帮助客户端通过网络连接到服务器。以下是一些常用的路由协议以及它们如何工作的一些细节: Route Name Relationship:Route Name Relationship (RR) 协议用于在路由中设...
    99+
    2023-10-27
    信息系统 路由协议 服务器
  • Linux下如何查看系统配置
    这篇文章将为大家详细讲解有关Linux下如何查看系统配置,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。在linux系统中,经常会遇到一些问题,在这个时候就需要查看系统的配置。CPU1. lscpu:显示c...
    99+
    2023-06-28
  • Linux系统下安装配置 OpenLDAP + phpLDAPadmin
    Linux系统下安装配置 OpenLDAP + phpLDAPadmin实验环境:操作系统:Centos 7.4服务器ip:192.168.3.41运行用户:root网络环境:InternetLDAP(轻量级目录访问协议)是一个能实现提供被...
    99+
    2023-06-05
  • Redhat Linux系统下磁盘配额的设置方法
    本篇内容主要讲解“Redhat Linux系统下磁盘配额的设置方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Redhat Linux系统下磁盘配额的设置方法”吧!配额可以使各个用户和组无法占...
    99+
    2023-06-10
  • Linux系统下iptables基本配置方法有哪些
    这篇文章主要介绍Linux系统下iptables基本配置方法有哪些,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!Linux系统下配置iptables的具体步骤:1、查看本机关于IPTABLES的设置情况[root@w...
    99+
    2023-06-16
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作