首页 > 编程知识 正文

ntp时间同步配置,ntp丢失同步

时间:2023-05-05 13:51:55 阅读:60341 作者:77

简单记录使用ntp在实践中同步centos7。

安装结构时,如果虚拟机之间的实践差异太大,则会发生异常。 也可以使用命令直接修改时间,但考虑到后期维护,选择使用ntp同步时间。

此配置使用五个虚拟机进行演示。

IP用途172.17.0.82ntp客户端:与内部ntp服务器(85 )同步时间172.17.0.83ntp客户端:与内部ntp服务器) 85同步时间172.17.0.84ntp客户端:内部

首先检查是否安装了ntp。rpm -q ntp

如果未安装,则使用命令:yum -y install ntp安装ntp。

如果已安装,请继续执行下一步。

安装完成后,按如下方式设置并启动ntp,然后启动ntp :

系统启用ntpd

系统启动ntpd

首先设置ntp服务器(172.17.0.85 )

编辑配置文件(vim /etc/ntp.conf ),注释默认ntp服务地址,并使用国内地址(https://www.pool.ntp.org/zone/cn和http://www

# formoreinformationaboutthisfile,see the man pages

#NTP.conf(5,ntp_acc(5) 5,ntp_auth(5) 5,ntp_clock(5) 5,ntp_misc(5) 5,ntp_mon(5) 5

差速文件/var/lib/NTP /差速

# permittimesynchronizationwithourtimesource,but do not

# permitthesourcetoqueryormodifytheserviceonthissystem。

restrictdefaultnomodifynotrapnopeernoquery

# permitallaccessovertheloopbackinterface.this could

# be tightened as well,but to do so would effect some of

# the管理员tive functions。

restrict 127.0.0.1

restrict :1

# hostsonlocalnetworkarelessrestricted。

# restrict 192.168.1.0 mask 255.255.255.0否修改说明

restrict 172.17.0.0 mask 255.255.255.0否修改说明

# usepublicserversfromthepool.NTP.org project。

# pleaseconsiderjoiningthepool (http://www.pool.NTP.org/join.html )。

# server0. centos.pool.NTP.org I burst

# server1. centos.pool.NTP.org I burst

# server2. centos.pool.NTP.org I burst

# server3. centos.pool.NTP.org I burst

服务器0.cn.pool.NTP.org

服务器1.cn.pool.NTP.org

服务器2.cn.pool.NTP.org

服务器3.cn.pool.NTP.org

restrict0. cn.pool.NTP.orgnomodifynotrapnoquery

restrict1. cn.pool.NTP.orgnomodifynotrapnoquery

restrict2. cn.pool.NTP.orgnomodifynotrapnoquery

restrict3. cn.pool.NTP.orgnomodifynotrapnoquery

服务器127.0.0.1 #本地时钟

fudge 127.0.0.1 stratum 10

# broadcast 192.168.1.255 autokey # broadcast ser

ver
#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

# Disable the monitoring facility to prevent amplification attacks using ntpdc
# monlist command when default restrict does not include the noquery flag. See
# CVE-2013-5211 for more details.
# Note: Monitoring will not be disabled with the limited restriction flag.
disable monitor

 

修改完成后保存退出,并重启ntp(systemctl restart ntpd)。

 

设置ntp客户端(以172.17.0.82为例,其余相同,略)

编辑配置文件(vim /etc/ntp.conf),注释掉默认ntp服务,使用我们自己配置的ntp服务器(即 172.17.0.85),具体修改部分突出显示,如下:

【!!!】这里需要注意的是,在使用ntp服务器时,如果有防火墙,则需要开启ntp服务器的udp协议123端口,否则其他虚拟机不能成功同步时间。

# 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

# 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 ::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.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 172.17.0.85

restrict 172.17.0.85 nomodify notrap noquery

server 127.0.0.1 # local clock
fudge 127.0.0.1 stratum 10

#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

# Disable the monitoring facility to prevent amplification attacks using ntpdc
# monlist command when default restrict does not include the noquery flag. See
# CVE-2013-5211 for more details.
# Note: Monitoring will not be disabled with the limited restriction flag.
disable monitor

修改完成后保存退出并重启ntp(systemctl restart ntpd)。

 

重启后可以使用ntpq -p 查看网络中的NTP服务器,如下分别是85和82(其余客户端与82类似):

 

还可以使用ntpstat 命令查看时间同步状态,但是需要等待几分钟才回看到同步信息,同样,下面分别是85和82:

可以看到82在开始时候并没有同步时间,而是等待一段时间后才开始同步。

END

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。