下龙山一号楼

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

yum install ntp

systemctl start ntpd

开放udp 123端口

vim /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).

#记录时间差异,BIOS 的芯片震荡周期频率与上层Time Server的时间差异
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

#ignore:拒绝所有类型的 NTP 联机
#nomodify:客户端不能使用 ntpc 与 ntpq 这两支程序来修改服务器的时间参数, 但客户端仍可透过这部主机来进行网络校时的;
#noquery:客户端不能够使用 ntpq, ntpc 等指令来查询时间服务器,等于不提供 NTP 的网络校时啰;
#notrap:不提供 trap 这个远程事件登录 (remote event logging) 的功能。
#notrust:拒绝没有认证的客户端。

# 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
restrict 192.168.1.1 mask 255.255.255.0 nomodify
# 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

#这里上层的是阿里云ntp服务器
server ntp1.aliyun.com prefer
server ntp2.aliyun.com
server ntp3.aliyun.com

#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

 

 

Chrony 是 NTP 客户端的替代品。它能以更精确的时间和更快的速度同步时钟,并且它对于那些不是全天候在线的系统非常有用。

chronyd 更小、更节能,它占用更少的内存且仅当需要时它才唤醒 CPU。即使网络拥塞较长时间,它也能很好地运行。它支持 Linux 上的硬件时间戳,允许在本地网络进行极其准确的同步。

它提供下列两个服务。

  • chronyc:Chrony 的命令行接口。
  • chronyd:Chrony 守护进程服务。

对于 RHEL/CentOS 系统,使用 YUM 命令 去安装 chrony。

  1. $ sudo yum install chrony

对于 Debian/Ubuntu 系统,使用 APT-GET 命令 或者 APT 命令 去安装 chrony。

  1. $ sudo apt install chrony

 

NTP 客户端需要知道它要连接到哪个 NTP 服务器来获取当前时间。我们可以直接在该 NTP 配置文件中的 server 或者 pool 项指定 NTP 服务器。通常,默认的配置文件位于 /etc/chrony/chrony.conf 或者 /etc/chrony.conf,取决于 Linux 发行版版本。为了更可靠的同步时间,建议指定至少三个服务器。

下面几行是我的 Ubuntu 18.04 LTS 服务器上的一个示例。

    [...]
    # About using servers from the NTP Pool Project in general see (LP: #104525).
    # Approved by Ubuntu Technical Board on 2011-02-08.
    # See http://www.pool.ntp.org/join.html for more information.
    pool ntp.ubuntu.com iburst maxsources 4
    pool 0.ubuntu.pool.ntp.org iburst maxsources 1
    pool 1.ubuntu.pool.ntp.org iburst maxsources 1
    pool 2.ubuntu.pool.ntp.org iburst maxsources 2
    [...]

从上面的输出中你可以看到,NTP 服务器池项目 已经被设置成为了默认的时间服务器。对于那些好奇的人,NTP 服务器池项目是一个时间服务器集群,用来为全世界千万个客户端提供 NTP 服务。它是 Ubuntu 以及其他主流 Linux 发行版的默认时间服务器。

在这里, * iburst 选项用来加速初始的同步过程 * maxsources 代表 NTP 源的最大数量

请确保你选择的 NTP 服务器是同步的、稳定的、离你的位置较近的,以便使用这些 NTP 源来提升时间准确度。

posted on 2019-11-04 16:03  下龙山王俊凯  阅读(714)  评论(0编辑  收藏  举报