linux下centos 各主机之间时间同步(有一台主机为外网)

注:如果防火墙开启,请开辟123端口

firewall-cmd --permanent --add-port=123/udp

firewall-cmd --reload

一.172.12.248.128设置ntp服务器

1.安装ntp服务

ntp相关rpm包获取地址:https://pan.baidu.com/s/1ZWn5JOeQsCGvqjQDimpDdg

rpm -Uvh autogen-libopts-5.18-5.el7.x86_64.rpm

rpm -Uvh ntpdate-4.2.6p5-25.el7.centos.x86_64.rpm

rpm -ivh ntp-4.2.6p5-25.el7.centos.x86_64.rpm

2.设置成开机自启动

chkconfig ntpd on (如无发开机自启动,则需要关闭chronyd服务开机自启动

systemctl disable chronyd.service)

 3.配置前先使用命令:ntpdate -u cn.pool.ntp.org 同步服务器

 4.修改/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

# 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

#改为要开放的客户端ip地址段

restrict 172.16.248.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 2.cn.pool.ntp.org

server 1.asia.pool.ntp.org

server 2.asia.pool.ntp.org

#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

# 允许上层时间服务器主动修改本机时间

restrict 2.cn.pool.ntp.org nomodify notrap noquery

restrict 1.asia.pool.ntp.org nomodify notrap noquery

restrict 2.asia.pool.ntp.org nomodify notrap noquery

 

server 127.127.1.0 # local clock

fudge 127.127.1.0 stratum 10

 

# 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

5.修改完成后重启ntpd服务 service ntpd restart

6.使用ntpstat 命令查看时间同步状态,这个一般需要5-10分钟后才能成功连接和同步。所以,服务器启动后需要稍等下:

刚启动的时候,一般是:

# ntpstat

unsynchronised

  time server re-starting

   polling server every 64 s

连接并同步后:

# ntpstat

synchronised to NTP server (202.112.10.36) at stratum 3

   time correct to within 275 ms

   polling server every 256 s

 

二.设置ntp客户端:172.12.248.129|130|131

1.安装ntp服务并设置为自动启动,和前面的设置方式相同。

2.然后编辑/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

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

restrict 172.16.248.1 nomodify notrap noquery

server 127.127.1.0 # local clock

fudge 127.127.1.0 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

3.重启服务service ntpd restart

4.使用ntpstat 命令查看时间同步状态,这个一般需要5-10分钟后才能成功连接和同步。所以,服务器启动后需要稍等下:

刚启动的时候,一般是:

# ntpstat

unsynchronised

  time server re-starting

   polling server every 64 s

连接并同步后:

# ntpstat

synchronised to NTP server (192.168.11.212) at stratum 3

   time correct to within 275 ms

   polling server every 256 s

 

你可能感兴趣的:(linux时间同步)